-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.script
More file actions
156 lines (142 loc) · 4.37 KB
/
enemy.script
File metadata and controls
156 lines (142 loc) · 4.37 KB
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
local global = require "main.global" -- Import the module
local level = 1
function init(self)
self.speed = 40
self.new_pos_time = 2
self.new_pos = go.get_position()
self.moving = false
self.dir = vmath.vector3()
self.scan_time = 1
self.attacking = false
self.attack_time = 1
self.health = 25*(_G.level*_G.level)
self.max_health = self.health
self.targets = {}
self.damage = 1*(_G.level*_G.level)
self.dead = false
self.charging = false
self.base = nil
end
local function new_pos(self, dt)
self.new_pos_time = self.new_pos_time - dt
if self.new_pos_time < 0 then
self.new_pos = global.random_position_around_base(go.get_position(self.base), 150) or vmath.vector3(0,0,0)
self.moving = true
self.new_pos_time = math.random(5, 25)
end
end
local function move(self, dt)
local current_pos = go.get_position()
self.dir = vmath.normalize(self.new_pos - current_pos) -- Get direction to target
local distance = vmath.length(self.new_pos - current_pos) -- Distance to target
if distance > 100 or self.charging and distance > 250 then -- Stop moving if close enough
local step = self.speed * dt -- Calculate step size based on speed
local new_position = current_pos + self.dir * math.min(step, distance) -- Move towards target
go.set_position(new_position)
end
end
local function attack(self, dt)
self.attack_time = self.attack_time - dt
local temp_target = self.targets[math.random(1, #self.targets)]
local target_pos
if go.exists(temp_target) then
target_pos = go.get_position(temp_target)
end
local shot = false
if self.attack_time < 0 and #self.targets > 0 and go.exists(temp_target) then
local direction = vmath.normalize(target_pos - go.get_position())
local angle = math.atan2(direction.y, direction.x)
local laser = factory.create("#laser_factory", go.get_position(), vmath.quat_rotation_z(angle))
msg.post(laser, "damage_amt", {damage = self.damage})
self.attack_time = 0.5
shot = true
end
--if go.exists(temp_target) then
-- if global.distance_to(go.get_position(), go.get_position(temp_target)) > 240 and not shot then
-- self.new_pos = go.get_position(temp_target)
-- move(self, dt)
-- end
--end
end
local function scan(self, dt)
self.scan_time = self.scan_time - dt
if self.scan_time < 0.5 and self.target == nil then
msg.post("/initfactory#init_spawner", "scan_enemy_radius", {enemy_pos = go.get_position()})
self.scan_time = 1
end
end
local function update_alpha(self)
-- Set sprite color to red
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 0, 0, 1))
-- Reset color after 1 second
timer.delay(0.5, false, function()
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 1, 1, 1)) -- Back to normal
end)
end
local function update_display(self, dt)
msg.post("#enemyUI", "update_health", {
health = self.health,
position = go.get_position(),
owner = go.get_id(),
max_health = self.max_health
})
end
function update(self, dt)
scan(self, dt)
if not self.attacking and not self.charging then
new_pos(self, dt)
end
if self.moving then
move(self, dt)
end
if #self.targets > 0 and not self.charging then
self.attacking = true
self.moving = false
else
self.attacking = false
self.moving = true
end
if (self.attacking or self.charging) and #self.targets > 0 then
attack(self, dt)
self.charging = false
end
update_display(self, dt)
end
function on_message(self, message_id, message, sender)
if message_id == hash("targets_for_enemy") then
self.targets = message.targets
self.attacking = true
end
if message_id == hash("info_on_spawn") then
self.base = message.base
end
if message_id == hash("hit") then
self.health = self.health - message.damage
update_alpha(self)
if self.health < 0 then
if not self.dead then
self.dead = true
msg.post("/gui#ui", "enemy_killed", {mult = level})
--level = level + 0.1
end
if _G.persons then
for _, person_id in ipairs(_G.persons) do
msg.post(person_id, "enemy_destroyed", { enemy_id = go.get_id() })
end
end
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="enemy"})
timer.delay(0.3, false, function()
go.delete()
print("Deleted enemy")
end)
end
end
if message_id == hash("charge") then
self.new_pos = go.get_position(_G.bases[1])
self.moving = true
self.charging = true
end
if message_id == hash("offset_ui") then
msg.post("#enemyUI", "offset", { pos = message.pos})
end
end