-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.lua
82 lines (77 loc) · 2.28 KB
/
enemy.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
require("lovepunk.entity")
require("helpfuldog")
Enemy = PhysicsObject.new(0, 0, 18, 18)
Enemy.__index = Enemy
function Enemy.new(x, y, w, h, player)
local self = setmetatable({}, Enemy)
self.x = x
self.y = y
self.width = w
self.height = h
self.player = player
self.friction = 0.6
self.gravity = 0.2
self.normalGravity = 0.2
self.speed = gs/1.7
self.accel = self.speed / 10
self.v = {x=0, y=0}
self.grounded = false
self.type = "enemy"
self.suckRange = 50
self.isSuckable = false
self.pullLock = false
self.damage = 1
return self
end
function Enemy:update(dt)
self.v.y = self.v.y + self.gravity
if self.isSuckable then
local items, len = self.scene.bumpWorld:querySegment(self.x + self.width/2,self.y+self.height/2,self.player.x + self.player.facingOffset.x,self.player.y + self.player.facingOffset.y)
local count = 0
for i, v in ipairs(items) do
if (v.type == "level") then count = count + 1 end
end
if (distance(self.player.x, self.player.y, self.x, self.y) < self.suckRange and count == 0) then
local isBeingSucked = false
local facing = self.player.facing
if self.player.vacuumState == VS_SUCKING and not self.player.carrying and
((facing == F_DOWN and self .y > self.player.y) or
(facing == F_UP and self.y < self.player.y) or
(facing == F_LEFT and self.x < self.player.x) or
(facing == F_RIGHT and self.x > self.player.x)) then
self.v = findVector({x=self.x, y=self.y}, {x=self.player.x, y=self.player.y}, 3)
self.beingPulled = true
end
end
if (self.pullLock) then
self.dislodged = true
self.lodgeTimer = 1
str = 5
self.v = findVector({x=self.x+self.width/2, y=self.y+self.height/2}, {x=self.player.x+self.player.facingOffset.x - self.width, y=self.player.y + self.player.facingOffset.y-self.height}, str)
end
end
PhysicsObject.update(self, dt)
local player = self:collide("player", self.x, self.y)
if player then
if self.isSuckable then
if player.canSuck then
player:suck(self)
self.scene:remove(self)
end
else
player:takeDamage(self, damage)
end
end
end
function Enemy:die()
self.scene:add(HitFx.new(self.x + self.width/2, self.y + self.height/2))
self.scene:shake(0.2, 3)
self.scene:remove(self)
end
function Enemy:flip(reverse)
if (reverse) then
self.scaleX = -1
else
self.scaleX = 1
end
end