function imp:_moure( foo, name ) name = name or "anonymous" if not foo then print_dbg(name.." not found") else print_dbg(name) end self.moure = foo end function noop () end ------------------------------- -- ------------------------------- function imp:reduce_timers() if self.mood==self.moods.stop then return end if DEBUG_FN_NAME then print_dbg("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:analyze_env() --OK if self.mood==self.moods.stop then return end if DEBUG_FN_NAME then print_dbg("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.pattern:target_node()) then -- Target reached if self.pattern:in_target(self) then self.analisis.target_reached=true end -- if collision(self, self.pattern:target_node()) then self.analisis.target_reached=true end end end ------------------------------- -- ------------------------------- function imp:choose_action() -- antic imp:move if self.mood==self.moods.stop then return self.mood end if DEBUG_FN_NAME then print_dbg("choose_action "..self.mood) end local next_action = self.actions.no_action -- Si no te ganes de fer res, no moure if self.mood==self.moods.stop then return self.actions.no_action end if self.analisis.going_to_fall then self.action_event="prefall" end if self.analisis.target_reached then self.action_event="target" end if self.analisis.can_go_altar then self:super_ready() end -- if self.fight_mode == self.fight_modes["super"] then self.action_event="super" end -- if self.fight_mode == self.fight_modes["end_super"] then self.action_event="end_super" end next_action = self:next_action() if self.analisis.target_reached and next_action=="super" then -- print("Super READY!") self.action_event = "super_ready" -- next_action = self:next_action() self:super() end if self.old_action~=next_action then self.old_action=next_action -- print(self.old_action) end self.action = next_action return next_action end ------------------------------- -- imp.controller_input() -- -- Traduir a una entrada de pad ------------------------------- function imp:controller_input() if DEBUG_FN_NAME then print_dbg("controller_input") end --To Do: JumpFWD if (self.moure~=imp.state_falling and self.moure~=imp.state_jumping and self.moure~=imp.state_super) and (self.action == self.actions.right or self.action == self.actions.left) then self.movement = self.action -- self.moure=imp.state_walking self:_moure(imp.state_walking, "state_walking (movement)") elseif (self.moure~=imp.state_falling and self.moure~=imp.state_jumping and self.moure~=imp.state_super) and self.action == self.actions.jump then self:do_jump() elseif (self.moure~=imp.state_falling and self.moure~=imp.state_jumping and self.moure~=imp.state_super) and self.action == "jumpfwd" then self:do_jump(true) elseif self.action == self.actions.shot then self:shot() elseif (self.moure~=imp.state_falling and self.moure~=imp.state_jumping and self.moure~=imp.state_super) and self.action == self.actions.super then self.moure=imp.state_super end self.action = self.actions.no_action end ------------------------------- -- ------------------------------- function imp:fight() -- OK if DEBUG_FN_NAME then print_dbg("fight") end self:create_hot_points() self:load_pattern_paths() self.pattern.manual_target = self.pattern.node[6] self.pattern.manual_path = {target=6, actions={{action="right",event="target"}}} self.shot_target = abad self.mood=self.moods.chase self:pattern_movement() end ------------------------------- -- ------------------------------- function imp:create_hot_points() --OK self.pattern.node[1] = point.new(44, 5, 1, 0, 8,"P1") self.pattern.node[2] = point.new(45, 8, 1, 0, 8,"P2") self.pattern.node[3] = point.new(44,12, 5, 8, 0,"P3") self.pattern.node[4] = point.new(54, 5, 4, -8, 0,"P4") self.pattern.node[5] = point.new(54,12, 4, 8, 0,"P5") self.pattern.node[6] = point.new(55, 8, 4, 8, 0,"P6") self.pattern.node[7] = point.new(55, 8, 0, 0, 0,"P7") self.pattern.node[8] = point.new(54, 5, 0, 0, 0,"P8") self.pattern.node[9] = point.new(54, 9, 1, 0, 0,"P9") self.pattern.node[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.pattern.path[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.pattern.path[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.pattern.path[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.pattern.path[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.pattern.path[5] = { {target=4, actions={{action="left" , event="target"}}}, {target=6, actions={{action="right", event="target"}}} } self.pattern.path[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.pattern.path[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.pattern.path[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.pattern.path[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.pattern.path[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.pattern.path[11] = { {target=4, actions={{action="left", event="target"}}} } self.pattern.path[12] = { {target=6, actions={{action="right", event="target"}}} } end ------------------------------------------------------------------- -- // PATTERN MOVEMENT function imp:pattern_movement() --OK if DEBUG_FN_NAME then print_dbg("pattern_movement") end self.movement_type = "pattern" -- print("Pattern") end ------------------------------------------------------------------- -- // ACTION function imp:next_action() local next_action = self.actions.no_action -- Si el blanc està a tir disparar if self.timers.shot<=0 then if h_collision(self, self.shot_target) then next_action = self.actions.shot end end -- Resta d'accions if next_action==self.actions.no_action then if self.move_type==self.move_types.free then -- next_action = self:free_next_action() elseif self.move_type==self.move_types.pattern then if self.pattern:in_target(self) then next_action=self.pattern:next_action() print("TARGET => "..next_action) else next_action=self.pattern:action() print("ON WAY => "..next_action) end elseif self.move_type==self.move_types.super then -- end end -- next_action es text, traduir return self.actions[next_action] end function imp:do_jump ( jumpfwd ) if DEBUG_FN_NAME then print_dbg("") end -- Inicialització de fer el salt jumpfwd = jumpfwd or false self.jump_height = 0 -- self.moure=imp.state_jumping self:_moure(imp.state_jumping,"state_jumping (states)") self.step=0 self.jumpfwd=jumpfwd self.action="" end function imp:state_jumping() if self.mood==self.moods.stop then return self.mood end if DEBUG_FN_NAME then print_dbg("state_jumping") end -- ?? self.wait=self.wait+1 self.wait=0 self.next_frame=imp.jumping_next_frame -- Pujar o caure if self.jump_height0 then self.y=self.y-vspace self.jump_in_half_block = self.jump_in_half_block-1 self.jump_in_half_block_used = true end end end -- Registrar el desplaçament self.jump_height = self.jump_height+1 end function imp:advance() if DEBUG_FN_NAME then print_dbg("advance") end local step_length=self.step_length; --lo que avança el imp cada pas local limit=tiletype.block if self.moure~=imp.state_walking then limit=tiletype.half end local x_check = self.x+self.bb.x+self.bb.w+step_length -- if self.flip then -- step_length = -step_length -- x_check = self.x+self.bb.x+step_length -- end -- self.action=="right" -- if self.action=="left" then -- print("ADVANCE => "..self.movement) if self.movement==self.actions.left then step_length = -step_length x_check = self.x+self.bb.x+step_length end local y_check = self.y+self.bb.h-4 if arc_check_tile(x_check, y_check)=tiletype.half or tile2_hit>=tiletype.half -- Encara que siga un tile de piso s'ha de comprovar que -- la y es un múltiple de l'alt dels tiles local over_tile = (y_check & 0xF) == 0 local can_land = floor_tile and over_tile if can_land then self.jump_in_half_block_used = false self.jump_height = 0 self.action_event = "land" end if can_land then print_dbg ("LANDED") end return can_land end function imp:state_walking() if self.mood==self.moods.stop then return end if not DEBUG_FN_NAME then print("state_walking") end -- msg_print(16,48,"state_walking",true) -- Limitar la velocitat de moviment self.wait=self.wait+1 -- Funció de selecció de frame self.next_frame = imp.walking_next_frame -- Comprovar dos punts de contacte del personatge en el piso a vore si cau local x1_check = self.x+self.bb.x local x2_check = x1_check+self.bb.w local y_check = self.y+self.bb.h; -- base del personatge local tile1 = arc_check_tile(x1_check,y_check) local tile2 = arc_check_tile(x2_check,y_check) if tile1==tiletype.void and tile2==tiletype.void then -- si no hi ha piso, caure self.moure=imp.state_falling self:_moure(imp.state_falling, "state_falling (states-walking)") return end self:advance() end function imp:state_normal() if DEBUG_FN_NAME then print_dbg("state_normal") end self.frame=28 self.wait=0 self.step=0 self.jumpfwd=false self.jump_height = 0 self.next_frame=imp.staying_next_frame 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.pattern:target_node()) 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 function imp:do_flip( actor ) actor = actor or self.pattern:target_node() -- enllaçat a actor if actor.x