-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction_handling.lua
240 lines (202 loc) · 7.92 KB
/
action_handling.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
local object_manager = require 'object_manager'
local action_handling = {}
-- target_selection_type -> { ... }
action_handling.registered_target_selections = {}
-- effect_type -> { ... }
action_handling.registered_effects = {}
action_handling.registered_effects_multitarget = {}
-- target_selection_type: projectile, ae, self
-- effect_type: damage, heal, runspeed, spawn, transfer, damage_over_time
--[[
application = {
target_selection = {target_selection_type = "cone", range = 10, cone = 60, piercing_number = 3, gfx = "/assets/action_projectiles/shield_bash_projectile.png"},
effects = {
{effect_type = "damage", str = 15},
{effect_type = "stun", duration = 3},
},
},
]]
-- any: contains x,y,rotation or oid
-- returns {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
function action_handling.get_target (any)
if any.oid then return { oid = any.oid }
else return { x = any.x or 0, y = any.y or 0, rotation = any.rotation or 0 } end
end
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- returns x,y or view
function action_handling.get_view (target)
local x,y = action_handling.get_target_position(target)
return target.viewx or x, target.viewy or y
end
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- returns x,y (id oid it returns its center)
function action_handling.get_target_position (target)
local x,y = target.x, target.y
local w,h = 0,0
if target.oid then
local o = object_manager.get(target.oid)
if o then
x = o.x or x
y = o.y or y
w = o.width or w
h = o.height or h
end
end
x = x or 0
y = y or 0
return x + w/2, y + h/2
end
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- returns rotation
function action_handling.get_target_rotation (target)
if target.rotation then
return target.rotation
elseif target.oid then
local o = object_manager.get(target.oid)
return o.rotation
else
print("ACTION could not determine rotation of target", action_handling.to_string_target(target))
return 0
end
end
-- o : object_mangers object
-- returns {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=}
function action_handling.object_to_target (o)
return {oid=o.oid}
end
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (INOUT)
-- target_with_view: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=}
-- returns target with added view necessary
function action_handling.add_view_on_demand (target, target_with_view)
target.viewx = target.viewx or target_with_view.viewx
target.viewy = target.viewy or target_with_view.viewy
return target
end
-- function targets_selected_callback({t0,t1,t2,...})
-- target_selection: eg. {target_selection_type = "ae", range = 10, piercing_number = 3, },
-- start_target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- function target_selection_callback(start_target, target_selection, source_oid, targets_selected_callback)
function action_handling.register_target_selection(name, target_selection_callback)
action_handling.registered_target_selections[name] = target_selection_callback
end
-- effect: see action_definitions.lua, eg. {effect_type = "damage", str = 15},
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- function effect_callback(target, effect, source_oid)
function action_handling.register_effect(name, effect_callback)
action_handling.registered_effects[name] = effect_callback
end
-- effect: see action_definitions.lua, eg. {effect_type = "damage", str = 15},
-- targets: list of {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- function effect_callback(targets, effect, source_oid)
function action_handling.register_effect_multitarget(name, effect_callback)
action_handling.registered_effects_multitarget[name] = effect_callback
end
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
function action_handling.to_string_target(target)
return "oid=" .. (target.oid or "nil") .. " x=" .. (target.x or "nil") .. " y=" .. (target.y or "nil") .. " rotation=" .. (target.rotation or "nil")
end
-- target_selection: eg. {target_selection_type = "ae", range = 10, piercing_number = 3, },
-- function targets_selected_callback({t0,t1,t2,...})
function action_handling.start_target_selection (start_target, target_selection, source_oid, targets_selected_callback)
local t = target_selection.target_selection_type
--~ print("ACTION start_target_selection", t, action_handling.to_string_target(start_target), source_oid)
local ts = action_handling.registered_target_selections[t]
if ts then
ts(start_target, target_selection, source_oid, targets_selected_callback)
else
--print("ACTION start_target_selection", "unknown type")
end
end
-- application: see action_definitions.lua
-- target: {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- source_oid
function action_handling.start (application, target, source_oid)
action_handling.start_target_selection(target, application.target_selection, source_oid, function (targets)
-- target selection finished
-- start each effect on each target
for ke,effect in pairs(application.effects) do
action_handling.start_effect(effect, targets, source_oid)
end
end)
end
-- returns list of ( {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center) )
function action_handling.find_cone_targets (x,y, dx,dy, coneDeltaRadians, range, maxTargetCount)
local maxObjectSize = 0
object_manager.visit(function(oid,o)
local w = o.width or 0
local h = o.heigh or 0
local s = vector.lenFromTo(o.x,o.y,o.x+w,o.y+h)
maxObjectSize = math.max(s, maxObjectSize)
end)
-- 2 * maxObjectSize : start and end object range
local l = object_manager.find_in_sphere(x,y, range + 2 * maxObjectSize)
-- utils.vardump(l)
print(x,y, range)
l = list.process_values(l)
:where(function(f)
return f.targetable
end)
:where(function(f)
return collision.isConeHittingAABB(x,y,dx,dy,range,f,coneDeltaRadians)
end)
:select(function(t)
local xx,yy = action_handling.get_target_position(t)
return {
target=t,
dist=vector.lenFromTo(x,y, xx,yy)
} end)
:orderby(function(a,b) return a.dist < b.dist end)
:take(maxTargetCount)
:select(function(a) return a.target end)
:done()
return l
end
-- returns list of ( {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center) )
function action_handling.find_ae_targets (x,y, range, maxTargetCount)
local maxObjectSize = 0
object_manager.visit(function(oid,o)
local w = o.width or 0
local h = o.heigh or 0
local s = vector.lenFromTo(o.x,o.y,o.x+w,o.y+h)
maxObjectSize = math.max(s, maxObjectSize)
end)
-- 2 * maxObjectSize : start and end object range
local l = object_manager.find_in_sphere(x,y, range + 2 * maxObjectSize)
--~ utils.vardump(l)
--~ print(x,y, range)
l = list.process_values(l)
:where(function(f)
return f.targetable
end)
:where(function(f)
return collision.minDistPointToAABB (x,y, f.x, f.y, f.x+f.width, f.y+f.height) <= range
end)
:select(function(t)
local xx,yy = action_handling.get_target_position(t)
return {
target=t,
dist=vector.lenFromTo(x,y, xx,yy)
} end)
:orderby(function(a,b) return a.dist < b.dist end)
:take(maxTargetCount)
:select(function(a) return a.target end)
:done()
return l
end
-- effect: see action_definitions.lua, eg. {effect_type = "damage", str = 15},
-- targets: list of {oid=,viewx=,viewy=} or {x=,y=,viewx=,viewy=} (x,y is center)
-- source_oid
function action_handling.start_effect (effect, targets, source_oid)
local t = effect.effect_type
--~ print("ACTION start_effect", t, action_handling.to_string_target(targets), source_oid)
if action_handling.registered_effects[t] then
for k,v in pairs(targets) do
action_handling.registered_effects[t](v, effect, source_oid)
end
elseif action_handling.registered_effects_multitarget[t] then
action_handling.registered_effects_multitarget[t](targets, effect, source_oid)
else
print("ACTION start_effect", "unknown type")
end
end
return action_handling