This repository has been archived by the owner on Feb 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.lua
141 lines (114 loc) · 3.46 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require 'input'
require 'entity'
require 'scoreboard'
Player = {}
Player.__index = Player
setmetatable(Player, {__index = Entity})
function Player:new(game, config)
local config = config or {}
local newPlayer = Entity:new(game)
newPlayer.type = "player"
newPlayer.playerNumber = config.playerNumber or 1
newPlayer.x = config.x or 400
newPlayer.y = config.y or 300
newPlayer.size = config.size or {
x = 98,
y = 60
}
newPlayer.scoreboard = Scoreboard:new(newPlayer.playerNumber)
newPlayer.speed = config.speed or 5
newPlayer.keys = config.keys or {
up = "up",
down = "down",
left = "left",
right = "right"
}
newPlayer.graphics = config.graphics or {
source = "assets/images/nyancat-sprites-playerI.png",
facing = "right"
}
newPlayer.sound = config.sound or {
moving = {
source = "assets/sounds/move.wav"
}
}
newPlayer.sound.collect = {
source = "assets/sounds/blip.ogg"
}
newPlayer.lastPosition = {
x = nil,
y = nil
}
if game.audio ~= nil then
newPlayer.sound.moving.sample = game.audio.newSource(newPlayer.sound.moving.source)
newPlayer.sound.moving.sample:setLooping(true)
newPlayer.sound.collect.sample = game.audio.newSource(newPlayer.sound.collect.source)
newPlayer.sound.collect.sample:setLooping(false)
end
if game.graphics ~= nil and game.animation ~= nil then
newPlayer.graphics.sprites = game.graphics.newImage(newPlayer.graphics.source)
newPlayer.graphics.grid = game.animation.newGrid(
newPlayer.size.x, newPlayer.size.y,
newPlayer.graphics.sprites:getWidth(),
newPlayer.graphics.sprites:getHeight()
)
newPlayer.graphics.animation = game.animation.newAnimation(
newPlayer.graphics.grid("1-6", 1),
0.05
)
end
return setmetatable(newPlayer, self)
end
function Player:collide(other)
self.x = self.lastPosition.x
self.y = self.lastPosition.y
end
function Player:collect(points)
self.scoreboard:update(points)
self.sound.collect.sample:play()
end
function Player:update(dt)
local dx = 0
local dy = 0
if self.game.input.pressed(self.keys.left) then
dx = dx - self.speed
if self.graphics.facing ~= "left" then
self.graphics.animation:flipH()
self.graphics.facing = "left"
end
end
if self.game.input.pressed(self.keys.right) then
dx = dx + self.speed
if self.graphics.facing ~= "right" then
self.graphics.animation:flipH()
self.graphics.facing = "right"
end
end
if self.game.input.pressed(self.keys.up) then
dy = dy - self.speed
end
if self.game.input.pressed(self.keys.down) then
dy = dy + self.speed
end
self.lastPosition = {
x = self.x,
y = self.y
}
self.x = self.x + dx
self.y = self.y + dy
if self.graphics.animation ~= nil then
if dx ~= 0 or dy ~= 0 then
self.graphics.animation:update(dt)
else
self.graphics.animation:gotoFrame(1)
end
end
-- -- This fails in 2 player.
-- if self.sound.moving.sample ~= nil then
-- if dx ~= 0 or dy ~= 0 then
-- self.sound.moving.sample:play()
-- else
-- self.sound.moving.sample:stop()
-- end
-- end
end