-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile.lua
executable file
·180 lines (158 loc) · 4.79 KB
/
mobile.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
local class = require "middleclass.middleclass"
local global = require "global"
local Collidable = require "collidable"
local Mobile = class("Mobile", Collidable)
local Static = require "static"
local shapes = require "hardoncollider.shapes"
local bufsize = 32
function Mobile:initialize(x, y, maxhp)
Collidable.initialize(self, x, y, maxhp)
self.vx = 0
self.vy = 0
self.targetx = nil
self.targety = nil
self.maxspeed = 1 --Stupid small default speed
self.layer = 10
-- Record previous positions
self.buffer = {}
self.bufidx = 0
self.rotation = 0
end
function Mobile:onCollision(other, dx, dy)
if other:isInstanceOf(Mobile) then
-- if other is Mobile, it will also be correcting for this collision
-- applying only half the separation vector produces less jitter
self.x = self.x + dx/2
self.y = self.y + dy/2
elseif other:isInstanceOf(Collidable) then
self.x = self.x + dx
self.y = self.y + dy
end
end
function Mobile:update(dt)
if self.targetx and self.targety then
local dx = self.targetx - self.x
local dy = self.targety - self.y
local mag = math.sqrt(dx^2 + dy^2)
if mag ~= 0 then
self.vx = dx / mag * self.maxspeed
self.vy = dy / mag * self.maxspeed
self.rotation = math.atan2(self.vx, -self.vy)
-- don't move further than we need to
if math.abs(self.vx * dt) > math.abs(dx) then
self.vx = dx
end
if math.abs(self.vy * dt) > math.abs(dy) then
self.vy = dy
end
else
self:stop()
end
end
self.x = self.x + self.vx * dt
self.y = self.y + self.vy * dt
self.buffer[self.bufidx] = {x=self.x, y=self.y}
self.bufidx = (self.bufidx + 1) % bufsize
local oldbufidx = (self.bufidx - bufsize) % bufsize
if self.buffer[oldbufidx] then
local dx = math.abs(self.buffer[oldbufidx].x - self.x)
local dy = math.abs(self.buffer[oldbufidx].y - self.y)
if dx < 2 and dy < 2 then
self:stop()
end
end
Collidable.update(self, dt)
end
function Mobile:getClosest(objType, dist, mustSee)
local function compare(entity1, entity2)
return self:getAbsDistance(entity1) < self:getAbsDistance(entity2)
end
assert(objType)
local entities = {}
for _, e in pairs(global.entities) do
if e.class.name == objType and
(dist == nil or self:getAbsDistance(e) < dist) then
table.insert(entities, e)
end
end
table.sort(entities, compare)
for _, entity in ipairs(entities) do
-- make nil truthy
if mustSee == false or self:canSee(entity) then
return entity
end
end
end
function riskyCanSee(self, target)
local points = {self.x, self.y}
if self.x < target.x then
points[3] = self.x + 1
points[4] = self.y
if self.y < target.y then
points[5] = target.x + 2
points[6] = target.y + 1
points[7] = target.x + 1
points[8] = target.y + 1
else
points[5] = target.x + 1
points[6] = target.y + 1
points[7] = target.x
points[8] = target.y + 1
end
else
points[3] = self.x - 1
points[4] = self.y
if self.y < target.y then
points[5] = target.x + 1
points[6] = target.y + 1
points[7] = target.x + 2
points[8] = target.y + 1
else
points[5] = target.x
points[6] = target.y + 1
points[7] = target.x + 1
points[8] = target.y + 1
end
end
local check = shapes.newPolygonShape(unpack(points))
local x1 = math.min(self.x, target.x)
local y1 = math.min(self.y, target.y)
local x2 = math.max(self.x, target.x)
local y2 = math.max(self.y, target.y)
for _, e in pairs(global.collider:shapesInRange(x1, y1, x2, y2)) do
-- Only static things which stop bullets obstruct line of
-- sight
if e.entity ~= target and
e.entity:isInstanceOf(Static) and
e.entity:stopsBullets() and
not e.entity.is_open and
e:collidesWith(check) then
return false
end
end
return true
end
-- Horrible work-around
function Mobile:canSee(target)
out, res = pcall(function () return riskyCanSee(self, target) end)
if out then
return res
else
return false
end
end
function Mobile:setTarget(x, y)
self.targetx = x
self.targety = y
self.buffer = {}
end
function Mobile:setMaxSpeed(speed)
self.maxspeed = speed
end
function Mobile:stop()
self.vx = 0
self.vy = 0
self.targetx = nil
self.targety = nil
end
return Mobile