-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlayer.lua
79 lines (71 loc) · 1.96 KB
/
Player.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Player = MovingTile:extend
{
x = 0,
y = 0,
image = "res/maid.png",
isMoving = false,
targetX = 0,
targetY = 0,
moveX = 0,
moveY = 0,
facing = UP,
changingPositionCounter = 0,
rightRad = math.rad(0),
downRad = math.rad(90),
leftRad = math.rad(180),
upRad = math.rad(270)
}
function Player:onNew()
self.x = 0
self.y = 0
self:resetTargets()
self.moveX = the.app.view.gridSize / 5
self.moveY = the.app.view.gridSize / 5
end
function Player:resetTargets()
self.x = self:roundXToGrid( self.x )
self.y = self:roundYToGrid( self.y )
self.targetX = self.x
self.targetY = self.y
end
function Player:changePosition( dir )
if dir == LEFT then
self.rotation = self.leftRad
elseif dir == RIGHT then
self.rotation = self.rightRad
elseif dir == UP then
self.rotation = self.upRad
else
self.rotation = self.downRad
end
self.isMoving = false
self:resetTargets()
self.changingPositionCounter = the.app.fps / 4
end
function Player:fire()
self.solid = false
self.active = false
end
function Player:kill()
self:fire()
self.imageOffset = { x = 54, y = 0 }
end
function Player:onUpdate( time )
if self.changingPositionCounter > 0 then
self.changingPositionCounter = self.changingPositionCounter - 1
return nil
end
if the.keys:pressed( 'a', 'left' ) then
self:changePosition( LEFT )
self.x = self:roundXToGrid( math.max( 0, self.x - the.app.view.gridSize ) )
elseif the.keys:pressed( 'd', 'right' ) then
self:changePosition( RIGHT )
self.x = self:roundXToGrid( math.min( the.app.width - the.app.view.gridSize, self.x + the.app.view.gridSize ) )
elseif the.keys:pressed( 'w', 'up' ) then
self:changePosition( UP )
self.y = self:roundYToGrid( math.max( 0, self.y - the.app.view.gridSize ) )
elseif the.keys:pressed( 's', 'down' ) then
self:changePosition( DOWN )
self.y = self:roundYToGrid( math.min( the.app.height - the.app.view.gridSize, self.y + the.app.view.gridSize ) )
end
end