-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
358 lines (298 loc) · 8.72 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
--require 'main'
--require 'TESound.TEsound'
SCREEN_X = 2560
SCREEN_Y = 1600
Player = Class{
function(self, num)
if num == 1 then
self.pnum = 1
self.keyup = "w"
self.keyright = "d"
self.keyleft = "a"
self.keydown = "s"
self.keyfire = "f"
self.keyroll = "g"
self.image = love.graphics.newImage('art/WomanSprite.png')
if ARCADE then
self.keyup = "up"
self.keyleft = "left"
self.keyright = "right"
self.keydown = "down"
self.keyfire = "z"
self.keyroll = "x"
end
elseif num == 2 then
self.pnum = 2
self.keyup = "o"
self.keyright = ";"
self.keyleft = "k"
self.keydown = "l"
self.keyfire = "j"
self.keyroll = "h"
self.image = love.graphics.newImage('art/ManSprite.png')
if ARCADE then
self.keyup = "r"
self.keyleft = "d"
self.keyright = "g"
self.keydown = "f"
self.keyfire = "a"
self.keyroll = "s"
end
end
self:init()
-- Set up for Timers
self.timer = Timer.new()
-- Set up anim8 for spritebatch animations -----------------------------
self.frameDelay = 0.3
self.frameFlipH = nop
self.frameFlipV = nop
self.grid = Anim8.newGrid(128, 128,
self.image:getWidth(),
self.image:getHeight())
self.standAnim = Anim8.newAnimation(
self.grid(1,1),
self.frameDelay)
self.runAnim = Anim8.newAnimation(
self.grid('2-3', 1),
self.frameDelay)
--ready for shooting animation here:
self.shootingAnim = Anim8.newAnimation(
self.grid('1-2', 2),
self.frameDelay)
self.hurtAnim = Anim8.newAnimation(
self.grid('1-2', 3),
self.frameDelay)
self.animation = self.standAnim
-- sound stuffs go here
self.gunsoundlist = { "sfx/gunshot1.ogg", "sfx/gunshot2.ogg"}
--, "sfx/gunshot3.ogg" }
-- particle sys stuff go here now!
gunParticleImage = love.graphics.newImage( "art/gunParticle.png" )
self.gunEmitter = love.graphics.newParticleSystem( gunParticleImage, 200 )
self.gunEmitter:setEmissionRate(800)
self.gunEmitter:setEmitterLifetime(0.02)
self.gunEmitter:setParticleLifetime(0.075)
self.gunEmitter:setSpread(3.14/4)
self.gunEmitter:setSizes(0.05, 0.25)
self.gunEmitter:setLinearAcceleration(0,9.8)
self.gunEmitter:setSpeed(300,500)
-- particle sys stuff go here now!
bloodParticleImage = love.graphics.newImage( "art/bloodParticle.png" )
self.bloodEmitter = love.graphics.newParticleSystem( bloodParticleImage, 500 )
self.bloodEmitter:setEmissionRate(500)
self.bloodEmitter:setEmitterLifetime(0.02)
self.bloodEmitter:setParticleLifetime(0.35)
self.bloodEmitter:setSpread(3.14/3)
self.bloodEmitter:setSizes(0.1, 0.5)
self.bloodEmitter:setLinearAcceleration(0,100)
self.bloodEmitter:setSpeed(200,300)
self.bloodEmitter:stop()
end
}
function Player:init()
self.health = 10
self.isplaying = false
self.scale = 0.5
self.width = 64 -- size we will draw each frame at
self.height = 64 -- size we will draw each frame at
self.facing = Vector(1,0)
self.fired = false -- keeps track of shooting state
self.isalive = true -- keeps track of aliveness. duh.
self.ishurt = false
if self.pnum == 1 then
self.position = Vector(4900, 900)
elseif self.pnum == 2 then
self.position = Vector(4200, 940)
end
-- love.physics code starts here -----------------------------------------
self.facing = Vector(1,0) -- normalized direction vector
self.acceleration = 12000 * 120
self.damping = 15
self.density = 2
self.body = love.physics.newBody(world,
((self.position.x + self.width) / 2 ),
((self.position.y + self.height) / 2 ),
--self.position.x,
--self.position.y,
"dynamic"
)
self.body:setFixedRotation(true)
self.shape = love.physics.newRectangleShape(
self.width/3,
self.height
)
self.fixture = love.physics.newFixture(
self.body,
self.shape,
self.density)
-- awkward but absolutely needed to pull out the
-- object that owns the fixture during collision
-- detection:
self.fixture:setUserData(self)
self.fixture:setCategory(PLAYER)
self.body:setLinearDamping( self.damping )
end
function Player:setPosition(v)
self.body:setX(v.x)
self.body:setY(v.y)
self.position.x = v.x
self.position.y = v.y
end
function Player:update(dt)
-- animation stuff
self.animation:update(dt)
-- sound stuff
TEsound.cleanup()
-- Inc timers
self.timer:update(dt)
-- particle stuffs
self.gunEmitter:update(dt)
self.bloodEmitter:update(dt)
-- delta holds direction of movement input
local delta = Vector(0,0)
local moved = false
if (not self.fired) and (not self.ishurt) and (self.isalive) and (not isGameOver) then
if love.keyboard.isDown(self.keyleft) then
delta.x = -1
self.body:applyForce(-self.acceleration * dt,0)
moved = true
--self.frameFlipH = -1
self.animation:flipH()
elseif love.keyboard.isDown(self.keyright) then
delta.x = 1
self.body:applyForce(self.acceleration * dt,0)
moved = true
--self.frameFlipH = 1
self.animation:flipH()
end
if love.keyboard.isDown(self.keyup) then
delta.y = -1
self.body:applyForce(0,-self.acceleration * dt)
moved = true
elseif love.keyboard.isDown(self.keydown) then
delta.y = 1
self.body:applyForce(0,self.acceleration * dt)
moved = true
end
end
-- Want length 1 vector with correct x, y elements
delta:normalize_inplace()
-- Handle the animation switching here as needed:
if self.ishurt or (not self.isalive) then
self.animation = self.hurtAnim
elseif (not self.fired) then
if moved then
if delta.x ~= 0 then
self.facing.x = delta.x
self.facing.y = 0
end
self.animation = self.runAnim
else
self.animation = self.standAnim
end
end
self.position.x, self.position.y =
self.body:getX() - self.width / 2,
self.body:getY() - self.height / 2
end
function Player:draw()
--love.graphics.polygon("fill", self.body:getWorldPoints(self.shape:getPoints()))
self.animation:draw(self.image,
self.position.x,
self.position.y,
0, -- angle
self.scale, -- x scale
self.scale, -- y scale
0, -- x offset
0 -- y offset
--self.frameFlipH,
--self.frameFlipV
)
love.graphics.draw(self.gunEmitter)
love.graphics.draw(self.bloodEmitter)
end
function Player:getCenter()
return self.body:getX(), self.body:getY()
end
function Player:keyPressHandler(key)
end
function Player:fire()
-- do the animation
self.fired = true
self.animation = self.shootingAnim
self.animation:gotoFrame(1)
self.timer:add(0.5, function()
self:stopFire()
self.shootingAnim:gotoFrame(1)
end)
-- figure out origin to fire from
-- and direction to fire in
local pos = Vector(0,0)
local aiming = self.facing
aiming.y = 0
aiming:normalize_inplace()
pos.x, pos.y = self.body:getWorldCenter()
pos = pos + aiming * 20
local rads
if aiming.x < 0 then
rads = 3.14
else
rads = 0
end
-- set up the flashy sparkly guy
self.gunEmitter:reset()
self.gunEmitter:setPosition(pos.x, pos.y - 10)
self.gunEmitter:setDirection( rads )
self.timer:add(0.25, function()
self.gunEmitter:start()
table.insert(bullets,Bullet(null, pos, aiming))
local vol = math.random(30, 50) / 100
local pitch = math.random(50, 125) / 100
TEsound.play(self.gunsoundlist, "gunshot", vol, pitch)
end)
end
function Player:stopFire()
self.fired = false
self.animation = self.standAnim
self.gunEmitter:stop()
end
function Player:keyReleaseHandler(key)
if key == self.keyfire then
if (not self.isHurt) and (not self.fired) then
self:fire()
self.animation = self.shootingAnim
end
elseif key == self.keyroll then
-- animate roll here
end
end
-- Resolve being shot here.
-- called by world collision callback in main.lua
function Player:isShot(bullet, collision)
local pos = Vector(self.position.x + 10, self.position.y + 20)
-- set up the bloody splurty guy
self.bloodEmitter:setDirection(0)
if math.random(1,2) == 1 then
self.bloodEmitter:setDirection(3.14)
end
--self.bloodEmitter:reset()
self.bloodEmitter:setPosition(pos.x + 32, pos.y + 32)
self.bloodEmitter:start()
self.health = self.health - 1
self.ishurt = true
self.animation = self.hurtAnim
if self.health < 1 and (not isGameOver) then
self:dies()
else
self.timer:add(0.5, function()
self.ishurt = false
self.animation = self.standAnim
self.bloodEmitter:stop()
end)
end
end
function Player:dies()
self.isalive = false
self.timer:clear()
StartGameOver(self)
end