Files
cacaus-arcade/data/imp2.lua

672 lines
24 KiB
Lua

imp = {}
local DEBUG_FN_NAME = false
function imp.reset()
imp.hit=imp.hit
imp.update=imp.update
imp.draw=imp.draw
imp.hab=abad.hab
imp.enabled=false
imp.counter=500
end
-- function imp.new(_hab, _x, _y)
-- local world_x, world_y = coords.room_to_world(_hab,_x,_y)
-- _fight_modes={"chase","away","super","end_super","shot"}
-- for i,v in ipairs(_fight_modes) do _fight_modes[v] = i end
--
-- return { name="imp",
-- hab=_hab,
-- x=world_x, y=world_y,
-- w=32, h=32,
-- flip=true,
-- frame=28,
-- wait=0,
-- vides=1,
-- energia=21,
-- max_energia=21,
-- falling=0,
-- step=0,
-- hurting=0,
-- jumpfwd=false,
-- step_length=1,
-- vmove_space=1,
-- max_jump_height=24,
-- jump_height=0,
-- jump_in_half_block=0,
-- jump_in_half_block_used = false,
-- -- max_shoot_cooldown=24,
-- shot_power=1,
-- shot_num_shots=1,
-- shoot_cooldown=25,
-- anim={28,29,28,30},
-- bb={x=8,y=0,w=16,h=32},
-- -- scene_intro=false,
-- -- scene_object=false,
-- enabled=true,
-- disable_reason="",
-- can_warp=false,
-- -- Atributs de jefe
-- movement_type="", -- Mode de moviment {pattern, free}
-- action="", -- Acció a realitzar en el "pad"
-- movement="", -- Cap on es mou independentment de si està "flipat"
-- fight_modes=_fight_modes, -- mode de lluita (Llevar?)
-- fight_mode="stop", -- mode de lluita actiu
-- fight_mode_cooldown=1500, -- cicles fins al canvi de mode
-- -- super_cooldown=2000, -- cicles fins a Super actiu
-- super_cooldown=200, -- cicles fins a Super actiu
-- shot_cooldown=100, -- cicles fina a poder disparar
-- hot_points={}, -- llista de punts del mapa on anar
-- target={}, -- següent punt on anar
-- old_target={}, -- target anterior
-- paths={}, -- Llista de "camins" a fer des d'un hot_point
-- path={}, -- Cami actiu
-- path_curr_action=1, -- Index d'acció dins del cami actiu
-- action_event="", -- Event d'acció que ha passat
-- analisis={}, -- Resultat de la informació extreta de l'entorn
-- old_action="-",
-- invencible=false,
-- invencible_time=50,
-- flip_wait=100, -- Temps per a forçar un flip
-- super_wait=200,
-- zoom=1,
-- super_pal={[13]=13,[14]=14,[15]=15},
-- super_fired = false,
-- -- direccio=imp.direccio,
-- draw=imp.draw,
-- hit=imp.hit,
-- do_jump=imp.do_jump,
-- jump=imp.jump,
-- state_jumping=imp.state_jumping,
-- update=imp.state_normal,
-- land=imp.land,
-- advance=imp.advance,
-- -- Funcions de jefe
-- fight=imp.fight, -- Inicialització per al combat. Crea el punts, carrega els paths, etc.
-- -- chase=imp.chase,
-- -- away=imp.away,
-- move=imp.move, -- Determina el següent moviment a fer (dreta, esq, bot, etc...)
-- super=imp.super, -- Activa el mode super poder
-- state_super=imp.state_super, -- Entra en estat super poder
-- shot=imp.shot, -- Dispara
-- do_flip=imp.do_flip, -- Orienta a l'imp cap a l'actor
-- -- mode_controller=imp.mode_controller,
-- reduce_cooldown=imp.reduce_cooldown, -- Decrementa els cooldown
-- reset_fight_mode_cooldown=imp.reset_fight_mode_cooldown, -- Asignar un valor a fight_mode_cooldown (per a mantindre una coherència cada volta que es crida)
-- controller_input=imp.controller_input, -- Traduir l'accio en moviment
-- actualitzar_comportament=imp.actualitzar_comportament, -- "Pensar" que fer
-- analyze_env=imp.analyze_env, -- Analitza l'entorn a vore que està pasant
--
-- path_reset=imp.path_reset, -- Elimina el path actiu
-- path_next_action=imp.path_next_action, -- Següent acció del path actiu
-- path_action=imp.path_action, -- Acció actual del path actiu
--
-- pattern_movement=imp.pattern_movement, -- Pasar a mode de moviment per patró
-- pattern_next_action=imp.pattern_next_action, -- Següent acció en mode pattern
-- pattern_next_target=imp.pattern_next_target, -- Següent target en mode pattern
-- pattern_recovery=imp.pattern_recovery, -- Si per algun motiu perd el mode pattern recuperar-lo
--
-- -- Al afegir pattern_recovery el moviment lliure no te sentit (ni es gasta en este moment), se queda ara per si de cas
-- free_movement=imp.free_movement, -- Pasar a mode de moviment lliure
-- free_next_action=imp.free_next_action, -- Següent acció en mode lliure
-- free_next_target=imp.free_next_target, -- Següent target en mode free
--
-- super_ready=imp.super_ready,
-- super_movement=imp.super_movement,
-- super_next_action=imp.super_next_action,
-- super_next_target=imp.super_next_target,
--
-- next_action=imp.next_action, -- Següent acció en qualsevol mode de moviment
-- next_target=imp.next_target, -- Següent target en qualsevol mode de moviment
--
-- -- imp.enabled=false
-- -- imp.counter=500
-- }
-- end
--
function imp.new(_hab,_x,_y)
local world_x, world_y = coords.room_to_world(_hab,_x,_y)
return {
-- Venia de caco
name="imp",
hab=_hab,
x=world_x, y=world_y,
w=32, h=32,
flip=true,
frame=28,
wait=0,
step=0,
can_warp=true,
warping=false,
shrink=1,
d_shrink=1,
angle=0,
d_angle=15,
hit=imp.hit,
update=imp.update_normal,
draw=imp.draw,
jumpfwd=false,
dying=false,
death_time=40,
anim={28,29,28,30}, -- seqüencia de frames
bb={x=8,y=0,w=16,h=32},
enabled= true,
disable_reason="",
-- Ve de imp1
energia=21,
max_energia=21,
actions={no_action=0, up=1, down=2, left=4, right=8, jump=16, shot=32, super=64},
moods={stop=0, chase=1, avoid=2, neutral=4},
mood=0, --anterior fight_mode
timers={mood=150,target=150, shot=2500, super=20000, flip_wait=50}, --anterior *_cooldown
move_types={free=0, pattern=1, super=2},
move_type=1, -- anterior movement_type
paths={}, -- Llista de "camins" a fer des d'un hot_point
-- paths_active = 0, -- Número de path seleccionat
pattern={}, -- Instruccions per a moviment per patró, anterior path
pattern_point=1, -- anterior path_curr_action
target={}, -- Punt al que anar
shot_target=nil, -- a qui disparar
analisis = {}, -- memoria per a guardar el resultat de l'analisis
hot_points={}, -- llista de punts del mapa on anar
step_length=1,
falling = 0,
action_event="", -- Event d'acció que ha passat
jump_height=0,
max_jump_height=24,
-- funcions
next_frame = imp.staying_next_frame, -- funcio de seleccio del frame
choose_mood = imp.choose_mood,
analyze_env = imp.analyze_env,
choose_target = imp.choose_target,
choose_action = imp.choose_action,
do_flip = imp.do_flip,
colisions = imp.colisions,
fight = imp.fight,
create_hot_points = imp.create_hot_points,
load_pattern_paths= imp.load_pattern_paths,
free_movement = imp.free_movement,
pattern_movement = imp.pattern_movement,
next_action = imp.next_action,
pattern_next_action = imp.pattern_next_action,
pattern_action = imp.pattern_action,
moure = imp.state_normal,
controller_input = imp.controller_input,
reduce_timers = imp.reduce_timers,
reset_mood_timer = imp.reset_mood_timer,
path_reset = imp.path_reset,
next_target = imp.next_target,
free_next_target = imp.free_next_target,
pattern_next_target = imp.pattern_next_target,
super_next_target = imp.super_next_target,
pattern_recovery = imp.pattern_recovery,
pattern_get_next_path = imp.pattern_get_next_path,
do_jump = imp.do_jump,
land = imp.land,
shot = imp.shot,
advance=imp.advance,
_moure=imp._moure, -- temporal debug
}
end
function imp:draw() --OK
local scr_x, scr_y = viewp:screen_coords( self.x, self.y )
-- Modo super
if self.moure==self.state_super then
-- rotar paleta
for col=13,15 do
local newc = self.super_pal[col]
if self.super_wait%6 == 0 then
newc = newc+1
if newc>15 then newc=13 end
end
pal.subpal(col,newc)
self.super_pal[col]=newc
end
-- pintar
draw.surf(96, 32, self.w, self.h,
scr_x, scr_y-self.h*(self.zoom-1),
self.w*self.zoom, self.h*self.zoom,
self.flip)
-- restaurar paleta
for col=13,15 do pal.subpal(col) end
else
-- Modo normal
if self.invencible then
pal.subpal(5,1)
end
draw.surf((self.frame&7)*self.w, (self.frame>>cxr2)*self.h, self.w, self.h, scr_x, scr_y, self.w, self.h, self.flip)
pal.subpal(5)
end
end
function imp:hit() -- OK
if DEBUG_FN_NAME then print("hit") end
if not self.invencible then
self.energia = self.energia -1
if distancia(self, abad)<50 and self.invencible_time<=0 then
self.invencible = true
self.invencible_time = 50
end
end
if self.energia==1 then self.can_warp=true end
if self.energia <= 0 then
self.energia = 0
-- self.enabled = false
print("END BOSS")
if self.can_warp then self.warping=true end
self.shrink=1
self.angle=0
self.dying=true
-- calcular velocitat per al warp
local warp_time = self.death_time/3
self.d_angle = 720 / warp_time; -- 720 = 2 voltes
self.d_shrink = self.shrink / warp_time
end
end
function imp:update_hit()
if not self.enabled then return end
-- Que pasa quan "mor"
end
function imp:update_normal()
print("-------------------------------------------------")
print("")
if self.pattern.target then print("TARGET= "..self.pattern.target) end
if self.pattern.target then print("ACTIONS= "..#self.pattern.actions) end
--self:reduce_timers()
-- Ajustar mood (emocions)
--self:choose_mood()
-- analisis
self:analyze_env()
-- decisio
self:choose_target()
self:choose_action()
-- moviment
self:controller_input()
self:moure()
-- Logica per a selecció de frame
--self:next_frame()
-- colisions en personatges
--self:colisions()
end
-- imp.next_frame()
function imp:walking_next_frame()
-- Selecció de frame
if self.wait==6 then
self.wait=0
self.step=(self.step+1)%4
self.frame=self.anim[self.step+1]
end
-- Orientar
self:do_flip()
-- Aguantar el flip
if self.timers.flip_wait<=0 then
if self.mood==self.moods.chase then
self:do_flip(abad)
else
self:do_flip(self.target)
end
end
end
function imp:jumping_next_frame()
self.frame=30
end
function imp:falling_next_frame()
self.frame=30
end
function imp:staying_next_frame()
self.frame=28
end
function imp:super_next_frame()
self.frame=11
end
-- /imp.next_frame()
-- imp.move()
function walking_move()
-- comprovar colsions en l'entorn en cas de moure
-- moure el personatge
end
function jumping_move()
end
function falling_move()
end
function staying_move()
end
function super_move()
end
-- /imp.move()
function imp:do_flip( actor )
actor = actor or self.target
-- enllaçat a actor
if actor.x<self.x then self.flip=true else self.flip=false end
end
function imp:colisions()
if empty_table(self.target) then return end
if collision(self, self.target) then
end
end
function imp:analyze_env() --OK
if self.mood==self.moods.stop then return end
if DEBUG_FN_NAME then print("analyze_env") end
-- Distancia fins a l'abad
local r = distancia(self,abad)
-- msg_print(10,20,r,true)
self.analisis.can_chase_abad = false
if r<=100 then
self.analisis.can_chase_abad=true
end
-- Els dos punts de cintura per a saber si es pot escalar
local x1_check = self.x+self.bb.x
local x2_check = self.x+self.bb.x+self.bb.w
local y_check = self.y+self.bb.h-4
local tile_type1, tile_code1= arc_check_tile(x1_check,y_check)
local tile_type2, tile_code2= arc_check_tile(x2_check,y_check)
self.analisis.can_climb = false
if tile_type1~=tiletype.void or tile_type2~=tiletype.void then
self.analisis.can_climb = true
end
-- Abad a tir
self.analisis.can_shot = false
if h_collision(self,abad) then self.analisis.can_shot=true end
-- Super preparat
self.analisis.can_super = false
if self.timers.super<=0 then self.analisis.can_super=true end
-- Acces a la zona central (només si el super està preparat)
x1_check = self.x+self.bb.x
x2_check = self.x+self.bb.x+self.bb.w
y_check = self.y+self.bb.h
self.analisis.can_go_altar = false
local hab1, tx1, ty1 = coords.world_to_tile(x1_check, y_check)
local hab2, tx2, ty2 = coords.world_to_tile(x2_check, y_check)
if (hab1==44 and tx1==6 and ty1==4) or (hab2==44 and tx2==7 and ty2==4)
or (hab1==45 and tx1==7 and ty1==3) or (hab2==45 and tx2==8 and ty2==3)then
if self.analisis.can_super then self.analisis.can_go_altar = true end
end
-- Cau al següent moviment?
self.analisis.going_to_fall = false
local step_length = self.step_length
if self.movement==self.actions.left then step_length = -step_length end
local tile_type1, tile_code1= arc_check_tile(x1_check+self.step_length,y_check)
local tile_type2, tile_code2= arc_check_tile(x2_check+self.step_length,y_check)
if tile_type1==tiletype.void and tile_type2==tiletype.void then
self.analisis.going_to_fall = true
end
-- Ha arribat a destí
self.analisis.target_reached = false
if not empty_table(self.target) then
-- Target reached
if collision(self, self.target) then self.analisis.target_reached=true end
end
end
-- ///////////////////////////////////////////////////////////
function imp:path_reset() --OK
if DEBUG_FN_NAME then print("path_reset") end
self.path={}
self.path_point=0;
end
function imp:reset_target_timer( value ) -- OK
if DEBUG_FN_NAME then print("reset_target_timer") end
value = value or 150
self.timers.target = value
return value
end
function imp:pattern_get_next_path()
if not DEBUG_FN_NAME then print("pattern_get_next_path") end
-- obtindre uno dels possibles camins des del target en el que està
-- Si el camí s'ha acabat estarà en el punt self.path.target
local next_path_idx = math.random(#self.paths[self.pattern.target])
self.pattern = self.paths[self.pattern.target][next_path_idx]
self.pattern_point = 1
end
function imp:create_hot_points() --OK
self.hot_points[1] = point.new(44, 5, 1, 0, 8,"P1")
self.hot_points[2] = point.new(45, 8, 1, 0, 8,"P2")
self.hot_points[3] = point.new(44,12, 5, 8, 0,"P3")
self.hot_points[4] = point.new(54, 5, 4, -8, 0,"P4")
self.hot_points[5] = point.new(54,12, 4, 8, 0,"P5")
self.hot_points[6] = point.new(55, 8, 4, 8, 0,"P6")
self.hot_points[7] = point.new(55, 8, 0, 0, 0,"P7")
self.hot_points[8] = point.new(54, 5, 0, 0, 0,"P8")
self.hot_points[9] = point.new(54, 9, 1, 0, 0,"P9")
self.hot_points[10] = point.new(55,4, 1, 0, 0,"P10")
-- self.hot_points[11] = abad
-- self.hot_points[11] = point.new(45, 1, 5, 8, 0,"altar-R")
-- self.hot_points[12] = point.new(44,11, 5, 0, 0,"altar-L")
end
function imp:load_pattern_paths() --OK
self.paths[1] = {
{target=3, actions= {{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="target"}}},
{target=4, actions= {{action="right" , event="land"},
{action="left" , event="target"}}},
{target=4, actions= {{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{target=10, actions= {{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="target"}}},
{target=6, actions= {{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="target"}}},
{target=5, actions= {{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="land"},
{action="left" , event="target"}}},
{target=5, actions= {{action="right" , event="target"}}}
}
self.paths[2] = {
{target=3, actions= {{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{target=6, actions= {{action="left" , event="land"},
{action="right" , event="target"}}},
{target=6, actions= {{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="target"}}},
{target=9, actions= {{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{target=4, actions= {{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{target=5, actions= {{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="land"},
{action="right" , event="target"}}},
{target=5, actions= {{action="left" , event="target"}}}
}
self.paths[3] = {
{target=1, actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="prefall"},
{action="jump" , event="target"}}},
{target=2, actions={{action="right" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="prefall"},
{action="jump" , event="target"}}},
{target=4, actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="right" , event="land"},
{action="left" , event="target"}}},
{target=5, actions={{action="left" , event="land"},
{action="right" , event="target"}}},
{target=6, actions={{action="left" , event="land"},
{action="right" , event="target"}}}
}
self.paths[4] = {
{target=1, actions={{action="jump" , event="target"}}},
{target=5, actions={{action="right", event="target"}}},
{target=6, actions={{action="right", event="target"}}},
{target=8, actions={{action="jump" , event="target"}}},
}
self.paths[5] = {
{target=4, actions={{action="left" , event="target"}}},
{target=6, actions={{action="right", event="target"}}}
}
self.paths[6] = {
{target=2, actions={{action="jump", event="target"}}},
{target=5, actions={{action="left", event="target"}}},
{target=7, actions={{action="jump", event="target"}}},
{target=4, actions={{action="left", event="target"}}}
}
self.paths[7] = {
{target=5, actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{target=10,actions={{action="left" , event="prefall"},
{action="jumpfwd", event="land"},
{action="left" , event="target"}}},
{target=2, actions={{action="jump" , event="target"}}}
}
self.paths[8] = {
{target=5, actions={{action="right" , event="prefall"},
{action="jumpfwd" , event="land"},
{action="right" , event="target"}}},
{target=9, actions={{action="right" , event="prefall"},
{action="jumpfwd" , event="land"},
{action="right" , event="target"}}},
{target=1, actions={{action="jump" , event="target"}}}
}
self.paths[9] = {
{target=5, actions={{action="right" , event="target"}}},
{target=1, actions={{action="left" , event="prefall"},
{action="jumpfwd" , event="land"},
{action="left" , event="prefall"},
{action="jump" , event="target"}}},
{target=4, actions={{action="left" , event="target"}}}
}
self.paths[10] = {
{target=5, actions={{action="left" , event="target"}}},
{target=2, actions={{action="right" , event="prefall"},
{action="jumpfwd" , event="land"},
{action="right" , event="prefall"},
{action="jump" , event="target"}}},
{target=6, actions={{action="right" , event="target"}}}
}
self.paths[11] = {
{target=4, actions={{action="left", event="target"}}}
}
self.paths[12] = {
{target=6, actions={{action="right", event="target"}}}
}
end
function imp:fight() -- OK
if DEBUG_FN_NAME then print("fight") end
self:create_hot_points()
self:load_pattern_paths()
self.target = self.hot_points[6]
self.pattern = {target=6, actions={{action="right",event="target"}}}
print("imp2.lua -> #self.pattern.actions= "..#self.pattern.actions)
self.shot_target = abad
self.mood=self.moods.chase
self:pattern_movement()
end
function imp:reduce_timers()
if self.mood==self.moods.stop then return end
if DEBUG_FN_NAME then print("reduce_timers") end
for key, val in pairs(self.timers) do
self.timers[key] = val - 1
if (val-1)<0 then self.timers[key] = 0 end
end
end
function imp:shot(actor)
if DEBUG_FN_NAME then print("shot") end
-- self.fight_mode = self.fight_modes["shot"]
-- self:reset_fight_mode_cooldown(1)
self:do_flip(abad)
self.flip_wait=100
-- print("FIREBALL!!")
local flip = false
local x_ini = self.x+self.bb.x+self.bb.w+4
if self.x>abad.x then
flip=true
x_ini = self.x-4
end
local hab, tx, ty, off_x, off_y = coords.world_to_tile(x_ini,self.y+(self.h/4))
fireball.init(hab, tx, ty, flip, off_x, off_y)
sound.play(audio_hit)
self.shot_cooldown = 100
end
-- function imp:actualitzar_comportament()
-- if DEBUG_FN_NAME then print("actualitzar_comportament") end
-- self:reduce_cooldown()
-- if self.invencible_time<=0 then self.invencible=false end
-- -- if self.update==imp.state_jumping or self.update==imp.state_falling then
-- -- -- self:think()
-- -- -- self:move()
-- -- end
-- self:analyze_env()
-- if self.moure==imp.state_walking or self.update==imp.state_normal then
-- self:move()
-- end
-- self:controller_input()
-- end
require "imp_mood"
require "imp_action"
require "imp_target"
require "imp_movement"
require "imp_states"
require "imp_super"