summaryrefslogtreecommitdiffhomepage
path: root/not/Player.lua
blob: 2bee903d356fc9dd1d0e09a222e808d25474a322 (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
--- `Player`
-- Special `not.Hero` controllable by a player.
Player = {
	-- TODO: move functions and properties related to controls from `not.Hero`.
	controlSet = --[[Controller.sets.*]]nil,
}

-- `Player` is a child of `Hero`.
require "not.Hero"
Player.__index = Player
setmetatable(Player, Hero)

-- Constructor of `Player`.
function Player:new (name, game, x, y)
	local o = setmetatable({}, self)
	o:init(name, game, x, y)
	-- Load portraits statically to `not.Hero`.
	-- TODO: this is heresy, put it into `load` method or something similar.
	if Hero.portrait_sprite == nil then
		Hero.portrait_sprite = love.graphics.newImage("assets/portraits.png")
		Hero.portrait_frame = love.graphics.newImage("assets/menu.png")
	end
	return o
end

-- Initializer of `Player`.
function Player:init (...)
	Hero.init(self, ...)
end

-- Controller set manipulation.
function Player:assignControlSet (set)
	self.controlset = set
end
function Player:getControlSet ()
	return self.controlset
end

-- Check if control of assigned controller is pressed.
function Player:isControlDown (control)
	return Controller.isDown(self:getControlSet(), control)
end

-- Update of `Player`.
function Player:update (dt)
	local x, y = self:getLinearVelocity()
	Hero.update(self, dt) -- TODO: It would be probably a good idea to add return to update functions to terminate if something goes badly in parent's update.

	-- Jumping.
	if self.isJumping and self.jumpTimer > 0 then
		self:setLinearVelocity(x,-160)
		self.jumpTimer = self.jumpTimer - dt
	end

	-- Walking.
	if self:isControlDown("left") then
		self.facing = -1
		self:applyForce(-250, 0)
		-- Controlled speed limit
		if x < -self.max_velocity then
			self:applyForce(250, 0)
		end
	end
	if self:isControlDown("right") then
		self.facing = 1
		self:applyForce(250, 0)
		-- Controlled speed limit
		if x > self.max_velocity then
			self:applyForce(-250, 0)
		end
	end

	-- Limiting walking speed.
	if not self:isControlDown("left") and
	   not self:isControlDown("right")
	then
		local face = nil
		if x < -12 then
			face = 1
		elseif x > 12 then
			face = -1
		else
			face = 0
		end
		self:applyForce(40*face,0)
		if not self.inAir then
			self:applyForce(80*face,0)
		end
	end
end

-- Controller callbacks.
function Player:controlpressed (set, action, key)
	if set ~= self:getControlSet() then return end
	local isDown = Controller.isDown
	local controlset = self:getControlSet()
	-- Jumping
	if action == "jump" then
		if self.jumpCounter > 0 then
			-- General jump logics
			self.isJumping = true
			--self:playSound(6)
			-- Spawn proper effect
			if not self.inAir then
				self:createEffect("jump")
			else
				self:createEffect("doublejump")
			end
			-- Start salto if last jump
			if self.jumpCounter == 1 then
				self.salto = true
			end
			-- Animation clear
			if (self.current == self.animations.attack) or
			   (self.current == self.animations.attack_up) or
			   (self.current == self.animations.attack_down) then
				self:setAnimation("default")
			end
			-- Remove jump
			self.jumpCounter = self.jumpCounter - 1
		end
	end

	-- Walking
	if (action == "left" or action == "right") and 
	   (self.current ~= self.animations.attack) and
	   (self.current ~= self.animations.attack_up) and
	   (self.current ~= self.animations.attack_down) then
		self:setAnimation("walk")
	end

	-- Punching
	if action == "attack" and self.punchCooldown <= 0 then
		local f = self.facing
		self.salto = false
		if isDown(controlset, "up") then
			-- Punch up
			if self.current ~= self.animations.damage then
				self:setAnimation("attack_up")
			end
			self:punch("up")
		elseif isDown(controlset, "down") then
			-- Punch down
			if self.current ~= self.animations.damage then
				self:setAnimation("attack_down")
			end
			self:punch("down")
		else
			-- Punch horizontal
			if self.current ~= self.animations.damage then
				self:setAnimation("attack")
			end
			if f == 1 then
				self:punch("right")
			else
				self:punch("left")
			end
			self.punchdir = 1
		end
	end
end
function Player:controlreleased (set, action, key)
	if set ~= self:getControlSet() then return end
	local isDown = Controller.isDown
	local controlset = self:getControlSet()
	-- Jumping
	if action == "jump" then
		self.isJumping = false
		self.jumpTimer = Hero.jumpTimer -- take initial from metatable
	end
	-- Walking
	if (action == "left" or action == "right") and not
	   (isDown(controlset, "left") or isDown(controlset, "right")) and
	   self.current == self.animations.walk
	then
		self:setAnimation("default")
	end
end