-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimal.script
More file actions
203 lines (170 loc) · 5.07 KB
/
animal.script
File metadata and controls
203 lines (170 loc) · 5.07 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local global = require "main.global" -- Import the module
local function set_animal_type(self)
local anim = hash("deer")
if self.type == "Deer" then
anim = hash("deer")
elseif self.type == "Bear" then
anim = hash("bear")
elseif self.type == "Boar" then
anim = hash("boar")
end
msg.post("#sprite", "play_animation", { id = anim })
msg.post("/initfactory#init_spawner", "animal_spawned", {r_id = go.get_id(), type = string.lower(self.type)})
end
function init(self)
local picker = math.random(1,10)
if picker < 2 then
self.type = "Bear"
self.damage = 10
self.speed = 80
self.health = 250
self.stamina = 800
elseif picker < 5 then
self.type = "Boar"
self.damage = 5
self.speed = 60
self.health = 100
self.stamina = 300
else
self.type = "Deer"
self.speed = 100
self.health = 50
self.stamina = 800
end
self.max_health = self.health
self.attacking = false
self.max_stamina = self.stamina
self.stamina_regen = self.max_stamina/100
self.new_pos = go.get_position()
self.new_pos_time = math.random(1,15)
self.running = false
set_animal_type(self)
self.look_time =1
self.target = nil
self.attack_time = 1
end
local function move(self, dt)
if global.distance_to(go.get_position(), self.new_pos) < 10 and not self.running and not self.attacking then
self.new_pos_time = self.new_pos_time - dt
if self.new_pos_time < 0 then
self.new_pos = global.random_position()
self.new_pos_time = math.random(1,15)
end
else
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)
local step = self.speed * dt -- Calculate step size based on speed
local new_position
if self.running and global.distance_to(go.get_position(), self.new_pos) < 200 then
new_position = current_pos - (self.dir * math.min(step, distance)) -- Move towards target
self.stamina = self.stamina - 1
self.speed = self.stamina/20
else
new_position = current_pos + (self.dir * math.min(step, distance)) -- Move towards target
if not self.attacking then
self.stamina = self.stamina - 1
self.speed = self.stamina/20
end
end
go.set_position(new_position)
end
end
local function look(self, dt)
for _, person in ipairs(_G.persons) do
if global.distance_to(go.get_position(), go.get_position(person)) < 160 then
self.new_pos = go.get_position(person)
if self.type == "Deer" then
self.running = true
return
else
self.attacking = true
self.target = person
return
end
end
end
self.running = false
self.attacking = false
self.target = nil
self.new_pos = go.get_position()
end
local function attack(self, dt)
self.attack_time = self.attack_time - dt
if self.attack_time < 0 then
if not go.exists(self.target) then
look(self, dt)
return
end
if global.distance_to(go.get_position(), go.get_position(self.target)) > 20 then
self.attacking = false
self.target = nil
return
end
msg.post(self.target, "hit", {damage = self.damage})
self.new_pos = go.get_position(self.target)
self.attack_time = 1
end
end
function update(self, dt)
if self.stamina > self.max_stamina * 0.1 and not self.target then
move(self, dt)
end
if self.stamina < self.max_stamina * 0.11 then
self.stamina_regen = self.stamina_regen - dt
if self.stamina_regen < 0 then
self.stamina = self.max_stamina
self.stamina_regen = self.max_stamina/100
end
end
if self.health < self.max_health and not self.target then
look(self, dt)
end
if self.attacking then
attack(self, dt)
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
function on_message(self, message_id, message, sender)
if message_id == hash("hit") then
self.health = self.health - message.damage
update_alpha(self)
if self.type == "Deer" then
self.stamina = self.stamina - 5
self.max_stamina = self.max_stamina - 5
end
if self.health <= 0 then
if not self.dead then
self.dead = true
end
local ppl_in_range = {}
if global.contains(_G[string.lower(self.type) .. "s"], go.get_id()) then
table.remove(_G[string.lower(self.type) .. "s"], global.remove_by_value(_G[string.lower(self.type) .. "s"], go.get_id()))
end
if _G.persons then
for _, person_id in ipairs(_G.persons) do
if global.distance_to(go.get_position(), go.get_position(person_id)) < 200 then
table.insert(ppl_in_range, person_id)
else
msg.post(person_id, "enemy_destroyed", { enemy_id = go.get_id()})
end
end
for _, p in ipairs(ppl_in_range) do
msg.post(p, "enemy_destroyed", { enemy_id = go.get_id(), rewards = {"food", self.max_health/#ppl_in_range}})
end
end
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="animal"})
timer.delay(0.3, false, function()
print("Deleted animal")
go.delete()
end)
end
end
end