-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchar.lua
265 lines (226 loc) · 6.74 KB
/
char.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
local rect = require("rect")
local camera = require("camera")
local playCamera = camera()
local rotation = 0;
local char = {}
setmetatable(char,char)
function char:__index(key)
return rawget(char,key)
end
function char:__call(nx,ny,handler)
local c = {x = nx, y = ny,collisionboxes = {},hurtboxes = {}, lookingRight =true, width = 0, handler = handler, word = "none", back = 'l', forward = 'r',bonus = {},bonusIndexes = {}, knockupHurtboxes = {}, knockupCollisionboxes = {},throwboxes = {},health = 1000}
setmetatable(c,char)
return c
end
function char:setstanding(newstanding)
self.standing = newstanding
self.state = self.standing:copy()
end
function char:setCrouching(newCrouching)
self.crouching = newCrouching
end
function char:setJumping(newJumping)
self.jumping = newJumping
end
function char:addCollisionbox(hx,hy,width,height)
table.insert(self.collisionboxes,rect(self.x+hx,self.y+hy,width,height)) -- place the hurtboxes in the relative grid
-- Probably refactor since these two functions are same
end
function char:ensureMapLimits()
for k,v in ipairs(self.state.collisionboxes) do
if v.x < playCamera.x then
self:__doMove(playCamera.x-v.x,0)
elseif v.endx > playCamera.x+640 then
self:__doMove((playCamera.x+640)-v.endx,0)
end
end
end
function char:__doMove(xVel,yVel)
--change character coordinates
self.x = self.x+xVel
self.y = self.y+yVel
--move all Hurtboxes
for k,v in ipairs(self.state.hurtboxes) do
v:setX(v.x+xVel)
v:setY(v.y+yVel)
end
for k,v in ipairs(self.state.collisionboxes) do
v:setX(v.x+xVel)
v:setY(v.y+yVel)
end
if self.state.hitboxes then for k,v in ipairs(self.state.hitboxes) do
v:setX(v.x+xVel)
v:setY(v.y+yVel)
end end
--Make sure the character did not step outside the map borders
--if he did,move him back inside
self:ensureMapLimits()
end
function char:setKnockupBoxes(hurt,colli)
self.knockupHurtboxes = hurt
self.knockupCollisionboxes = colli
end
local function checkCollision(self,otherChar,xVel,yVel)
if xVel < 0 then -- when moving to the left
for k,v in ipairs(self.state.collisionboxes) do
for k2,v2 in ipairs (otherChar.state.collisionboxes) do
if v.x <= playCamera.x then
return "border",playCamera.x - v.x
elseif(v:collide(v2) and v2.x <= v.x) then
return "player",(v.x-v2.endx)+1
end
end
end
elseif xVel > 0 then --when moving to the right
for k,v in ipairs(self.state.collisionboxes) do
for k2,v2 in ipairs (otherChar.state.collisionboxes) do
if v.endx >= playCamera.x + 640 then
return "border",(playCamera.x+640)-v.endx
elseif(v:collide(v2) and v2.endx >= v.endx) then
return "player",(v.endx -v2.x)-1
end
end
end
end
end
function char:move(xVel,yVel,otherChar,ignoreCollision)
local collision,distance = checkCollision(self,otherChar,xVel,yVel)
if (ignoreCollision and not (collision=="border")) or not collision then
self:__doMove(xVel,yVel)
elseif collision == "player" and not (checkCollision(otherChar,self,xVel,yVel)=="border") then
otherChar:move(xVel/2,0,self,true)
self:__doMove(xVel/2,yVel)
else
self:__doMove(0,yVel)
end
local nowCollide,distance = checkCollision(self,otherChar,xVel,yVel)
if not ignoreCollision and nowCollide then
self:__doMove(-distance,0)
end
end
--the two functions below definitely need to be cleaned up
function flipBox(box,width,self)-- takes a rect and flips it width refers to the width of the character!
local nx = box.x-self.x --get the hurtboxe's "local" coordinates
nx = nx+box.width --get the upper right corner
nx = nx-width --move the y axis to the middle of the character
box:setX(-nx+self.x) --mirror the upper right corner,as width and height stay the same it falls into place
end
function char:flip()
width = self.state:getWidth()
self.lookingRight = not self.lookingRight
if self.lookingRight then
self.back = 'l'
self.forward = 'r'
else
self.back ='r'
self.forward = 'l'
end
self.state:flipBoxes()
self:ensureMapLimits()
end
function char:draw(coord,name)
love.graphics.setColor(255,255,255) -- set color to white
--[[ if self.lookingRight then
love.graphics.draw(self.image,self.x,self.y)
else
love.graphics.draw(self.image,self.x,self.y,0,-1,1,self.image:getWidth(),0)
end]]
love.graphics.setColor(0,255,0)
for k,v in ipairs(self.state.collisionboxes) do love.graphics.rectangle("line",v.x,v.y,v.width,v.height) end
love.graphics.setColor(255,0,0)--set color to red
for k,v in ipairs(self.state.hurtboxes) do love.graphics.rectangle("line",v.x,v.y,v.width,v.height) end -- draw hurtboxes for debugging
love.graphics.setColor(255,255,0)
if self.state.draw then self.state:draw() end
end
function char:handleInput(inputs)
self.state:handleInput(inputs)
end
function char:supplyBoxes()
return self.state:supplyBoxes()
end
function char:update(opponent)
self.state:update()
self.handler:update()
for k,v in ipairs(self.bonus) do
v:update(self,opponent)
end
end
function char:queueState(state)
self.nextState = state
end
function char:isBlocking()
return self.state:isBlocking()
end
function char:setState(toSet)
if self.state then
self.state:setState(toSet)
else
self.state=toSet:copy()
end
if not self.lookingRight then
self.lookingRight = true
self:flip(75)
end
self.state:init()
self.state:checkInputs()
self:ensureMapLimits()
end
function char:setJumpForward(newJf)
self.jumpForward = newJf
end
function char:setJumpBack(newJb)
self.jumpBack = newJb
end
function char:getBottom()--returns the lowest coordinate of the character's collisionboxes
return self.state:getBottom()
end
function char:handleHit(damage,chip,hitEffect,blockEffect)
self.state:handleHit(damage,chip,hitEffect,blockEffect,level);
end
function char:doDamage(damage)
self.health = self.health - damage
distributeEvents("damageReceived",self.c1,damage)
--TODO replace this behaviour to allow more dynamic results ,e.g rounds
if self.health <= 0 then
switchGamestate(require "startGamestate")
end
end
function char:addBonus(b,ch2)
table.insert(self.bonus,b)
self.bonusIndexes[b] = #self.bonus
end
function char:removeBonus(b)
table.remove(self.bonus,self.bonusIndexes[b])
self.bonusIndexes[b] = nil
end
function char:getHeight()
return self:getBottom() - self.y
end
function char:getWidth()
return self.state:getWidth()
end
function char:getCollisionStart()
return self.state:getCollisionStart()
end
--inner class hurtbox
local hurtbox = {}
setmetatable(hurtbox,hurtbox)
hurtbox.__index = rect
function hurtbox:__call(x,y,width,height,flags)
local nt = rect(x,y,width,height)
setmetatable(nt,{__index = hurtbox})
if flags then
nt.flags = flags
else
nt.flags = {}
end
return nt
end
function hurtbox:hasFlag(flag)
for k,v in ipairs(self.flags) do
if v == flag then return true end
end
return false
end
char.hurtbox = hurtbox
return char