summaryrefslogtreecommitdiffhomepage
path: root/player.lua
blob: a0f189ab4c11f072d427ed7d8a817cd69b1afd7e (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
-- `Player`
-- Entity controlled by a player. It has a physical body and a sprite. Can play animations and interact with other instances of the same class.
-- Collision category: [2]

-- WHOLE CODE HAS FLAG OF "need a cleanup"

-- Metatable of `Player`
-- nils initialized in constructor
Player = {
	-- General and physics
	name = "empty",
	body = nil,
	shape = nil,
	fixture = nil,
	sprite = nil,
	rotate = 0, -- "angle" would sound better
	facing = 1,
	max_velocity = 105,
	world = nil, -- game world
	-- Combat
	combo = 1,
	lives = 3,
	spawntimer = 0,
	alive = true,
	punchcd = 0,
	punchinitial = 0.25,
	punchdir = 0, -- a really bad thing
	-- Animation
	animations = require "animations",
	current = nil,
	frame = 1,
	delay = 0.10,
	initial = nil,
	-- Movement
	inAir = true,
	salto = false,
	jumpactive = false,
	jumpdouble = true,
	jumptimer = 0.14,
	-- Keys
	controller = nil,
	controller_empty = {isDown = function () return false end},
	-- HUD
	portrait_sprite = nil,
	portrait_sheet  = require "portraits"
}

-- Constructor of `Player`
function Player:new (game, world, x, y, name)
	-- Meta
	local o = {}
	setmetatable(o, self)
	self.__index = self
	-- Physics
	o.body    = love.physics.newBody(world, x, y, "dynamic")
	o.shape   = love.physics.newRectangleShape(10, 17)
	o.fixture = love.physics.newFixture(o.body, o.shape, 8)
	o.fixture:setUserData(o)
	o.fixture:setCategory(2)
	o.fixture:setMask(2)
	o.body:setFixedRotation(true)
	-- Misc
	o.name   = name or "empty"
	o.sprite = love.graphics.newImage("assets/"..o.name..".png")
	o.world  = game
	-- Animation
	o.initial = o.delay
	o.current = o.animations.idle
	o:createEffect("respawn")
	-- Portrait load for first object created
	if self.portrait_sprite == nil then
		self.portrait_sprite = love.graphics.newImage("assets/portraits.png")
	end
	return o
end

-- Destructor of `Player`
function Player:delete()
	-- body deletion is handled by world deletion
	self.sprite = nil
end

function Player:assignController(controller)
	self.controller = controller or nil
	controller:setParent(self)
end

function Player:getController()
	if self.controller ~= nil then
		return self.controller
	else
		return self.controller_empty
	end
end

-- Update callback of `Player`
function Player:update (dt)
	-- # VERTICAL MOVEMENT
	-- Jumping
	if self.jumpactive and self.jumptimer > 0 then
		local x,y = self.body:getLinearVelocity()
		self.body:setLinearVelocity(x,-160)
		self.jumptimer = self.jumptimer - dt
	end

	-- Salto
	if self.salto and (self.current == self.animations.walk or self.current == self.animations.idle) then
		self.rotate = (self.rotate + 17 * dt * self.facing) % 360
	elseif self.rotate ~= 0 then
		self.rotate = 0
	end

	-- # HORIZONTAL MOVEMENT
	-- Walking
	local x,y = self.body:getLinearVelocity()
	local controller = self:getController()
	if controller:isDown("left") then
		self.facing = -1
		self.body:applyForce(-250, 0)
		-- Controlled speed limit
		if x < -self.max_velocity then
			self.body:applyForce(250, 0)
		end
	end
	if controller:isDown("right") then
		self.facing = 1
		self.body:applyForce(250, 0)
		-- Controlled speed limit
		if x > self.max_velocity then
			self.body:applyForce(-250, 0)
		end
	end

	-- Custom linear damping
	if  not controller:isDown("left") and
		not controller:isDown("right")
	then
		local face = nil
		if x < -12 then
			face = 1
		elseif x > 12 then
			face = -1
		else
			face = 0
		end
		self.body:applyForce(40*face,0)
		if not self.inAir then
			self.body:applyForce(80*face,0)
		end
	end

	-- # ANIMATIONS
	-- Animation
	self.delay = self.delay - dt
	if self.delay < 0 then
		self.delay = self.delay + self.initial
		-- Thank you De Morgan!
		if self.current.repeated or not (self.frame == self.current.frames) then
			self.frame = (self.frame % self.current.frames) + 1
		elseif controller:isDown("right") or controller:isDown("left") then
			-- If nonrepeatable animation is finished and player is walking
			self:changeAnimation("walk")
		elseif self.current == self.animations.damage then
			self:changeAnimation("idle")
		end
	end

	-- # DEATH
	-- We all die in the end.
	local m = self.world.map
	if (self.body:getX() < m.center_x - m.width*1.5 or self.body:getX() > m.center_x + m.width*1.5  or
	    self.body:getY() < m.center_y - m.height*1.5 or self.body:getY() > m.center_y + m.height*1.5) and
	    self.alive
	then
		self:die()
	end

	-- respawn
	if self.spawntimer > 0 then
		self.spawntimer = self.spawntimer - dt
	end
	if self.spawntimer <= 0 and not self.alive and self.lives >= 0 then
		self:respawn()
	end

	-- # PUNCH
	-- Cooldown
	self.punchcd = self.punchcd - dt

	-- Stop vertical
	local c,a = self.current, self.animations
	if (c == a.attack_up or c == a.attack_down or c == a.attack) and self.frame < c.frames then
		if self.punchdir == 0 then
			self.body:setLinearVelocity(0,0)
		else
			self.body:setLinearVelocity(32*self.facing,0)
		end
	end

	if self.punchcd <= 0 and self.punchdir == 1 then
		self.punchdir = 0
	end
end

-- Keypressed callback (I think?) of `Player`
function Player:controllerPressed (key)
	local controller = self:getController()
	-- Jumping
	if key == "jump" then
		if not self.inAir then
			self:createEffect("jump")
			self.jumpactive = true
			if (self.current == self.animations.attack) or
			   (self.current == self.animations.attack_up) or
			   (self.current == self.animations.attack_down) then
				self:changeAnimation("idle")
			end
		elseif self.jumpdouble then
			self:createEffect("doublejump")
			self.jumpactive = true
			self.jumpdouble = false
			self.salto = true
		end
	end

	-- Walking
	if key == "left" or key == "right" then
		self:changeAnimation("walk")
	end

	-- Punching
	if key == "attack" and self.punchcd <= 0 then
		local f = self.facing
		self.salto = false
		if controller:isDown("up") then
			-- Punch up
			if self.current ~= self.animations.damage then
				self:changeAnimation("attack_up")
			end
			self:hit(2*f,-10,3*f,7, 0, -1)
		elseif controller:isDown("down") then
			-- Punch down
			if self.current ~= self.animations.damage then
				self:changeAnimation("attack_down")
			end
			self:hit(-4,-2,4,7, 0, 1)
		else
			-- Punch horizontal
			if self.current ~= self.animations.damage then
				self:changeAnimation("attack")
			end
			self:hit(2*f,-4,8*f,4, f, 0)
			self.punchdir = 1
		end
	end
end

-- Keyreleased callback (I think?) of `Player`
function Player:controllerReleased (key)
	local controller = self:getController()
	-- Jumping
	if key == "jump" then
		self.jumpactive = false
		self.jumptimer = 0.12
	end

	-- Walking
	if (key == "left" or key == "right") and not
	   (controller:isDown("left") or controller:isDown("right")) and
	   self.current == self.animations.walk
	then
		self:changeAnimation("idle")
	end
end

-- Draw of `Player`
function Player:draw (offset_x, offset_y, scale, debug)
	-- defaults
	local offset_x = offset_x or 0
	local offset_y = offset_y or 0
	local scale = scale or 1
	local debug = debug or false
	-- sprite draw
	love.graphics.setColor(255,255,255,255)
	love.graphics.draw(self.sprite, self.current[self.frame], (self.body:getX()+offset_x)*scale, (self.body:getY()+offset_y)*scale, self.rotate, self.facing*scale, 1*scale, 12, 15)
	-- debug draw
	if debug then
		love.graphics.setColor(50, 255, 50, 100)
		love.graphics.polygon("fill", self.world.camera:translatePoints(self.body:getWorldPoints(self.shape:getPoints())))
		love.graphics.setColor(255,255,255,255)
	end
end

-- Draw HUD of `Player`
-- elevation: 1 bottom, 0 top
function Player:drawHUD (x,y,scale,elevation)
	love.graphics.setColor(255,255,255,255)
	love.graphics.draw(self.portrait_sprite, self.portrait_sheet[self.name].normal, x*scale, y*scale, 0, scale, scale)
	local dy = 30 * elevation
	love.graphics.print(self.combo.."0x",(x+2)*scale,(y-3+dy)*scale,0,scale,scale)
	love.graphics.print(math.max(0, self.lives),(x+24)*scale,(y-3+dy)*scale,0,scale,scale)
end

-- Change animation of `Player`
-- idle, walk, attack, attack_up, attack_down, damage
function Player:changeAnimation(animation)
	self.frame = 1
	self.delay = self.initial
	self.current = self.animations[animation]
end

-- Spawn `Effect` relative to `Player`
function Player:createEffect(name)
	if name == "trail" or name == "hit" then
		-- 16px effect: -7 -7
		self.world:createEffect(name, self.body:getX()-8, self.body:getY()-8)
	elseif name ~= nil then
		-- 24px effect: -12 -15
		self.world:createEffect(name, self.body:getX()-12, self.body:getY()-15)
	end
end

-- Punch of `Player`
-- offset_x, offset_y, step_x, step_y, vector_x, vector_y
function Player:hit (ox, oy, sx, sy, vx, vy)
	-- start cooldown
	self.punchcd = self.punchinitial
	-- actual punch
	for k,n in pairs(self.world.Nauts) do
		if n ~= self then
			if self:testHit(n, ox, oy, sx, sy) then
				n:damage(vx, vy)
			end
		end
	end
end

-- Hittest
function Player:testHit (target, ox, oy, sx, sy)
	local x, y = self.body:getPosition()
	for v=0,2 do
		for h=0,2,1+v%2 do
			if target.fixture:testPoint(x+ox+h*sx, y+oy+v*sy) then return true end
		end
	end
	return false
end

-- Taking damage of `Player` by successful hit test
function Player:damage (horizontal, vertical)
	self:createEffect("hit")
	local x,y = self.body:getLinearVelocity()
	self.body:setLinearVelocity(x,0)
	self.body:applyLinearImpulse((32+12*self.combo)*horizontal, (68+10*self.combo)*vertical + 15)
	self:changeAnimation("damage")
	self.combo = math.min(20, self.combo + 1)
end

-- DIE
function Player:die ()
	self.combo = 1
	self.lives = self.lives - 1
	self.alive = false
	self.spawntimer = 1
	self.body:setActive(false)
	self.world:onNautKilled(self)
end

-- And then respawn. Like Jon Snow.
function Player:respawn ()
	self.alive = true
	self.body:setLinearVelocity(0,0)
	self.body:setPosition(self.world:getSpawnPosition())
	self.body:setActive(true)
	self:createEffect("respawn")
end