[WIP] Treballant en el jefe. Separat el 'motor' de moviment per patró
This commit is contained in:
152
data/pattern.lua
Normal file
152
data/pattern.lua
Normal file
@@ -0,0 +1,152 @@
|
||||
pattern={}
|
||||
|
||||
function pattern.new()
|
||||
return {
|
||||
node = {}, -- array de punts ( node[1] = punt1,... node[n] = puntN )
|
||||
path = {}, -- array de camins entre nodes ( path[1] = { {target=2, actions={ {action="right", until="land"} ...} } ...} )
|
||||
path_idx = 0, -- punter a path actiu (coincidix en el node)
|
||||
target_idx = 1, -- punter a target actiu, no al número de target si no a l'index dins del path
|
||||
action_idx = 1, -- punter a l'acció actual
|
||||
manual_target = {},
|
||||
manual_path = {},
|
||||
next_action = pattern.next_action,
|
||||
next_target = pattern.next_target,
|
||||
in_target = pattern.in_target,
|
||||
target = pattern.target,
|
||||
target_node = pattern.target_node,
|
||||
action = pattern.action,
|
||||
event = pattern.event,
|
||||
actions = pattern.actions,
|
||||
recovery = pattern.recovery
|
||||
}
|
||||
end
|
||||
|
||||
-- Necessite saber quina estrategia aplicar abans de moure al següent target
|
||||
-- ara aleatori
|
||||
|
||||
function pattern:origin()
|
||||
return self.node[self.path_idx]
|
||||
end
|
||||
|
||||
function pattern:origin_num ()
|
||||
return self.path_idx
|
||||
end
|
||||
|
||||
function pattern:set_path ( num )
|
||||
self.path_idx = num
|
||||
end
|
||||
|
||||
function pattern:get_path ( num )
|
||||
return self.path[num]
|
||||
end
|
||||
|
||||
|
||||
-- [OK] Torna el id del target actual
|
||||
-- function pattern:get_target ( )
|
||||
-- return self.path[self.path_idx][target_idx].target
|
||||
-- end
|
||||
function pattern:target()
|
||||
print("pattern:target 1")
|
||||
if not empty_table(self.manual_path) then
|
||||
print("pattern:target 2 a")
|
||||
return self.manual_path.target
|
||||
else
|
||||
print("pattern:target 2 b")
|
||||
return self.path[self.path_idx][self.target_idx].target
|
||||
end
|
||||
end
|
||||
|
||||
-- [OK] Torna la llista d'accions actual
|
||||
-- function pattern:get_actions ( )
|
||||
-- return self.path[self.path_idx][target_idx].actions
|
||||
-- end
|
||||
function pattern:actions()
|
||||
if not empty_table(self.manual_path) then
|
||||
return self.manual_path.actions
|
||||
else
|
||||
return self.path[self.path_idx][self.target_idx].actions
|
||||
end
|
||||
end
|
||||
|
||||
-- [OK] Torna l'acció actual
|
||||
-- function pattern:get_action ( )
|
||||
-- return self.path[self.path_idx][target_idx].actions[self.action_idx]
|
||||
-- end
|
||||
function pattern:action()
|
||||
if not empty_table(self.manual_path) then
|
||||
return self.manual_path.actions[self.action_idx].action
|
||||
else
|
||||
return self.path[self.path_idx][self.target_idx].actions[self.action_idx].action
|
||||
end
|
||||
end
|
||||
|
||||
-- [OK] Torna l'event actual
|
||||
function pattern:event()
|
||||
if not empty_table(self.manual_path) then
|
||||
return self.manual_path.actions[self.action_idx].event
|
||||
else
|
||||
return self.path[self.path_idx][self.target_idx].actions[self.action_idx].event
|
||||
end
|
||||
end
|
||||
|
||||
-- [OK] Obtindre la següent acció a fer
|
||||
-- function pattern:next()
|
||||
-- self.action_idx = self.action_idx+1
|
||||
-- if self.action_idx>#self.path[self.path_idx][self.target_idx].actions then
|
||||
-- pattern:next_target() -- pasar per paràmetre o no
|
||||
-- end
|
||||
-- return pattern:action()
|
||||
-- end
|
||||
function pattern:next_action ( )
|
||||
print("pattern:next_action 1")
|
||||
local num_actions = 0
|
||||
-- index a la següent acció
|
||||
self.action_idx = self.action_idx+1
|
||||
-- comprovar si no queden accions disponibles
|
||||
if not empty_table(self.manual_path) then
|
||||
num_actions = #self.manual_path.actions
|
||||
else
|
||||
num_actions = #self.path[self.path_idx][self.target_idx].actions
|
||||
end
|
||||
print("pattern:next_action 2")
|
||||
if self.action_idx>num_actions then
|
||||
-- avançar el target
|
||||
self:next_target() -- pasar per paràmetre o no
|
||||
end
|
||||
print("pattern:next_action 3")
|
||||
-- Tornar l'acció
|
||||
return self:action()
|
||||
end
|
||||
|
||||
-- [OK] Canvia al següent target segons el path o al que se li pase
|
||||
function pattern:next_target( target )
|
||||
print("pattern:next_target 1")
|
||||
target = target or self:target()
|
||||
print("pattern:next_target 2")
|
||||
-- Si hi ha una entrada manual eliminar-la
|
||||
if not empty_table(self.manual_path) then
|
||||
self.manual_path = {}
|
||||
self.manual_target = {}
|
||||
end
|
||||
print("pattern:next_target 3")
|
||||
|
||||
-- target id
|
||||
self.path_idx = target
|
||||
-- tindre en compte l'estrategia per a triar el següent target des del path actual per ara aleatori
|
||||
self.target_idx = math.random(#self.path[self.path_idx])
|
||||
self.action_idx = 1
|
||||
print("pattern:next_target 4")
|
||||
end
|
||||
|
||||
-- [OK] Torna el node del target actual
|
||||
function pattern:target_node()
|
||||
return self.node[self:target()]
|
||||
end
|
||||
|
||||
-- Comprova si point ha arribat al target (Afegir precissio per si volem que estiga nomes tocar, dins o abans d'eixir?)
|
||||
function pattern:in_target( area )
|
||||
return collision( area, self:target_node() )
|
||||
end
|
||||
|
||||
function pattern:recovery( point, target)
|
||||
end
|
||||
Reference in New Issue
Block a user