#include <GUIConstants.au3>
#include <GDIPlus.au3>
#include "VectorMath.au3"
#include <Misc.au3>
#include <WinAPI.au3>
#include <Array.au3>
#include "MouseOnEvent.au3"

; - Author: name22(www.autoit.de)
; - Modified: fr34q

Opt("GUIOnEventMode", 1)

$iGUIWidth = @DesktopWidth;600
$iGUIHeight = @DesktopHeight;400
$bIsFullScreen = True ; Nur setzen, wenn ihr bei Width und Height die Desktop-maße gewählt habt
$nFPS = 100 ; Default: 50
$bTrapMouse = True

$nScaleFactor = $iGUIHeight / 400

$nFriction = 0.98
$nSlowMotion = 4

$nPlayerSpeedMax = 200*$nScaleFactor
$nAccelerationForce = 10*$nScaleFactor

$nProjectileSpeedPlayer = 500*$nScaleFactor
$iCoolDownPlayer = 200

$nLifeForcePlayer = 60
$nHealthRegenSpeed = 1

$nSlowMotionFuelMax = 100
$nSlowMotionFuel = 100
$nSlowMotionRefillSpeed = 8
$nSlowMotionConsumptionSpeed = 20

$nWaveIdleTime = 3000
$iWaveCounter = 1

$nSleepTime = 1000 / $nFPS
$nOffsetSleepTime = 0

$nFPS_Display = 0
$nFPS_Average = 0
$iFPS_Counter = 1

$bNewGame = True
$bNewWave = True
$bPaused = False

Global $hBitmapBG

$nTimeMod = 1

$vUser32Dll = DllOpen("User32.dll")
$vNTdll = DllOpen("ntdll.dll")

$tRectBorder = DllStructCreate($tagRECT)
$tPoint = DllStructCreate($tagPOINT)
DllStructSetData($tRectBorder, "Left", 0)
DllStructSetData($tRectBorder, "Top", 0)
DllStructSetData($tRectBorder, "Right", $iGUIWidth)
DllStructSetData($tRectBorder, "Bottom", $iGUIHeight)

$tPrecSleep = DllStructCreate("int64 time;")
$pPrecSleep = DllStructGetPtr($tPrecSleep)

If $bIsFullScreen Then
	$hWnd = GUICreate("AutoIt Arena Fight v1.0  - Author: name22(www.autoit.de)", $iGUIWidth, $iGUIHeight,0,0,0x80800000)
Else
	$hWnd = GUICreate("AutoIt Arena Fight v1.0  - Author: name22(www.autoit.de)", $iGUIWidth, $iGUIHeight)
EndIf
GUISetCursor(16, 1, $hWnd)
GUISetState()

$hDC = _WinAPI_GetDC($hWnd)
$hCDC_Buffer = _WinAPI_CreateCompatibleDC($hDC)
$hGDI_Bitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iGUIWidth, $iGUIHeight)
_WinAPI_SelectObject($hCDC_Buffer, $hGDI_Bitmap)

_GDIPlus_Startup()

$hGraphic = _GDIPlus_GraphicsCreateFromHDC($hCDC_Buffer)
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
_GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)

_WinAPI_BitBlt($hDC, 0, 0, $iGUIWidth, $iGUIHeight, $hCDC_Buffer, 0, 0, 0x00CC0020)

$hBrush_Enemy = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hBrush_Player = _GDIPlus_BrushCreateSolid(0xFF0000FF)
$hBrush_FPS = _GDIPlus_BrushCreateSolid(0xFF000000)
$hBrush_BG_Menu = _GDIPlus_BrushCreateSolid("0x" & Hex(200, 2) & "F0F0F0")
$hBrush_ProjectileBG = _GDIPlus_BrushCreateSolid(0xFF000000)
$hBrush_MenuTextNormal = _GDIPlus_BrushCreateSolid(0xFF000000)
$hBrush_MenuTextHighlight = _GDIPlus_BrushCreateSolid(0xFF808080)
$hBrush_MenuTextGreyedOut = _GDIPlus_BrushCreateSolid(0xFFC0C0C0)
$hBrush_LifeBar = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hBrush_SlowMotionBar = _GDIPlus_BrushCreateSolid(0xFF0000FF)
$hBrush_Announcement = _GDIPlus_BrushCreateSolid("0x" & Hex(200, 2) & "000000")

$hPen_Bar = _GDIPlus_PenCreate(0xFF000000, 2)
$hPen_Cursor = _GDIPlus_PenCreate(0xFF505050, 2)

$hStringFormat = _GDIPlus_StringFormatCreate()
$hFamily_SegoeUI = _GDIPlus_FontFamilyCreate("Segoe UI")
$hFont_FPS = _GDIPlus_FontCreate($hFamily_SegoeUI, 7*$nScaleFactor)
$hFont_Menu = _GDIPlus_FontCreate($hFamily_SegoeUI, 20*$nScaleFactor)
$hFont_Announcement = _GDIPlus_FontCreate($hFamily_SegoeUI, 50*$nScaleFactor)

$aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, "FPS 000", $hFont_FPS, _GDIPlus_RectFCreate(), $hStringFormat)
$tLayout_FPS = $aMeasure[0]
$aMeasure = ""
DllStructSetData($tLayout_FPS, "X", $iGUIWidth - DllStructGetData($tLayout_FPS, "Width") - 3)
DllStructSetData($tLayout_FPS, "Y", $iGUIHeight - DllStructGetData($tLayout_FPS, "Height"))
DllStructSetData($tLayout_FPS, "Width", DllStructGetData($tLayout_FPS, "Width") + 3)

#cs
$aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, "Continue", $hFont_Menu, _GDIPlus_RectFCreate(0, 100), $hStringFormat)
$tLayout_Menu_Continue = $aMeasure[0]
$aMeasure = ""
DllStructSetData($tLayout_Menu_Continue, "X", $iGUIWidth / 2 - DllStructGetData($tLayout_Menu_Continue, "Width") / 2)
DllStructSetData($tLayout_Menu_Continue, "Width", DllStructGetData($tLayout_Menu_Continue, "Width") + 1)
#ce
$tLayout_Menu_Continue = _CreateMenuItem("Continue",100*$nScaleFactor)
$tLayout_Menu_Start = _CreateMenuItem("Start",100*$nScaleFactor)
$tLayout_Menu_Options = _CreateMenuItem("Options",150*$nScaleFactor)
$tLayout_Menu_Quit = _CreateMenuItem("Quit",200*$nScaleFactor)

#region Menu-Items für das Menü Options
$tLayout_MOpt_Resolution = _CreateMenuItem("Change Resolution",100*$nScaleFactor)
$tLayout_MOpt_Control = _CreateMenuItem("Set Control",150*$nScaleFactor)
$tLayout_MOpt_Return = _CreateMenuItem("Return",200*$nScaleFactor)
#endregion

$aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, "Wave 1", $hFont_Announcement, _GDIPlus_RectFCreate(), $hStringFormat)
$tLayout_Announcement = $aMeasure[0]
$aMeasure = ""
DllStructSetData($tLayout_Announcement, "X", $iGUIWidth / 2 - DllStructGetData($tLayout_Announcement, "Width") / 2)
DllStructSetData($tLayout_Announcement, "Y", $iGUIHeight / 2 - DllStructGetData($tLayout_Announcement, "Height") / 2)
DllStructSetData($tLayout_Announcement, "Width", 0)
DllStructSetData($tLayout_Announcement, "Height", 0)

$nX_PlayerPos = Random(10, $iGUIWidth - 10)
$nY_PlayerPos = Random(10, $iGUIHeight - 10)
$nX_PlayerSpeed = 0
$nY_PlayerSpeed = 0
$nLifeForce = $nLifeForcePlayer

Global $aEnemys[1][6] = [[0]], $aProjectiles[1][6] = [[0]], $aAccelForce = _Vector_Create(0, 0), $nProjectileSpeedEnemy

GUISetOnEvent($GUI_EVENT_CLOSE, "_Close", $hWnd)

If $bTrapMouse Then _OnMouseRegister()

_Pause()
$bNewGame = False

$nT_CoolDown = TimerInit()
$nT_WaveIdle = TimerInit()
$nRestartCheck = 0

$nT_Sleep = TimerInit() + $nSleepTime
$nT_UpdateFPS = TimerInit()
$nT_AdjustSleepTime = TimerInit()

While True
	DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep) + $nOffsetSleepTime))
	DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
	$nFrameTime = TimerDiff($nT_Sleep)
	$nT_Sleep = TimerInit()

	$nFPS_Cur = 1000 / $nFrameTime
	If TimerDiff($nT_UpdateFPS) >= 500 Then
		$nFPS_Average = ($nFPS_Average * ($iFPS_Counter - 1) + $nFPS_Cur) / $iFPS_Counter
		$iFPS_Counter += 1
		$nFPS_Display = $nFPS_Cur
		$nT_UpdateFPS = TimerInit()
	EndIf
	If TimerDiff($nT_AdjustSleepTime) >= 5000 Then
		If $nFPS_Average - $nFPS < -2 Then $nOffsetSleepTime -= 0.5
		If $nFPS_Average - $nFPS > 2 Then $nOffsetSleepTime += 0.5
		$iFPS_Counter = 1
		$nT_AdjustSleepTime = TimerInit()
	EndIf

	Switch _IsPressed("20", $vUser32Dll)
		Case True
			Switch $nTimeMod
				Case 1
					If $nSlowMotionFuel >= 10 Then
						$nTimeMod = $nSlowMotion
						$nSlowMotionFuel -= $nSlowMotionConsumptionSpeed / $nFPS_Cur
					EndIf
				Case $nSlowMotion
					If $nSlowMotionFuel > 0 Then
						$nSlowMotionFuel -= $nSlowMotionConsumptionSpeed / $nFPS_Cur
					Else
						$nTimeMod = 1
					EndIf
			EndSwitch
		Case False
			$nTimeMod = 1
	EndSwitch

	$nFPS_Cur *= $nTimeMod

	Select
		Case $nLifeForce >= 1 And $nLifeForce < $nLifeForcePlayer
			$nLifeForce += $nHealthRegenSpeed / $nFPS_Cur
		Case $nLifeForce > $nLifeForcePlayer
			$nLifeForce = $nLifeForcePlayer
		Case $nLifeForce <= 0
			$iWaveCounter = 1
			_Restart()
			$bNewGame = True
			_Pause()
			$bNewWave = True
	EndSelect

	Select
		Case $nSlowMotionFuel >= 0 And $nSlowMotionFuel < $nSlowMotionFuelMax
			$nSlowMotionFuel += $nSlowMotionRefillSpeed / $nFPS_Cur
		Case $nSlowMotionFuel > $nSlowMotionFuelMax
			$nSlowMotionFuel = $nSlowMotionFuelMax
		Case $nSlowMotionFuel < 0
			$nSlowMotionFuel = 0
	EndSelect


	_GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)

	Switch TimerDiff($nT_WaveIdle) < $nWaveIdleTime
		Case True
			Switch $bNewWave
				Case True
					$nLifeForce = $nLifeForcePlayer
					Dim $aProjectiles[1][6] = [[0]]
					$aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, "Wave 1", $hFont_Announcement, _GDIPlus_RectFCreate(), $hStringFormat)
					$tLayout_Announcement = $aMeasure[0]
					$aMeasure = ""
					DllStructSetData($tLayout_Announcement, "X", $iGUIWidth / 2 - DllStructGetData($tLayout_Announcement, "Width") / 2)
					DllStructSetData($tLayout_Announcement, "Y", $iGUIHeight / 2 - DllStructGetData($tLayout_Announcement, "Height") / 2)
					DllStructSetData($tLayout_Announcement, "Width", 0)
					DllStructSetData($tLayout_Announcement, "Height", 0)
					$bNewWave = False
			EndSwitch
			_GDIPlus_GraphicsDrawStringEx($hGraphic, "Wave " & $iWaveCounter, $hFont_Announcement, $tLayout_Announcement, $hStringFormat, $hBrush_Announcement)
		Case False
			If $nRestartCheck < $nT_WaveIdle Then
				_Restart()
				$nRestartCheck = $nT_WaveIdle
			EndIf
			If $aEnemys[0][0] = 0 Then
				$nT_WaveIdle = TimerInit()
				$bNewWave = True
				$iWaveCounter += 1
			EndIf
	EndSwitch

	$aCursorInfo = GUIGetCursorInfo($hWnd)
	If $aCursorInfo[2] And TimerDiff($nT_CoolDown) >= $iCoolDownPlayer * $nTimeMod Then
		$aProjectiles[0][0] += 1
		ReDim $aProjectiles[$aProjectiles[0][0] + 1][6]

		$aDiff = _Vector_Create($aCursorInfo[0] - $nX_PlayerPos, $aCursorInfo[1] - $nY_PlayerPos)
		$aDirection = _Vector_Normalize($aDiff)

		$aProjectiles[$aProjectiles[0][0]][0] = $nX_PlayerPos
		$aProjectiles[$aProjectiles[0][0]][1] = $nY_PlayerPos
		$aProjectiles[$aProjectiles[0][0]][2] = $aDirection[0] * $nProjectileSpeedPlayer
		$aProjectiles[$aProjectiles[0][0]][3] = $aDirection[1] * $nProjectileSpeedPlayer
		$aProjectiles[$aProjectiles[0][0]][4] = 8
		$aProjectiles[$aProjectiles[0][0]][5] = 1

		$nT_CoolDown = TimerInit()
	EndIf

	$aAccelForce[0] = 0
	$aAccelForce[1] = 0

	If _IsPressed("25", $vUser32Dll) Or _IsPressed("41", $vUser32Dll) And Sqrt($nX_PlayerSpeed ^ 2 + $nY_PlayerSpeed ^ 2) < $nPlayerSpeedMax Then $aAccelForce[0] = -1
	If _IsPressed("26", $vUser32Dll) Or _IsPressed("57", $vUser32Dll) And Sqrt($nX_PlayerSpeed ^ 2 + $nY_PlayerSpeed ^ 2) < $nPlayerSpeedMax Then $aAccelForce[1] = -1
	If _IsPressed("27", $vUser32Dll) Or _IsPressed("44", $vUser32Dll) And Sqrt($nX_PlayerSpeed ^ 2 + $nY_PlayerSpeed ^ 2) < $nPlayerSpeedMax Then $aAccelForce[0] = 1
	If _IsPressed("28", $vUser32Dll) Or _IsPressed("53", $vUser32Dll) And Sqrt($nX_PlayerSpeed ^ 2 + $nY_PlayerSpeed ^ 2) < $nPlayerSpeedMax Then $aAccelForce[1] = 1

	_GDIPlus_GraphicsFillEllipse($hGraphic, $nX_PlayerPos - (10*$nScaleFactor), $nY_PlayerPos - (10*$nScaleFactor), 20*$nScaleFactor, 20*$nScaleFactor, $hBrush_Player)

	$aAccelDirection = _Vector_Normalize($aAccelForce)
	$nX_PlayerSpeed += $aAccelDirection[0] * $nAccelerationForce
	$nY_PlayerSpeed += $aAccelDirection[1] * $nAccelerationForce

	$nX_PlayerSpeed *= $nFriction
	$nY_PlayerSpeed *= $nFriction
	$nX_PlayerPos += $nX_PlayerSpeed / $nFPS_Cur
	$nY_PlayerPos += $nY_PlayerSpeed / $nFPS_Cur

	If $nX_PlayerPos - (10*$nScaleFactor) <= 0 And $nX_PlayerSpeed < 0 Then
		$nX_PlayerSpeed *= -1
		$nX_PlayerPos = 10*$nScaleFactor
	EndIf
	If $nX_PlayerPos + (10*$nScaleFactor) >= $iGUIWidth And $nX_PlayerSpeed > 0 Then
		$nX_PlayerSpeed *= -1
		$nX_PlayerPos = $iGUIWidth - 10*$nScaleFactor
	EndIf
	If $nY_PlayerPos - (10*$nScaleFactor) <= 0 And $nY_PlayerSpeed < 0 Then
		$nY_PlayerSpeed *= -1
		$nY_PlayerPos = 10*$nScaleFactor
	EndIf
	If $nY_PlayerPos + (10*$nScaleFactor) >= $iGUIHeight And $nY_PlayerSpeed > 0 Then
		$nY_PlayerSpeed *= -1
		$nY_PlayerPos = $iGUIHeight - 10*$nScaleFactor
	EndIf

	For $i1 = 1 To $aEnemys[0][0]
		If $i1 > $aEnemys[0][0] Then ExitLoop
		If $aEnemys[$i1][4] <= 0 Then
			_ArrayDelete($aEnemys, $i1)
			$aEnemys[0][0] -= 1
			$i1 -= 1
		Else
			Switch ($aEnemys[$i1][0] - $nX_PlayerPos) ^ 2 + ($aEnemys[$i1][1] - $nY_PlayerPos) ^ 2 <= (20*$nScaleFactor)^2
				Case True
					$nLifeForce -= 8
					$aEnemys[$i1][4] -= 10

					$aV_UN = _Vector_Normalize(_Vector_Create($aEnemys[$i1][0] - $nX_PlayerPos, $aEnemys[$i1][1] - $nY_PlayerPos))
					$aV_UT = _Vector_Create(-$aV_UN[1], $aV_UN[0])

					$nVel1_N = $aV_UN[0] * $aEnemys[$i1][2] + $aV_UN[1] * $aEnemys[$i1][3]
					$nVel2_N = $aV_UN[0] * $nX_PlayerSpeed + $aV_UN[1] * $nY_PlayerSpeed
					$nVel1_T = $aV_UT[0] * $aEnemys[$i1][2] + $aV_UT[1] * $aEnemys[$i1][3]
					$nVel2_T = $aV_UT[0] * $nX_PlayerSpeed + $aV_UT[1] * $nY_PlayerSpeed

					$aEnemys[$i1][2] = $nVel2_N * $aV_UN[0] + $nVel1_T * $aV_UT[0]
					$aEnemys[$i1][3] = $nVel2_N * $aV_UN[1] + $nVel1_T * $aV_UT[1]
					$nX_PlayerSpeed = $nVel1_N * $aV_UN[0] + $nVel2_T * $aV_UT[0]
					$nY_PlayerSpeed = $nVel1_N * $aV_UN[1] + $nVel2_T * $aV_UT[1]
				EndSwitch

			If TimerDiff($aEnemys[$i1][5]) >= $aEnemys[$i1][6] * $nTimeMod Then
				$aProjectiles[0][0] += 1
				ReDim $aProjectiles[$aProjectiles[0][0] + 1][6]

				$aDiff = _Vector_Create($nX_PlayerPos - $aEnemys[$i1][0], $nY_PlayerPos - $aEnemys[$i1][1])
				$aDirection = _Vector_Normalize($aDiff)

				$aProjectiles[$aProjectiles[0][0]][0] = $aEnemys[$i1][0]
				$aProjectiles[$aProjectiles[0][0]][1] = $aEnemys[$i1][1]
				$aProjectiles[$aProjectiles[0][0]][2] = $aDirection[0] * $nProjectileSpeedEnemy
				$aProjectiles[$aProjectiles[0][0]][3] = $aDirection[1] * $nProjectileSpeedEnemy
				$aProjectiles[$aProjectiles[0][0]][4] = 5
				$aProjectiles[$aProjectiles[0][0]][5] = 0

				$aEnemys[$i1][5] = TimerInit()
			EndIf

			_GDIPlus_GraphicsFillEllipse($hGraphic, $aEnemys[$i1][0] - (10*$nScaleFactor), $aEnemys[$i1][1] - (10*$nScaleFactor), 20*$nScaleFactor, 20*$nScaleFactor, $hBrush_Enemy)

			$aEnemys[$i1][0] += $aEnemys[$i1][2] / $nFPS_Cur
			$aEnemys[$i1][1] += $aEnemys[$i1][3] / $nFPS_Cur

			If $aEnemys[$i1][0] - (10*$nScaleFactor) <= 0 And $aEnemys[$i1][2] < 0 Then $aEnemys[$i1][2] *= -1
			If $aEnemys[$i1][0] + (10*$nScaleFactor) >= $iGUIWidth And $aEnemys[$i1][2] > 0 Then $aEnemys[$i1][2] *= -1
			If $aEnemys[$i1][1] - (10*$nScaleFactor) <= 0 And $aEnemys[$i1][3] < 0 Then $aEnemys[$i1][3] *= -1
			If $aEnemys[$i1][1] + (10*$nScaleFactor) >= $iGUIHeight And $aEnemys[$i1][3] > 0 Then $aEnemys[$i1][3] *= -1

			For $i2 = $i1 + 1 To $aEnemys[0][0]
				Switch ($aEnemys[$i1][0] - $aEnemys[$i2][0]) ^ 2 + ($aEnemys[$i1][1] - $aEnemys[$i2][1]) ^ 2 <= (20*$nScaleFactor)^2
					Case True
						$aV_UN = _Vector_Normalize(_Vector_Create($aEnemys[$i1][0] - $aEnemys[$i2][0], $aEnemys[$i1][1] - $aEnemys[$i2][1]))
						$aV_UT = _Vector_Create(-$aV_UN[1], $aV_UN[0])

						$nVel1_N = $aV_UN[0] * $aEnemys[$i1][2] + $aV_UN[1] * $aEnemys[$i1][3]
						$nVel2_N = $aV_UN[0] * $aEnemys[$i2][2] + $aV_UN[1] * $aEnemys[$i2][3]
						$nVel1_T = $aV_UT[0] * $aEnemys[$i1][2] + $aV_UT[1] * $aEnemys[$i1][3]
						$nVel2_T = $aV_UT[0] * $aEnemys[$i2][2] + $aV_UT[1] * $aEnemys[$i2][3]

						$aEnemys[$i1][2] = $nVel2_N * $aV_UN[0] + $nVel1_T * $aV_UT[0]
						$aEnemys[$i1][3] = $nVel2_N * $aV_UN[1] + $nVel1_T * $aV_UT[1]
						$aEnemys[$i2][2] = $nVel1_N * $aV_UN[0] + $nVel2_T * $aV_UT[0]
						$aEnemys[$i2][3] = $nVel1_N * $aV_UN[1] + $nVel2_T * $aV_UT[1]
				EndSwitch
			Next
		EndIf
	Next

	For $i = 1 To $aProjectiles[0][0]
		If $i > $aProjectiles[0][0] Then ExitLoop

		DllStructSetData($tPoint, "X", $aProjectiles[$i][0])
		DllStructSetData($tPoint, "Y", $aProjectiles[$i][1])

		If _WinAPI_PtInRect($tRectBorder, $tPoint) Then

			$aProjectiles[$i][0] += $aProjectiles[$i][2] / $nFPS_Cur
			$aProjectiles[$i][1] += $aProjectiles[$i][3] / $nFPS_Cur

			_GDIPlus_GraphicsFillEllipse($hGraphic, $aProjectiles[$i][0] - (4*$nScaleFactor), $aProjectiles[$i][1] - (4*$nScaleFactor), 8*$nScaleFactor, 8*$nScaleFactor, $hBrush_ProjectileBG)

			Switch $aProjectiles[$i][5]
				Case 0
					_GDIPlus_GraphicsFillEllipse($hGraphic, $aProjectiles[$i][0] - (3*$nScaleFactor), $aProjectiles[$i][1] - (3*$nScaleFactor), 6*$nScaleFactor, 6*$nScaleFactor, $hBrush_Enemy)
				Case 1
					_GDIPlus_GraphicsFillEllipse($hGraphic, $aProjectiles[$i][0] - (3*$nScaleFactor), $aProjectiles[$i][1] - (3*$nScaleFactor), 6*$nScaleFactor, 6*$nScaleFactor, $hBrush_Player)
			EndSwitch

			Switch $aProjectiles[$i][5]
				Case 0
					If _CircleIsTouchingCircle($aProjectiles[$i][0], $aProjectiles[$i][1], $nX_PlayerPos, $nY_PlayerPos, 6*$nScaleFactor, 10*$nScaleFactor) Then
						$nLifeForce -= $aProjectiles[$i][4]
						_ArrayDelete($aProjectiles, $i)
						$aProjectiles[0][0] -= 1
						$i -= 1
					EndIf
				Case 1
					For $i2 = 1 To $aEnemys[0][0]
						If _CircleIsTouchingCircle($aProjectiles[$i][0], $aProjectiles[$i][1], $aEnemys[$i2][0], $aEnemys[$i2][1], 6*$nScaleFactor, 10*$nScaleFactor) Then
							$aEnemys[$i2][4] -= $aProjectiles[$i][4]
							_ArrayDelete($aProjectiles, $i)
							$aProjectiles[0][0] -= 1
							$i -= 1
							ExitLoop
						EndIf
					Next
			EndSwitch
		Else
			_ArrayDelete($aProjectiles, $i)
			$aProjectiles[0][0] -= 1
			$i -= 1
		EndIf
	Next

	_GDIPlus_GraphicsDrawRect($hGraphic, 5, 5, 100, 10, $hPen_Bar)
	_GDIPlus_GraphicsDrawRect($hGraphic, 5, 18, 100, 10, $hPen_Bar)
	_GDIPlus_GraphicsFillRect($hGraphic, 5, 5, $nLifeForce * 100 / $nLifeForcePlayer, 10, $hBrush_LifeBar)
	_GDIPlus_GraphicsFillRect($hGraphic, 5, 18, $nSlowMotionFuel * 100 / $nSlowMotionFuelMax, 10, $hBrush_SlowMotionBar)

	_GDIPlus_GraphicsDrawEllipse($hGraphic, $aCursorInfo[0] - 8, $aCursorInfo[1] - 8, 16, 16, $hPen_Cursor)
	_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0], $aCursorInfo[1] - 11, $aCursorInfo[0], $aCursorInfo[1] - 4, $hPen_Cursor)
	_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0] - 11, $aCursorInfo[1], $aCursorInfo[0] - 4, $aCursorInfo[1], $hPen_Cursor)
	_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0], $aCursorInfo[1] + 11, $aCursorInfo[0], $aCursorInfo[1] + 4, $hPen_Cursor)
	_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0] + 11, $aCursorInfo[1], $aCursorInfo[0] + 4, $aCursorInfo[1], $hPen_Cursor)
	_GDIPlus_GraphicsDrawStringEx($hGraphic, "FPS " & Int($nFPS_Display), $hFont_FPS, $tLayout_FPS, $hStringFormat, $hBrush_FPS)

	_WinAPI_BitBlt($hDC, 0, 0, $iGUIWidth, $iGUIHeight, $hCDC_Buffer, 0, 0, 0x00CC0020)

	Switch True
		Case _IsPressed("50", $vUser32Dll) Or Not WinActive($hWnd)
			Dim $aTimerOffset[4 + $aEnemys[0][0]]

			$aTimerOffset[0] = TimerDiff($nT_Sleep)
			$aTimerOffset[1] = TimerDiff($nT_UpdateFPS)
			$aTimerOffset[2] = TimerDiff($nT_AdjustSleepTime)
			$aTimerOffset[3] = TimerDiff($nT_CoolDown)
			For $i = 1 To $aEnemys[0][0]
				$aTimerOffset[$i + 3] = TimerDiff($aEnemys[$i][5])
			Next

			_Pause()

			$nT_Sleep = TimerInit() - $aTimerOffset[0]
			$nT_UpdateFPS = TimerInit() - $aTimerOffset[1]
			$nT_AdjustSleepTime = TimerInit() - $aTimerOffset[2]
			$nT_CoolDown = TimerInit() - $aTimerOffset[3]
			For $i = 1 To $aEnemys[0][0]
				$aEnemys[$i][5] = TimerInit() - $aTimerOffset[$i + 3]
			Next
	EndSwitch
WEnd

Func _Pause()
	If $bTrapMouse Then _OnMouseUnRegister()

	$hBitmapBG = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bitmap)
	$hGraphicTmp = _GDIPlus_ImageGetGraphicsContext($hBitmapBG)
	_GDIPlus_GraphicsFillRect($hGraphicTmp, 0, 0, $iGUIWidth, $iGUIHeight, $hBrush_BG_Menu)
	_GDIPlus_GraphicsDispose($hGraphicTmp)

	Do
		Sleep(20)
	Until Not _IsPressed("50", $vUser32Dll)

	$nT_Sleep = TimerInit() + $nSleepTime
	$nT_UpdateFPS = TimerInit()
	$nT_AdjustSleepTime = TimerInit()

	While Not _IsPressed("50", $vUser32Dll) Or Not WinActive($hWnd)
		DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep) + $nOffsetSleepTime))
		DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
		$nFrameTime = TimerDiff($nT_Sleep)
		$nT_Sleep = TimerInit()

		$nFPS_Cur = 1000 / $nFrameTime
		If TimerDiff($nT_UpdateFPS) >= 500 Then
			$nFPS_Average = ($nFPS_Average * ($iFPS_Counter - 1) + $nFPS_Cur) / $iFPS_Counter
			$iFPS_Counter += 1
			$nFPS_Display = $nFPS_Cur
			$nT_UpdateFPS = TimerInit()
		EndIf
		If TimerDiff($nT_AdjustSleepTime) >= 5000 Then
			If $nFPS_Average - $nFPS < -2 Then $nOffsetSleepTime -= 0.5
			If $nFPS_Average - $nFPS > 2 Then $nOffsetSleepTime += 0.5
			$iFPS_Counter = 1
			$nT_AdjustSleepTime = TimerInit()
		EndIf

		Global $aCursorInfo = GUIGetCursorInfo($hWnd)

		_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmapBG, 0, 0, $iGUIWidth, $iGUIHeight)
		Switch $bNewGame
			Case True
				If _DrawMenuItem($tLayout_Menu_Start,"Start") Then ExitLoop
			Case False
				If _DrawMenuItem($tLayout_Menu_Continue,"Continue") Then ExitLoop
		EndSwitch
		If _DrawMenuItem($tLayout_Menu_Options,"Options",True) Then _Options()
		If _DrawMenuItem($tLayout_Menu_Quit,"Quit") Then _Close()

		_GDIPlus_GraphicsDrawEllipse($hGraphic, $aCursorInfo[0] - 8, $aCursorInfo[1] - 8, 16, 16, $hPen_Cursor)
		_GDIPlus_GraphicsDrawStringEx($hGraphic, "FPS " & Int($nFPS_Display), $hFont_FPS, $tLayout_FPS, $hStringFormat, $hBrush_FPS)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0], $aCursorInfo[1] - 11, $aCursorInfo[0], $aCursorInfo[1] - 4, $hPen_Cursor)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0] - 11, $aCursorInfo[1], $aCursorInfo[0] - 4, $aCursorInfo[1], $hPen_Cursor)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0], $aCursorInfo[1] + 11, $aCursorInfo[0], $aCursorInfo[1] + 4, $hPen_Cursor)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0] + 11, $aCursorInfo[1], $aCursorInfo[0] + 4, $aCursorInfo[1], $hPen_Cursor)
		_WinAPI_BitBlt($hDC, 0, 0, $iGUIWidth, $iGUIHeight, $hCDC_Buffer, 0, 0, 0x00CC0020)
	WEnd

	If $bNewGame Then
		$nT_WaveIdle = TimerInit()
		$bNewWave = True
	EndIf

	Do
		Sleep(20)
	Until Not _IsPressed("50", $vUser32Dll) And Not _IsPressed("01", $vUser32Dll)

	If $bTrapMouse Then _OnMouseRegister()
EndFunc   ;==>_Pause

; eingefügt ab hier
Func _Options()
	$hBitmapBG = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bitmap)
	$hGraphicTmp = _GDIPlus_ImageGetGraphicsContext($hBitmapBG)
	_GDIPlus_GraphicsFillRect($hGraphicTmp, 0, 0, $iGUIWidth, $iGUIHeight, $hBrush_BG_Menu)
	_GDIPlus_GraphicsDispose($hGraphicTmp)

	$nT_Sleep = TimerInit() + $nSleepTime
	$nT_UpdateFPS = TimerInit()
	$nT_AdjustSleepTime = TimerInit()

	While Not _IsPressed("50", $vUser32Dll) Or Not WinActive($hWnd) ; mit [P] würde man wieder zurück kommen
		DllStructSetData($tPrecSleep, "time", -10000 * ($nSleepTime - TimerDiff($nT_Sleep) + $nOffsetSleepTime))
		DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
		$nFrameTime = TimerDiff($nT_Sleep)
		$nT_Sleep = TimerInit()

		$nFPS_Cur = 1000 / $nFrameTime
		If TimerDiff($nT_UpdateFPS) >= 500 Then
			$nFPS_Average = ($nFPS_Average * ($iFPS_Counter - 1) + $nFPS_Cur) / $iFPS_Counter
			$iFPS_Counter += 1
			$nFPS_Display = $nFPS_Cur
			$nT_UpdateFPS = TimerInit()
		EndIf
		If TimerDiff($nT_AdjustSleepTime) >= 5000 Then
			If $nFPS_Average - $nFPS < -2 Then $nOffsetSleepTime -= 0.5
			If $nFPS_Average - $nFPS > 2 Then $nOffsetSleepTime += 0.5
			$iFPS_Counter = 1
			$nT_AdjustSleepTime = TimerInit()
		EndIf

		Global $aCursorInfo = GUIGetCursorInfo($hWnd)

		_GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmapBG, 0, 0, $iGUIWidth, $iGUIHeight)
		If _DrawMenuItem($tLayout_MOpt_Resolution,"Change Resolution") Then Sleep(10); Nächster Schritt -> Funktion mit neuem Menü, in dem man auswählen kann (kann ich wg. mangelnder GDI+-Kenntnisse leider nicht umsetzen)
		If _DrawMenuItem($tLayout_MOpt_Control,"Set Control") Then Sleep(10); Hier um die Steuerung zu verändern -> $sControl_Pause = "50" usw sollte dann so vorhanden sein, dass mans ändern kann, die änderungen am Besten in ner "Arena Fight.ini" verewigen
																	; kann ich machen, falls du mir im Vorraus das GDI+ Zeug scriptest
		If _DrawMenuItem($tLayout_MOpt_Return,"Return") Then ExitLoop

		_GDIPlus_GraphicsDrawEllipse($hGraphic, $aCursorInfo[0] - 8, $aCursorInfo[1] - 8, 16, 16, $hPen_Cursor)
		_GDIPlus_GraphicsDrawStringEx($hGraphic, "FPS " & Int($nFPS_Display), $hFont_FPS, $tLayout_FPS, $hStringFormat, $hBrush_FPS)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0], $aCursorInfo[1] - 11, $aCursorInfo[0], $aCursorInfo[1] - 4, $hPen_Cursor)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0] - 11, $aCursorInfo[1], $aCursorInfo[0] - 4, $aCursorInfo[1], $hPen_Cursor)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0], $aCursorInfo[1] + 11, $aCursorInfo[0], $aCursorInfo[1] + 4, $hPen_Cursor)
		_GDIPlus_GraphicsDrawLine($hGraphic, $aCursorInfo[0] + 11, $aCursorInfo[1], $aCursorInfo[0] + 4, $aCursorInfo[1], $hPen_Cursor)
		_WinAPI_BitBlt($hDC, 0, 0, $iGUIWidth, $iGUIHeight, $hCDC_Buffer, 0, 0, 0x00CC0020)
	WEnd
	; Das Zeug ovn _Pause() wiederherstellen: (bin nicht sicher, ob das so passt)
	$hBitmapBG = _GDIPlus_BitmapCreateFromHBITMAP($hGDI_Bitmap)
	$hGraphicTmp = _GDIPlus_ImageGetGraphicsContext($hBitmapBG)
	_GDIPlus_GraphicsFillRect($hGraphicTmp, 0, 0, $iGUIWidth, $iGUIHeight, $hBrush_BG_Menu)
	_GDIPlus_GraphicsDispose($hGraphicTmp)

	Do
		Sleep(20)
	Until Not _IsPressed("50", $vUser32Dll)
EndFunc   ;==>_Options
; eingefügt bis hier

Func _Restart()
	$iEnemys = Ceiling($iWaveCounter / 5)
	If $iEnemys > 8 Then $iEnemys = 8

	$nProjectileSpeedEnemy = 200 + $iWaveCounter * 5
	$nEnemySpeed = Random($iWaveCounter * 10 + 50, $iWaveCounter * 10 + 100, 1)
	If $nEnemySpeed > 500 Then $nEnemySpeed = Random(400, 500, 1)

	Dim $aEnemys[$iEnemys + 1][7] = [[$iEnemys]]
	For $i = 1 To $aEnemys[0][0]
		$aEnemys[$i][0] = Random(10*$nScaleFactor, $iGUIWidth - (10*$nScaleFactor))
		$aEnemys[$i][1] = Random(10*$nScaleFactor, $iGUIHeight - (10*$nScaleFactor))
		$aNormTmp = _Vector_Normalize(_Vector_Create(Random(-1, 1), Random(-1, 1)))
		$aEnemys[$i][2] = $aNormTmp[0] * $nEnemySpeed
		$aEnemys[$i][3] = $aNormTmp[1] * $nEnemySpeed
		$aEnemys[$i][4] = 5 + $iWaveCounter * 5
		$aEnemys[$i][5] = TimerInit()
		$aEnemys[$i][6] = Random(800 - $iWaveCounter * 5, 1000 - $iWaveCounter * 5, 1)
		If $aEnemys[$i][6] < 200 Then $aEnemys[$i][6] = Random(200, 250, 1)
	Next
EndFunc   ;==>_Restart

Func _Close()
	_WinAPI_ReleaseDC($hWnd, $hDC)
	_WinAPI_DeleteDC($hCDC_Buffer)
	_WinAPI_DeleteObject($hGDI_Bitmap)

	_GDIPlus_GraphicsDispose($hGraphic)
	_GDIPlus_BitmapDispose($hBitmapBG)
	_GDIPlus_BrushDispose($hBrush_Player)
	_GDIPlus_BrushDispose($hBrush_Enemy)
	_GDIPlus_BrushDispose($hBrush_BG_Menu)
	_GDIPlus_BrushDispose($hBrush_FPS)
	_GDIPlus_BrushDispose($hBrush_ProjectileBG)
	_GDIPlus_BrushDispose($hBrush_MenuTextNormal)
	_GDIPlus_BrushDispose($hBrush_MenuTextHighlight)
	_GDIPlus_BrushDispose($hBrush_LifeBar)
	_GDIPlus_BrushDispose($hBrush_SlowMotionBar)
	_GDIPlus_BrushDispose($hBrush_Announcement)
	_GDIPlus_PenDispose($hPen_Bar)
	_GDIPlus_PenDispose($hPen_Cursor)
	_GDIPlus_StringFormatDispose($hStringFormat)
	_GDIPlus_FontFamilyDispose($hFamily_SegoeUI)
	_GDIPlus_FontDispose($hFont_FPS)
	_GDIPlus_FontDispose($hFont_Menu)
	_GDIPlus_Shutdown()

	DllClose($vUser32Dll)
	DllClose($vNTdll)

	If $bTrapMouse Then _OnMouseUnRegister()

	Exit
EndFunc   ;==>_Close

Func _PointIsInCircle($iX_Point, $iY_Point, $iX_Circle, $iY_Circle, $iRadius_Circle)
	$iDistPoints = _GetPointsDistance($iX_Point, $iY_Point, $iX_Circle, $iY_Circle)
	If ($iRadius_Circle > 0 And $iDistPoints < $iRadius_Circle) Or ($iRadius_Circle < 0 And $iDistPoints > $iRadius_Circle) Or $iDistPoints = 0 Then Return 1
	Return 0
EndFunc   ;==>_PointIsInCircle

Func _CircleIsTouchingCircle($iX_Circle1, $iY_Circle1, $iX_Circle2, $iY_Circle2, $iRadius_Circle1, $iRadius_Circle2)
	$iDistCircles = _GetPointsDistance($iX_Circle1, $iY_Circle1, $iX_Circle2, $iY_Circle2)
	If $iDistCircles < $iRadius_Circle1 + $iRadius_Circle2 Then Return True
	Return False
EndFunc   ;==>_CircleIsTouchingCircle

Func _PointIsInRect($iX_Point, $iY_Point, $iX_Rect, $iY_Rect, $iWidth_Rect, $iHeight_Rect)
	If $iX_Point >= $iX_Rect And $iX_Point <= $iX_Rect + $iWidth_Rect And $iY_Point >= $iY_Rect And $iY_Point <= $iY_Rect + $iHeight_Rect Then Return 1
	Return 0
EndFunc   ;==>_PointIsInRect

Func _GetPointsDistance($iPointX1, $iPointY1, $iPointX2, $iPointY2)
	Return Sqrt(($iPointX1 - $iPointX2) ^ 2 + ($iPointY1 - $iPointY2) ^ 2)
EndFunc   ;==>_GetPointsDistance

;eingefügt ab hier
Func _CreateMenuItem($sText,$iHeight)
	$aMeasure = _GDIPlus_GraphicsMeasureString($hGraphic, $sText, $hFont_Menu, _GDIPlus_RectFCreate(0, $iHeight), $hStringFormat)
	$tMenuItem = $aMeasure[0]
	$aMeasure = ""
	DllStructSetData($tMenuItem, "X", $iGUIWidth / 2 - DllStructGetData($tMenuItem, "Width") / 2)
	DllStructSetData($tMenuItem, "Width", DllStructGetData($tMenuItem, "Width") + 1)
	Return $tMenuItem
EndFunc   ;==>_CreateMenuItem
Func _DrawMenuItem(ByRef $tMenuItem,$sText,$bGreyedOut=False)
	If $bGreyedOut Then
		_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont_Menu, $tMenuItem, $hStringFormat, $hBrush_MenuTextGreyedOut)
		Return False
	EndIf
	Switch _PointIsInRect($aCursorInfo[0], $aCursorInfo[1], DllStructGetData($tMenuItem, "X"), DllStructGetData($tMenuItem, "Y"), DllStructGetData($tMenuItem, "Width"), DllStructGetData($tMenuItem, "Height"))
		Case True
			_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont_Menu, $tMenuItem, $hStringFormat, $hBrush_MenuTextHighlight)
			If $aCursorInfo[2] Then Return True ; geklickt
		Case False
			_GDIPlus_GraphicsDrawStringEx($hGraphic, $sText, $hFont_Menu, $tMenuItem, $hStringFormat, $hBrush_MenuTextNormal)
	EndSwitch
	Return False
EndFunc   ;==>_DrawMenuItem

Func _MouseMove()
	If Not WinActive($hWnd) Or $bPaused Then Return 0

	_MouseSetOnEvent($MOUSE_MOVE_EVENT,"")
	$bMoved = False
	$aMouse = MouseGetPos()
	$aWinPos= WinGetPos($hWnd)
	If $aMouse[0] < $aWinPos[0] Then
		MouseMove($aWinPos[0],$aMouse[1],0) ; Links außen
		$bMoved = True
	EndIf
	If $aMouse[0] > ($aWinPos[0]+$aWinPos[2]) Then
		MouseMove($aWinPos[0]+$aWinPos[2],$aMouse[1],0) ; Rechts außen
		$bMoved = True
	EndIf
	If $aMouse[1] < $aWinPos[1] Then
		MouseMove($aMouse[0],$aWinPos[1],0) ; Oben
		$bMoved = True
	EndIf
	If $aMouse[1] > ($aWinPos[1]+$aWinPos[3]) Then
		MouseMove($aMouse[0],$aWinPos[1]+$aWinPos[3],0) ; Unten
		$bMoved = True
	EndIf

	_MouseSetOnEvent($MOUSE_MOVE_EVENT,"_MouseMove","","",0,-1)

	If $bMoved Then Return 1
	Return 0
EndFunc   ;==>_MouseMove
Func _MouseClick()
	If Not WinActive($hWnd) Then Return 0

	$aMouse = MouseGetPos()
	$aWinPos= WinGetPos($hWnd)
	If $aMouse[0] < $aWinPos[0] Or $aMouse[0] > ($aWinPos[0]+$aWinPos[2]-5) Or $aMouse[1] < $aWinPos[1] Or $aMouse[1] > ($aWinPos[1]+$aWinPos[3]-5) Then Return 1 ; Klick blockieren
	Return 0 ; Klick zulassen
EndFunc   ;==>_MouseClick
Func _OnMouseRegister()
	_MouseSetOnEvent($MOUSE_MOVE_EVENT,"_MouseMove","","",0,-1) ; Die Maus fangen
	_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT,"_MouseClick","","",0,-1) ; Klicks nur im Fenster zulassen
	_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT,"_MouseClick","","",0,-1)
	_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT,"_MouseClick","","",0,-1)
	_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT,"_MouseClick","","",0,-1)
EndFunc   ;==>_OnMouseRegister
Func _OnMouseUnRegister()
	_MouseSetOnEvent($MOUSE_MOVE_EVENT,"")
	_MouseSetOnEvent($MOUSE_PRIMARYDOWN_EVENT,"")
	_MouseSetOnEvent($MOUSE_PRIMARYUP_EVENT,"")
	_MouseSetOnEvent($MOUSE_SECONDARYDOWN_EVENT,"")
	_MouseSetOnEvent($MOUSE_SECONDARYUP_EVENT,"")
EndFunc   ;==>_OnMouseUnRegister
;eingefügt bis hier

; - Copyright: name22, Samstag 9. April 2011