; Author ........: Detefon ; Date ..........: 09/12/2014 #include-once #include #include #include #include #include ;#include ; ;~ https://github.com/ez2sugul/stopwatch_client/blob/master/JSMN.au3 Opt("GUIOnEventMode", 1) Opt("GUIEventOptions", 1) Opt("MustDeclareVars", 1) OnAutoItExitRegister("on_exit") Global $hDLL = DllOpen("User32.dll") ; direções da roda dos ventos ; wind rose's direction Global $ww, $ee, $nn, $ss, $nw, $ne, $sw, $se ; diração do ator ; directon's actor Global $iDir = 0, $iWalk = 0, $iMov = $iDir Global $ii, $jj ;Global $JSON = load_json("map.json") Global Const $ROOT = Round(Sqrt(2) / 2, 3) Global $hGui Global $hGraphic, $hPen, $hBitmap, $hBackbuffer Global Const $SD = "Scripting.Dictionary" Global $oWall = ObjCreate($SD) Global $oActor = ObjCreate($SD) Global $img ; $aMap[0] linhas / lines ; $aMap[1] colunas / columns Global $aMap[2] = [10, 8] ; tamanho da célula (o ator e cada bloco do muro possuem este tamanho) ; size's cell (the actor and each wall block have this size) Global $iCell = 32 ; limite do gráfico ; graphics limit Global $aLim[4] $aLim[0] = 0 $aLim[1] = 0 $aLim[2] = $aMap[0] * $iCell - $iCell $aLim[3] = $aMap[1] * $iCell - $iCell Global $aSize[2] = [$aMap[0] * $iCell, $aMap[1] * $iCell] Global $aGrid[$aSize[0]][$aSize[1]] add_actor(2, 96, $iCell, $iCell, 3, 0xFF0000FF) Func add_actor($xx, $yy, $width, $height, $speed, $iColor) $oActor.Add($oActor.Count + 1, ObjCreate($SD)) $oActor.Item($oActor.Count).Add("x", $xx) $oActor.Item($oActor.Count).Add("y", $yy) $oActor.Item($oActor.Count).Add("w", $width) $oActor.Item($oActor.Count).Add("h", $height) $oActor.Item($oActor.Count).Add("st", $speed) $oActor.Item($oActor.Count).Add("sx", $oActor.Item($oActor.Count).Item("st") * $ROOT) $oActor.Item($oActor.Count).Add("c", $iColor) EndFunc ;==>add_actor dump($oActor) Global $aPress[6] = [False, False, False, False, False, False] Global $aKeys[6] = [11, 20, 25, 26, 27, 28] $hGui = GUICreate("Detefon's grid", $aSize[0], $aSize[1]) GUISetOnEvent($GUI_EVENT_CLOSE, "_quit") GUISetState(@SW_SHOWNORMAL) _GDIPlus_Startup() $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui) $hBitmap = _GDIPlus_BitmapCreateFromGraphics($aSize[0], $aSize[1], $hGraphic) $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap) $hPen = _GDIPlus_PenCreate() _GDIPlus_GraphicsClear($hBackbuffer) _GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2) $img = _GDIPlus_ImageLoadFromFile("char2.png") add_wall($iCell, $iCell, $iCell, $iCell, 0xFFFF0000) add_wall($iCell * 2, $iCell, 16, $iCell, 0xFFFF0000) add_wall($iCell * 4, $iCell, $iCell, $iCell, 0xFFFF0000) add_wall($iCell * 6, $iCell, $iCell, $iCell, 0xFFFF0000) add_wall($iCell * 6, $iCell * 2, $iCell, 16, 0xFFFF0000) add_wall($iCell * 6, $iCell * 5, $iCell, $iCell, 0xFFFF0000) add_wall($iCell * 6, $iCell * 4.5, 16, 16, 0xFFFF0000) add_wall($iCell * 6.5, $iCell * 6, 16, 16, 0xFFFF0000) add_wall($iCell * 7, $iCell * 5, 16, 16, 0xFFFF0000) add_wall($iCell * 5.5, $iCell * 5.5, 16, 16, 0xFFFF0000) add_wall(40, 120, 5, 96, 0xFFFF0000) add_wall(90, 120, 5, 96, 0xFFFF0000) add_wall(280, 120, 5, 96, 0xFFFF0000) AdlibRegister("_update", 20) AdlibRegister("walk", 80) While Sleep(20) WEnd Func _update() keyboard() _GDIPlus_GraphicsClear($hBackbuffer, 0x33FFFFFF) draw_wall() move_actors() _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $aSize[0], $aSize[1]) EndFunc ;==>_update Func move_actors() For $each In $oActor move($each) _box($hBackbuffer, $oActor.Item($each).Item("x"), $oActor.Item($each).Item("y"), $iCell, $iCell, $oActor.Item($each).Item("c")) Next Local $aa[9] = [0, 1, 2, 3, 7, 6, 5, 4, 0] If $iDir Then _GDIPlus_GraphicsDrawImageRectRect($hBackbuffer, $img, $iWalk * 28, $aa[$iDir] * 32 + 1, 32, 32, $oActor.Item($each).Item("x"), $oActor.Item($each).Item("y"), 32, 32) Else _GDIPlus_GraphicsDrawImageRectRect($hBackbuffer, $img, $iWalk * 28, $aa[$iMov] * 32 + 1, 32, 32, $oActor.Item($each).Item("x"), $oActor.Item($each).Item("y"), 32, 32) EndIf EndFunc ;==>move_actors Func draw_wall() For $each In $oWall _box($hBackbuffer, _ $oWall.Item($each).Item("x"), _ $oWall.Item($each).Item("y"), _ $oWall.Item($each).Item("w"), _ $oWall.Item($each).Item("h"), _ $oWall.Item($each).Item("c")) Next EndFunc ;==>draw_wall Func add_wall($xx, $yy, $ww, $hh, $iColor) Local $oo = ObjCreate($SD) $oWall.Add($oWall.Count + 1, ObjCreate($SD)) $oWall.Item($oWall.Count).Add("x", $xx) $oWall.Item($oWall.Count).Add("y", $yy) $oWall.Item($oWall.Count).Add("w", $ww) $oWall.Item($oWall.Count).Add("h", $hh) $oWall.Item($oWall.Count).Add("c", $iColor) For $ii = $xx To $xx + $ww - 1 For $jj = $yy To $yy + $hh - 1 $aGrid[$ii][$jj] = $oWall.Count Next Next EndFunc ;==>add_wall Func move($each) ;~ nw n ne ;~ w 0 e ;~ sw s se ;~ 32 3 34 ;~ 2 0 4 ;~ 52 5 54 $ww = $aPress[2] And Not $aPress[4] ? True : False $ee = $aPress[4] And Not $aPress[2] ? True : False $nn = $aPress[3] And Not $aPress[5] ? True : False $ss = $aPress[5] And Not $aPress[3] ? True : False $ne = $nn And $ee ? True : False $se = $ss And $ee ? True : False $sw = $ww And $ss ? True : False $nw = $ww And $nn ? True : False Select Case $nw __move_x($each, 8, "sx") $iDir = 8 Case $ne __move_x($each, 2, "sx") $iDir = 2 Case $se __move_x($each, 4, "sx") $iDir = 4 Case $sw __move_x($each, 6, "sx") $iDir = 6 Case Else Select Case $nn __move_t($each, 1, "st") $iDir = 1 Case $ee __move_t($each, 3, "st") $iDir = 3 Case $ss __move_t($each, 5, "st") $iDir = 5 Case $ww __move_t($each, 7, "st") $iDir = 7 Case Else __move_t($each, 0, 0) $iDir = 0 EndSelect EndSelect EndFunc ;==>move Func walk() If $iDir Then $iWalk -= 1 If $iWalk < 0 Then $iWalk = 8 $iMov = $iDir EndIf EndFunc ;==>walk Func __move_t($each, $dir, $speed = "") If Not $dir Then Return EndIf Switch $dir Case 1 ; $nn If $oActor.Item($each).Item("y") - $oActor.Item($each).Item($speed) < $aLim[1] Then $oActor.Item($each).Item("y") = $aLim[1] Else For $ii = $oActor.Item($each).Item("x") To $oActor.Item($each).Item("x") + $oActor.Item($each).Item("w") - 2 For $jj = $oActor.Item($each).Item("y") To ($oActor.Item($each).Item("y") - $oActor.Item($each).Item($speed) - 1) < 0 ? 0 : ($oActor.Item($each).Item("y") - $oActor.Item($each).Item($speed) - 1) Step -1 If $aGrid[$ii][$jj] Then ExitLoop (2) Next Next If $aGrid[$ii][$jj] Then $oActor.Item($each).Item("y") = $oWall.Item($aGrid[$ii][$jj]).Item("y") + $oWall.Item($aGrid[$ii][$jj]).Item("h") Else $oActor.Item($each).Item("y") -= $oActor.Item($each).Item($speed) EndIf EndIf Case 3 ; $ee If $oActor.Item($each).Item("x") + $oActor.Item($each).Item($speed) > $aLim[2] Then $oActor.Item($each).Item("x") = $aLim[2] Else For $ii = $oActor.Item($each).Item("x") + $oActor.Item($each).Item("w") - 1 To $oActor.Item($each).Item("x") + $oActor.Item($each).Item("w") + $oActor.Item($each).Item($speed) - 2 For $jj = $oActor.Item($each).Item("y") To $oActor.Item($each).Item("y") + $oActor.Item($each).Item("h") - 2 If $aGrid[$ii][$jj] Then ExitLoop (2) Next Next If $aGrid[$ii][$jj] Then $oActor.Item($each).Item("x") = Int($oWall.Item($aGrid[$ii][$jj]).Item("x") - $oActor.Item($each).Item("w")) Else $oActor.Item($each).Item("x") += $oActor.Item($each).Item($speed) EndIf EndIf Case 5 ; $ss If $oActor.Item($each).Item("y") + $oActor.Item($each).Item($speed) > $aLim[3] Then $oActor.Item($each).Item("y") = $aLim[3] Else For $ii = $oActor.Item($each).Item("x") To $oActor.Item($each).Item("x") + $oActor.Item($each).Item("w") - 2 For $jj = $oActor.Item($each).Item("y") + $oActor.Item($each).Item("h") - 1 To $oActor.Item($each).Item("y") + $oActor.Item($each).Item("h") + $oActor.Item($each).Item($speed) - 2 If $aGrid[$ii][$jj] Then ExitLoop (2) Next Next If $aGrid[$ii][$jj] Then $oActor.Item($each).Item("y") = Int($oWall.Item($aGrid[$ii][$jj]).Item("y") - $oActor.Item($each).Item("h")) Else $oActor.Item($each).Item("y") += $oActor.Item($each).Item($speed) EndIf EndIf Case 7 ; $ww If $oActor.Item($each).Item("x") - $oActor.Item($each).Item($speed) < $aLim[0] Then $oActor.Item($each).Item("x") = $aLim[0] Else For $ii = $oActor.Item($each).Item("x") To ($oActor.Item($each).Item("x") - $oActor.Item($each).Item($speed) - 1) < 0 ? 0 : ($oActor.Item($each).Item("x") - $oActor.Item($each).Item($speed) - 1) Step -1 For $jj = $oActor.Item($each).Item("y") To ($oActor.Item($each).Item("y") + $oActor.Item($each).Item("h") - 2) If $aGrid[$ii][$jj] Then ExitLoop (2) Next Next If $aGrid[$ii][$jj] Then $oActor.Item($each).Item("x") = $oWall.Item($aGrid[$ii][$jj]).Item("x") + $oWall.Item($aGrid[$ii][$jj]).Item("w") Else $oActor.Item($each).Item("x") -= $oActor.Item($each).Item($speed) EndIf EndIf EndSwitch EndFunc ;==>__move_t Func __move_x($each, $dir, $speed = 0) Switch $dir Case 2 ; $ne __move_t($each, 1, "sx") __move_t($each, 3, "sx") Case 4 ; $se __move_t($each, 3, "sx") __move_t($each, 5, "sx") Case 6 ; $sw __move_t($each, 5, "sx") __move_t($each, 7, "sx") Case 8; $nw __move_t($each, 1, "sx") __move_t($each, 7, "sx") EndSwitch EndFunc ;==>__move_x Func keyboard() For $ii = 0 To 5 If _IsPressed($aKeys[$ii], $hDLL) And Not $aPress[$ii] Then $aPress[$ii] = True If Not _IsPressed($aKeys[$ii], $hDLL) And $aPress[$ii] Then $aPress[$ii] = False Next EndFunc ;==>keyboard Func _quit() Exit EndFunc ;==>_quit Func on_exit() _GDIPlus_GraphicsDispose($hBackbuffer) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_PenDispose($hPen) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() If $hDLL <> -1 Then DllClose($hDLL) Exit EndFunc ;==>on_exit Func _box($hToGraphic, $iXX, $iYY, $iWW, $iHH, $iColor = 0xFF000000) ; _box2 Local $aBox[5][2] $aBox[0][0] = 4 $aBox[1][0] = $iXX $aBox[1][1] = $iYY $aBox[2][0] = $iXX + $iWW - 1 $aBox[2][1] = $iYY $aBox[3][0] = $iXX + $iWW - 1 $aBox[3][1] = $iYY + $iHH - 1 $aBox[4][0] = $iXX $aBox[4][1] = $iYY + $iHH - 1 If $iColor Then _GDIPlus_PenSetColor($hPen, $iColor) _GDIPlus_GraphicsDrawPolygon($hToGraphic, $aBox, $hPen) Else _GDIPlus_GraphicsDrawPolygon($hToGraphic, $aBox) EndIf EndFunc ;==>_box Func dump($oo, $key = "", $iIdent = 0) Local $iTab = 4 Local $sKeys If IsDictionary($oo) Then $sKeys = $oo.Keys For $each In $sKeys If IsDictionary($oo.Item($each)) Or IsArray($oo.Item($each)) Then ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & $each & "{" & @LF) dump($oo.Item($each), ($key == "" ? "" : $key & ".") & $each, $iTab + $iIdent) ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & "}" & @LF) Else ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & ".") & $each & " = " & $oo.Item($each) & @LF) EndIf Next Else If IsArray($oo) Then Switch UBound($oo, 0) Case 1 For $ii = 0 To UBound($oo, 1) - 1 If IsDictionary($oo[$ii]) Or IsArray($oo[$ii]) Then dump($oo[$ii], $key & "[" & $ii & "]", $iTab + $iIdent) Else ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & "[") & $ii & "] = " & $oo[$ii] & @LF) EndIf Next Case 2 For $ii = 0 To UBound($oo, 1) - 1 For $jj = 0 To UBound($oo, 2) - 1 If IsDictionary($oo[$ii][$jj]) Or IsArray($oo[$ii][$jj]) Then dump($oo[$ii][$jj], $key & "[" & $ii & "][" & $jj & "]", $iTab + $iIdent) Else ConsoleWrite(($iIdent == 0 ? "" : (_StringRepeat(" ", $iIdent))) & ($key == "" ? "" : $key & "[") & $ii & "][" & $jj & "] = " & $oo[$ii][$jj] & @LF) EndIf Next Next EndSwitch Else EndIf EndIf EndFunc ;==>dump Func IsDictionary($oo = Null) If Not IsObj($oo) Or Not (ObjName($oo, 2) == "Scripting.Dictionary") Then Return SetError(1, 0, False) Return True EndFunc ;==>IsDictionary Func die($sInput = "", $sData = "") Local $aText[2] = ["@error[ " & $sInput & " ]", ($sData ? "@msg[ " & $sData & " ]" : "")] If $sInput Then MsgBox(0, "@error", _ArrayToString($aText, @LF)) Exit EndFunc ;==>die Func load_json($sInput = "") Local $hFile Local $sJson If Not $sInput Then die("no file") $hFile = FileOpen($sInput) If Not $hFile Or @error Then die("error open file") $sJson = FileRead($hFile) If Not $sJson Or @error Then die("error read file") ;$sJson = Jsmn_Decode($sJson) If Not IsDictionary($sJson) Then die("error not json") Return $sJson EndFunc ;==>load_json #cs http://lpc.opengameart.org/static/lpc-style-guide/assets.html #ce