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
|
require "not.Hero"
--- `Player`
-- Special `not.Hero` controllable by a player.
-- TODO: move functions and properties related to controls from `not.Hero`.
Player = Hero:extends()
Player.controllerSet =--[[Controller.sets.*]]nil
-- Constructor of `Player`.
function Player:new (name, x, y, world)
Player.__super.new(self, name, x, y, world)
end
-- Controller set manipulation.
function Player:assignControllerSet (set)
self.controllerSet = set
end
function Player:getControllerSet ()
return self.controllerSet
end
-- Check if control of assigned controller is pressed.
function Player:isControlDown (control)
return Controller.isDown(self:getControllerSet(), control)
end
function Player:isJumping ()
return self:isControlDown("jump")
end
function Player:isWalkingLeft ()
return self:isControlDown("left")
end
function Player:isWalkingRight ()
return self:isControlDown("right")
end
-- Controller callbacks.
function Player:controlpressed (set, action, key)
if set ~= self:getControllerSet() then return end
self.smoke = false -- TODO: temporary
-- Walking
if (action == "left" or action == "right") then
if (self.current ~= self.animations.attack) and
(self.current ~= self.animations.attack_up) and
(self.current ~= self.animations.attack_down) then
self:setAnimation("walk")
end
end
-- Punching
if action == "attack" and self.punchCooldown <= 0 then
local f = self.facing
self.salto = false
if self:isControlDown("up") then
-- Punch up
if self.current ~= self.animations.damage then
self:setAnimation("attack_up")
end
self:punch("up")
elseif self:isControlDown("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
end
end
end
function Player:controlreleased (set, action, key)
if set ~= self:getControllerSet() then return end
-- Jumping
if action == "jump" then
self.jumpTimer = Hero.jumpTimer -- take initial from metatable
end
-- Walking
if (action == "left" or action == "right") then
if not (self:isControlDown("left") or self:isControlDown("right")) then
if self.current == self.animations.walk then
self:setAnimation("default")
end
end
end
end
return Player
|