[WIP] Treballant en el jefe. Implementant chase i avoid

This commit is contained in:
2026-04-19 15:56:33 +02:00
parent dab08a0f17
commit 5622392f41
6 changed files with 94 additions and 20 deletions

View File

@@ -1,5 +1,9 @@
pattern={}
PATTERN_RANDOM = "random"
PATTERN_CHASE = "chase"
PATTERN_AVOID = "avoid"
function pattern.new()
return {
node = {}, -- array de punts ( node[1] = punt1,... node[n] = puntN )
@@ -9,6 +13,9 @@ function pattern.new()
action_idx = 1, -- punter a l'acció actual
manual_target = {},
manual_path = {},
poi = {}, -- points of interest
-- target_modes = { PATTERN_RANDOM, PATTERN_CHASE, PATTERN_AVOID },
target_mode = PATTERN_RANDOM,
next_action = pattern.next_action,
next_target = pattern.next_target,
in_target = pattern.in_target,
@@ -20,10 +27,16 @@ function pattern.new()
-- recovery = pattern.noop,
draw = pattern.draw,
collision = pattern.collision,
add_manual_target = pattern.add_manual_target
add_manual_target = pattern.add_manual_target,
set_target_mode = pattern.set_target_mode,
}
end
function pattern:set_target_mode ( mode, poi )
self.poi = poi or {}
self.target_mode = mode
end
function pattern:add_manual_target ( node, path )
self.manual_target = self.node[node]
self.manual_path = path
@@ -171,8 +184,46 @@ function pattern:next_target( target )
-- 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])
if self.poi.x == nil or self.poi.y == nil then
self.target_mode = PATTERN_RANDOM
end
if #self.path[self.path_idx]==1 then
self.target_idx = 1
elseif self.target_mode == PATTERN_RANDOM then
self.target_idx = math.random(#self.path[self.path_idx])
else
-- calcular distancies
local next_target_min = 0
local next_target_max = 0
local min_distance = -1
local max_distance = 0
local d = 0
for k, v in pairs(self.path[self.path_idx]) do
d = distancia( self.node[v.target], self.poi )
if d>max_distance then
max_distance = d
next_target_max = k
end
if min_distance==-1 and d>=0 then min_distance=d+1 end
if d<min_distance and d>=0 then
min_distance = d
next_target_min = k
end
end
-- asignar target segons el mode
if self.target_mode==PATTERN_CHASE then
self.target_idx = next_target_min
elseif self.target_mode==PATTERN_AVOID then
self.target_idx = next_target_max
else
self.target_idx = math.random(#self.path[self.path_idx])
end
end
self.action_idx = 1
end