From 87e2a134a8b20102cd195255092f57c33350470e Mon Sep 17 00:00:00 2001 From: JailGamer Date: Thu, 9 Apr 2026 23:12:17 +0200 Subject: [PATCH] =?UTF-8?q?[WIP]=20Treballant=20en=20el=20jefe.=20Recupera?= =?UTF-8?q?r=20informaci=C3=B3=20de=20l'entorn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/helpers.lua | 11 +++ data/imp.lua | 186 ++++++++++++++++++++++++++++++++++++----------- data/point.lua | 8 +- 3 files changed, 158 insertions(+), 47 deletions(-) diff --git a/data/helpers.lua b/data/helpers.lua index 02cf390..46a9903 100644 --- a/data/helpers.lua +++ b/data/helpers.lua @@ -18,6 +18,17 @@ function half_collision(a, b) and (a.y+a.bb.y <= b.y+b.bb.y+b.bb.h)) end +function v_collision(a, b) + return ((a.x+a.bb.x+a.bb.w >= b.x+b.bb.x) + and (a.x+a.bb.x <= b.x+b.bb.x+b.bb.w) ) + +end + +function h_collision(a, b) + return ((a.y+a.bb.y+a.bb.h >= b.y+b.bb.y) + and (a.y+a.bb.y <= b.y+b.bb.y+b.bb.h)) +end + function remove_actor(actor) for index, value in pairs(actors) do if value == actor then diff --git a/data/imp.lua b/data/imp.lua index f43bde5..045707a 100644 --- a/data/imp.lua +++ b/data/imp.lua @@ -36,6 +36,7 @@ function imp.new(_hab, _x, _y) local world_x, world_y = coords.room_to_world(_hab,_x,_y) _fight_modes={"chase","away","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, @@ -100,6 +101,8 @@ function imp.new(_hab, _x, _y) set_fight_mode=imp.set_fight_mode, think=imp.think, controller_input=imp.controller_input, + ia=imp.ia, + analyze_env=imp.analyze_env, -- imp.enabled=false -- imp.counter=500 } @@ -107,15 +110,23 @@ end function imp:fight() print("FIGHTING MODE") - table.insert(self.hot_points, point.new(54,5,4,12,8,"P1")) - table.insert(self.hot_points, point.new(44,5,3,12,8,"P2")) - table.insert(self.hot_points, point.new(55,7,4,12,8,"P3")) - table.insert(self.hot_points, point.new(45,7,3,12,8,"P4")) - table.insert(self.hot_points, point.new(44,12,5,12,8,"P5")) - table.insert(self.hot_points, point.new(54,12,4,12,8,"P6")) + -- table.insert(self.hot_points, point.new(54,5,4,12,8,"P1")) + -- table.insert(self.hot_points, point.new(44,5,3,12,8,"P2")) + -- table.insert(self.hot_points, point.new(55,7,4,12,8,"P3")) + -- table.insert(self.hot_points, point.new(45,7,3,12,8,"P4")) + -- table.insert(self.hot_points, point.new(44,12,5,12,8,"P5")) + -- table.insert(self.hot_points, point.new(54,12,4,12,8,"P6")) + + table.insert(self.hot_points, point.new(54, 5, 4, 0, 0,"P1")) + table.insert(self.hot_points, point.new(44, 5, 3, 0, 0,"P2")) + table.insert(self.hot_points, point.new(55, 8, 4, 0, 0,"P3")) + table.insert(self.hot_points, point.new(45, 8, 3, 0, 0,"P4")) + table.insert(self.hot_points, point.new(44,12, 5, 0, 0,"P5")) + table.insert(self.hot_points, point.new(54,12, 4, 0, 0,"P6")) table.insert(self.hot_points, abad); - self.mode="chase" self.target = self.hot_points[3] + + self.mode="chase" end function imp:draw() @@ -149,9 +160,108 @@ function imp:do_jump ( jumpfwd ) self.action="" end +function imp:analyze_env() + + -- Distancia fins els punts + local scr_x, scr_y = viewp:screen_coords(self.x, self.y) + -- for _, p in pairs(self.hot_points) do + -- local scr_x1, scr_y1 = viewp:screen_coords(p.x, p.y) + -- local color = 2 + -- if self.target == p then color = 4 end + -- draw.line(scr_x, scr_y, scr_x1, scr_y1, color) + -- + -- dx = (self.x-p.x) + -- dy = (self.y-p.y) + -- d = math.sqrt(dx*dx+dy*dy) + -- end + + -- Distancia fins a l'abad + dx = (self.x-abad.x) + dy = (self.y-abad.y) + r = math.sqrt(dx*dx+dy*dy) + -- msg_print(10,20,r,true) + local can_chase_abad = false + if r<=100 then + -- draw.circ(scr_x+self.bb.w/2,scr_y+self.bb.h/2,r,2) + 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) + local can_climb = false + if tile_type1~=tiletype.void or tile_type2~=tiletype.void then + can_climb = true + end + + -- Abad a tir + local can_shot = false + if h_collision(self,abad) then can_shot=true end + + -- Super preparat + local can_super = false + if self.super_cooldown<=0 then can_super=true end + + -- Acces a la zona central + x1_check = self.x+self.bb.x + x2_check = self.x+self.bb.x+self.bb.w + y_check = self.y+self.bb.h + local 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 + can_go_altar = true + end + + -- Cau al següent moviment + local going_to_fall = false + local tile_type1, tile_code1= arc_check_tile(x1_check,y_check) + local tile_type2, tile_code2= arc_check_tile(x2_check,y_check) + if tile_type1==tiletype.void and tile_type2==tiletype.void then + going_to_fall = true + end + + local target_reached = false + local target_aligned = false + if #self.target>0 then + -- Target reached + if collision(self, self.target) then target_reached=true end + + -- Target aligned + if half_collision(self, self.target) then target_aligned=true end + end + -- if can_climb then print("Climb!") end + -- if can_shot then print("Shot!") end + -- if can_super then print("Super!") end + -- if can_go_altar then + -- -- msg_print(10,20,"Altar READY!", true) + -- print("Altar!") + -- end + -- msg_print(10,40,hab1..", "..tx1..", "..ty1, true) + -- msg_print(10,60,hab2..", "..tx2..", "..ty2, true) +end + +function imp:ia() + self:reduce_cooldown() + -- if self.update==imp.state_jumping or self.update==imp.state_falling then + -- -- self:set_fight_mode() + -- -- self:think() + -- end + self:analyze_env() + if self.update==imp.state_walking or self.update==imp.state_normal then + self:set_fight_mode() + self:think() + end + self:controller_input() +end + + function imp:state_jumping() -- msg_print(0,0,"state_jumping",true) - self:reduce_cooldown() -- ?? self.wait=self.wait+1 self.wait=0 @@ -169,9 +279,7 @@ function imp:state_jumping() -- cap endavant? if self.jumpfwd then self:advance() end - -- self:set_fight_mode() - -- self:think() - self:controller_input() + self:ia() end function imp:jump() @@ -221,8 +329,7 @@ function imp:jump() end function imp:state_walking() -msg_print(16,48,"state_walking",true) - self:reduce_cooldown() +-- msg_print(16,48,"state_walking",true) -- Limitar la velocitat de moviment self.wait=self.wait+1 @@ -248,22 +355,17 @@ msg_print(16,48,"state_walking",true) end self:advance() - - self:set_fight_mode() - self:think() - self:controller_input() + self:ia() end function imp:state_falling() -msg_print(16,16,"state_falling",true) - self:reduce_cooldown() +-- msg_print(16,16,"state_falling",true) self.frame=30 self.wait=self.wait+1 -- Si toca terra canviar el mode if self:land() then - print("landing") self.update=imp.state_normal return end @@ -275,14 +377,11 @@ msg_print(16,16,"state_falling",true) -- Caiguent cap endavant? if self.jumpfwd then self:advance() end - - -- self:set_fight_mode() - -- self:think() - self:controller_input() + self:ia() end function imp:land () -msg_print(16,32,"land",true) +-- msg_print(16,32,"land",true) -- Els dos punts de baix de l'abad local x1_check = self.x+self.bb.x local x2_check = self.x+self.bb.x+self.bb.w @@ -341,27 +440,23 @@ end -- Controlador principal del personatge function imp:state_normal() -- msg_print(0,0,"state_normal",true) - self:reduce_cooldown() - self.frame=28 self.wait=0 self.step=0 self.jumpfwd=false self.jump_height = 0 - self:set_fight_mode() - self:think() - self:controller_input() + self:ia() end -function imp:chase() - self.mode = self.fight_modes["chase"] - self.mode_cooldown = 150+math.random(50)-1 -end +-- function imp:chase() +-- self.mode = self.fight_modes["chase"] +-- self.mode_cooldown = 150+math.random(50)-1 +-- end -function imp:away() - self.mode = self.fight_modes["away"] - self.mode_cooldown = 150+math.random(50)-1 -end +-- function imp:away() +-- self.mode = self.fight_modes["away"] +-- self.mode_cooldown = 150+math.random(50)-1 +-- end function imp:state_super() self.mode = self.fight_modes["super"] @@ -416,7 +511,6 @@ end -- traduir a acció de pad ------------------------------- function imp:think( ) - local scr_x, scr_y = viewp:screen_coords( self.x+self.bb.x, self.y ) -- local check_x = -- local check_y = local action = "stay" @@ -432,19 +526,23 @@ function imp:think( ) -- Els dos punts de baix de l'abad 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-2 + 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) - draw.rect(scr_x+self.bb.x,scr_y+self.bb.h-1,16,16,2) + + local scr_x1, scr_y = viewp:screen_coords( x1_check, y_check ) + local scr_x2, scr_y = viewp:screen_coords( x2_check, y_check ) + draw.rect(scr_x1,scr_y,scr_x2-scr_x1,8,2) if collision(self, self.target) then print("Next target "..tile_type1.." "..tile_code1) + print("Next target "..tile_type2.." "..tile_code2) self.mode_cooldown = 0 elseif half_collision(self, self.target) and tile_type1==tiletype.void and tile_type2==tiletype.void then -- and (self.y>self.target.y or self.y