-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfly.lua
122 lines (102 loc) · 2.74 KB
/
fly.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
require "collisions"
local fly = {}
fly.__index = fly
function newFly(object)
local n = object
n.width = 8
n.height = 8
n.xspeed = 0
n.yspeed = 0
n.behavior = "random"
n.t = 0
n.HIT = 0
n.target = false
n.direction = "left"
n.animations = {
left = newAnimation(IMG_fly_left, 16, 16, 2, 10),
right = newAnimation(IMG_fly_right, 16, 16, 2, 10),
}
n.anim = n.animations[n.direction]
return setmetatable(n, fly)
end
function fly:distance(ch)
local dX = (self.x + self.width/2) - (ch.x + ch.width/2)
local dY = (self.y + self.height/2) - (ch.y + ch.height/2)
return math.sqrt( ( dX^2 ) + ( dY^2 ) )
end
function fly:update(dt)
self.t = self.t + 1
if self:distance(character1) < 64 and self.behavior == "random" and character1.hp > 0 and not self.target then
self.behavior = "follow"
sfx_fly:play()
self.target = character1
end
if character2 and self:distance(character2) < 64 and self.behavior == "random" and character2.hp > 0 and not self.target then
self.behavior = "follow"
sfx_fly:play()
self.target = character2
end
if self.behavior == "random" and self.t % 200 == 0 and self.HIT == 0 then
self.xspeed = math.random(-0.1, 0.1)
self.yspeed = math.random(-0.1, 0.1)
if self.xspeed > 0 then self.direction = "right" else self.direction = "left" end
elseif self.behavior == "follow" and self.HIT == 0 then
if self.x < (self.target.x+self.target.width/2-4) then
self.xspeed = 0.4
else
self.xspeed = -0.4
end
if self.y < (self.target.y+self.target.height/2-4) then
self.yspeed = 0.4
elseif self.y > self.target.y then
self.yspeed = -0.4
end
if self.xspeed > 0 then self.direction = "right" else self.direction = "left" end
end
self.x = self.x + self.xspeed
self.y = self.y + self.yspeed
if self.HIT > 0 then
self.HIT = self.HIT - 1
end
if self.HIT == 1 then
entities_remove(self)
return
end
solid_collisions(self)
self.anim = self.animations[self.direction]
self.anim:update(dt)
end
function fly:draw()
self.anim:draw(self.x-4, self.y-4)
end
function fly:on_collide(e1, e2, dx, dy)
if e2.type == "ground" then
if math.abs(dy) < math.abs(dx) and dy ~= 0 then
self.yspeed = 0
self.y = self.y + dy
end
if math.abs(dx) < math.abs(dy) and dx ~= 0 then
self.xspeed = 0
self.x = self.x + dx
end
elseif (e2.type == "sword" or e2.type == "magicarrow") and self.HIT == 0 then
local dmg = 1
self.HIT = 32
if e2.x < self.x then
self.xspeed = 2
else
self.xspeed = -2
end
self.behavior = "follow"
table.insert(effects, newNotif({x=self.x, y=self.y, text=dmg}))
sfx_flydie:play()
elseif e2.type == "character" and e2.HIT == 0 and e2.hp > 0 then
if self.x < e2.x then
e2.x = e2.x + 1
self.x = self.x - 1
else
e2.x = e2.x - 1
self.x = self.x + 1
end
end
end