1
+ local Directions = {
2
+ LEFT = { x = - 1 , y = 0 , cmp = " _LESS" , use = " x" },
3
+ RIGHT = { x = 1 , y = 0 , cmp = " _MORE" , use = " x" },
4
+ DOWN = { x = 0 , y = 1 , cmp = " _MORE" , use = " y" },
5
+ UP = { x = 0 , y = - 1 , cmp = " _LESS" , use = " y" }
6
+ }
7
+ local CompOps = {
8
+ _LESS = function (a , b ) return a <= b ; end ,
9
+ _MORE = function (a , b ) return a >= b ; end
10
+ }
11
+
12
+ StartWalking = 0 ;
13
+ Walking = 1 ;
14
+ EndWalking = 2 ;
15
+ NotWalking = 3 ;
16
+
17
+ function Local .Init (position )
18
+ Terrain = Scene :getGameObject (" Terrain" );
19
+ Object .pos = { x = position .x , y = position .y };
20
+ Object .direction = " NONE" ;
21
+ Object .walking = NotWalking ;
22
+ Object .sprSize = This :LevelSprite ():getSize ().x ;
23
+ Object .speed = 0.5 ;
24
+ InitializeBindings ();
25
+
26
+ local pVec = obe .UnitVector (
27
+ position .x * This :LevelSprite ():getSize ().x ,
28
+ position .y * This :LevelSprite ():getSize ().y
29
+ );
30
+ This :LevelSprite ():setPosition (pVec );
31
+ end
32
+
33
+ function Object :getType ()
34
+ return This :getType ();
35
+ end
36
+
37
+ function Object :getSprSize ()
38
+ return This :LevelSprite ():getSize ();
39
+ end
40
+
41
+ function InitializeBindings ()
42
+ for k , v in pairs (Directions ) do
43
+ Global .Actions [k ] = function ()
44
+ if Object .walking == NotWalking and not Scene :getGameObject (" Game" ).victory then
45
+ Object .direction = k ;
46
+ Object .walking = StartWalking ;
47
+ This :LevelSprite ():loadTexture (" Sprites/GameObjects/Robot/Robot_" .. k .. " .png" );
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ function Global .Game .Update (dt )
54
+ if Object .walking == StartWalking then
55
+ Object .bound = {
56
+ x = Directions [Object .direction ].x ,
57
+ y = Directions [Object .direction ].y ,
58
+ cmp = Directions [Object .direction ].cmp ,
59
+ use = Directions [Object .direction ].use
60
+ }
61
+ Object .walking = Walking ;
62
+ local xProj = Object .pos .x + Directions [Object .direction ].x ;
63
+ local yProj = Object .pos .y + Directions [Object .direction ].y ;
64
+ local tileOnFuturePosition = Terrain .elements [yProj + 1 ][xProj + 1 ][1 ]:getType ();
65
+ local barrelToMove = Terrain .elements [yProj + 1 ][xProj + 1 ][2 ];
66
+ if tileOnFuturePosition == " Wall" then
67
+ Object .walking = NotWalking ;
68
+ else
69
+ if barrelToMove ~= nil then
70
+ xProj = Object .pos .x + 2 * Directions [Object .direction ].x ;
71
+ yProj = Object .pos .y + 2 * Directions [Object .direction ].y ;
72
+ tileOnFuturePosition = Terrain .elements [yProj + 1 ][xProj + 1 ][1 ]:getType ();
73
+ local barrelOnFuturePosition = Terrain .elements [yProj + 1 ][xProj + 1 ][2 ];
74
+ if tileOnFuturePosition == " Wall" or barrelOnFuturePosition ~= nil then
75
+ Object .walking = NotWalking ;
76
+ else
77
+ barrelToMove :move (Object .direction );
78
+ Terrain .elements [Object .pos .y + 1 ][Object .pos .x + 1 ][2 ] = nil ;
79
+ Object .pos = {
80
+ x = Object .pos .x + Directions [Object .direction ].x ,
81
+ y = Object .pos .y + Directions [Object .direction ].y
82
+ };
83
+ Terrain .elements [Object .pos .y + 1 ][Object .pos .x + 1 ][2 ] = This :access ();
84
+ end
85
+ else
86
+ Terrain .elements [Object .pos .y + 1 ][Object .pos .x + 1 ][2 ] = nil ;
87
+ Object .pos = {
88
+ x = Object .pos .x + Directions [Object .direction ].x ,
89
+ y = Object .pos .y + Directions [Object .direction ].y
90
+ };
91
+ Terrain .elements [Object .pos .y + 1 ][Object .pos .x + 1 ][2 ] = This :access ();
92
+ end
93
+ end
94
+
95
+ elseif Object .walking == Walking then
96
+ local mx , my ;
97
+ if Object .bound .use == " x" then
98
+ mx , my = Object .bound .x * dt * Object .speed , 0 ;
99
+ else
100
+ mx , my = 0 , Object .bound .y * dt * Object .speed ;
101
+ end
102
+ local move = obe .UnitVector (mx , my );
103
+ This :LevelSprite ():move (move );
104
+ local newpos = This :LevelSprite ():getPosition ();
105
+ if CompOps [Object .bound .cmp ](newpos [Object .bound .use ], Object .pos [Object .bound .use ] * Object .sprSize ) then
106
+ Object .walking = NotWalking ;
107
+ end
108
+ --[[ elseif Object.walking == EndWalking then
109
+ if not Hook.InputManager:getAction(Object.direction):check() then
110
+ Object.walking = NotWalking;
111
+ else
112
+ Object.walking = StartWalking;
113
+ end]]
114
+ else
115
+ local spritePos = obe .UnitVector (
116
+ Object .pos .x * This :LevelSprite ():getSize ().x ,
117
+ Object .pos .y * This :LevelSprite ():getSize ().y
118
+ );
119
+ This :LevelSprite ():setPosition (spritePos );
120
+ end
121
+ end
0 commit comments