/
Player.lua
126 lines (106 loc) · 2.64 KB
/
Player.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
Player = Class{__includes = {Block}}
local function Sign(n)
if n == 0 then
return 0
elseif n < 0 then
return -1
else
return 1
end
end
function Player:init(x, y, w, h, Grav)
Block.init(self, x, y, w, h)
self.Gravity = Grav
self.Velocity = {x = 0, y = 0}
self.ApplyGravity = true
self.checkpoint = {x = x, y = y}
end
function Player:move(x, y)
Block.move(self, x, y)
self.ApplyGravity = true
end
function Player:moveTo(x, y)
Block.moveTo(self, x, y)
self.ApplyGravity = true
end
function Player:update(dt)
if(self.ApplyGravity) then
self.Velocity.y = self.Velocity.y + self.Gravity * dt
end
local Collision = HC.collisions(self)
local IsOnSomething = false
for Shape, Vector in pairs(Collision) do
if Shape.IsDamaging then
Reset(true)
return
end
if Sign(-Vector.x) == Sign(self.Velocity.x) then
self.Velocity.x = 0
end
if(Sign(-Vector.y) == Sign(self.Velocity.y)) then
if(Sign(self.Velocity.y) == 1) then
self.ApplyGravity = false
IsOnSomething = true
self.Velocity.y = 1
else
self.Velocity.y = 0
end
end
if Shape.IsCheckpoint then
local X, Y = self.InternalShape:center()
self.checkpoint = {x = X, y = Y}
end
self.InternalShape:move(Vector.x, Vector.y)
self.ShadowBody:move(Vector.x, Vector.y)
end
if IsOnSomething == false then
self.ApplyGravity = true
end
if self.ApplyGravity == false then
local NearbyObjects = HC.neighbors(self)
for Object,_ in pairs(NearbyObjects) do
local FoundObject
for _, SearchObject in pairs(MovingWorld) do
local SearchX, SearchY = SearchObject.Item:center()
local ObjectX, ObjectY = Object:center()
if SearchX == ObjectX and SearchY == ObjectY then
FoundObject = SearchObject.Item
break
end
end
if FoundObject ~= nil then
if FoundObject.Velocity then
local _, ObjectY, _, _ = FoundObject:bbox()
local _, _, _, MyYandH = self:bbox()
if ObjectY == MyYandH then
self.InternalShape:move(FoundObject.Velocity.x*dt, FoundObject.Velocity.y*dt)
self.ShadowBody:move(FoundObject.Velocity.x*dt, FoundObject.Velocity.y*dt)
end
end
end
end
end
self.InternalShape:move(self.Velocity.x*dt, self.Velocity.y*dt)
self.ShadowBody:move(self.Velocity.x*dt, self.Velocity.y*dt)
end
function Player:AddVelocity(x, y)
if x then
self.Velocity.x = self.Velocity.x + x
end
if y then
self.Velocity.y = self.Velocity.y + y
self.ApplyGravity = true
end
end
function Player:SetVelocity(x, y)
if x then
self.Velocity.x = x
end
if y then
self.Velocity.y = y
self.ApplyGravity = true
end
end
function Player:GetVelocity()
return {x = self.Velocity.x, y = self.Velocity.y}
end