summaryrefslogtreecommitdiffhomepage
path: root/player.lua
blob: f01fcba93da8270e1b773e90a1199253cb897d75 (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
-- `Player`
-- Collision category: [2]

-- Metatable of `Player`
-- nils initialized in constructor
Player = {
	-- General and physics
	name = "Player",
	body = nil,
	shape = nil,
	fixture = nil,
	sprite = nil,
	rotate = 0, -- "angle" would sound better
	facing = 1,
	max_velocity = 105,
	combo = 1,
	-- Animation
	animations = require "animations",
	current = nil,
	frame = 1,
	delay = 0.10,
	initial = nil,
	-- Movement
	inAir = true,
	jumpactive = false,
	jumpdouble = true,
	jumptimer = 0.14,
	-- Keys
	key_jump  = "rshift",
	key_left  = "left",
	key_right = "right",
	key_up    = "up",
	key_down  = "down",
	key_hit   = "return" -- don't ask
}

-- Constructor of `Player`
function Player:new(world, x, y, spritesheet)
	-- 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.sprite  = love.graphics.newImage(spritesheet)
	o.fixture:setUserData(o)
	o.fixture:setCategory(2)
	o.fixture:setMask(2)
	o.body:setFixedRotation(true)
	o.body:setLinearDamping(0.1)
	print(o.body.mass)
	-- Animation
	o.initial = o.delay
	o.current = o.animations.idle
	return o
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 not self.jumpdouble and self.inAir then
		self.rotate = (self.rotate + 17 * dt * self.facing) % 360
	else
		self.rotate = 0
	end
	
	-- HORIZONTAL MOVEMENT
	-- Walking
	local x,y = self.body:getLinearVelocity()
	if love.keyboard.isDown(self.key_left) then
		self.facing = -1
		self.body:applyForce(-200, 0)
		if x < -self.max_velocity then
			self.body:applyForce(200, 0)
		end
	end
	if love.keyboard.isDown(self.key_right) then
		self.facing = 1
		self.body:applyForce(200, 0)
		if x > self.max_velocity then
			self.body:applyForce(-200, 0)
		end
	end

	-- Limit `Player` horizontal speed
	-- Maximum speed may be actually a little bit higher or lower
	--[[
	local x,y = self.body:getLinearVelocity()
	if x > self.max_velocity then
		self.body:setLinearVelocity(self.max_velocity, y)
	end
	if x < -self.max_velocity then
		self.body:setLinearVelocity(-self.max_velocity, y)
	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 love.keyboard.isDown(self.key_right) or
			   love.keyboard.isDown(self.key_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
end

-- Keypressed callback of `Player`
function Player:keypressed(key)
	-- Jumping
	if key == self.key_jump then
		if not self.inAir then
			self.jumpactive = true
		elseif self.jumpdouble then
			self.jumpactive = true
			self.jumpdouble = false
		end
	end
	
	-- Walking
	if key == self.key_left or key == self.key_right then
		self:changeAnimation("walk")
	end
	
	-- Punching
	if key == self.key_hit then
		-- Punch up
		if love.keyboard.isDown(self.key_up) then
			self:hit(0, -1)
		-- Punch down
		elseif love.keyboard.isDown(self.key_down) and self.inAir then
			self:hit(0, 1)
		-- Punch horizontal
		else
			self:hit(self.facing, 0)
		end
	end
end

-- Keyreleased callback of `Player`
function Player:keyreleased(key)
	-- Jumping
	if key == self.key_jump then
		self.jumpactive = false
		self.jumptimer = 0.12
	end
	
	-- Walking
	if (key == self.key_left or key == self.key_right) and not
	   (love.keyboard.isDown(self.key_left) or love.keyboard.isDown(self.key_right)) and
	   self.current == self.animations.walk
	then
		self:changeAnimation("idle")
	end
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

-- Punch of `Player`
function Player:hit(horizontal, vertical)
	if vertical == -1 then
		self:changeAnimation("attack_up")
	elseif vertical == 1 then
		self:changeAnimation("attack_down")
	else
		self:changeAnimation("attack")
		self.body:applyForce(800*self.facing, 0)
	end
	for k,n in pairs(Nauts) do
		if n ~= self then
			local didHit = false
			if n.fixture:testPoint(self.body:getX()+12*horizontal,self.body:getY()-2) then
				didHit = true
			end
			if n.fixture:testPoint(self.body:getX()+7*horizontal,self.body:getY()+2) then
				didHit = true
			end
			if n.fixture:testPoint(self.body:getX()+17*horizontal,self.body:getY()+2) then
				didHit = true
			end
			if n.fixture:testPoint(self.body:getX()+12*horizontal,self.body:getY()+6) then
				didHit = true
			end
			if didHit then
				n:damage(horizontal, vertical)
				n.combo = n.combo + 1
				print(n.combo)
			end
		end
	end
end

-- Taking damage of `Player` by successful hit test
function Player:damage(horizontal, vertical)
	self.body:applyLinearImpulse((10+10*self.combo)*horizontal, (50+10*self.combo)*vertical + 15)
	self:changeAnimation("damage")
end