def update(self, data, common_data, dt): speed = 0.3 # if doing something that can't be interrupted then countdown to end of it if self.coolDown(data, dt): pass else: if data.health <= 0: self.setState(data, common_data, eStates.dead) return self.setState(data, common_data, eStates.stationary) if rand_num(10) == 0: # self.setState(data, common_data, eStates.stationary) data.vel = Vec3(0, 0, 0) data.cooldown = rand_num(5) / 8.0 + 0.3 else: # chase hero if data.target_cool <= 0: data.target = Vec3(rand_num(600), rand_num(200), rand_num(200)) data.target_cool = rand_num(20) else: data.target_cool -= 1 if (data.target.x < common_data.pos.x): # self.setState(data, common_data, eStates.runLeft) data.vel = Vec3(-speed, 0, 0) data.facing = eDirections.left else: # self.setState(data, common_data, eStates.runRight) data.vel = Vec3(speed, 0, 0) data.facing = eDirections.right if (data.target.z < common_data.pos.z): data.vel.z = -speed else: data.vel.z = speed if common_data.pos.distSq( Vec3(data.target.x, data.target.y, common_data.pos.z)) < 800: data.vel.y = 0 # drop on target elif (common_data.pos.z < 80 + rand_num(200)) and (data.vel.z < 3): data.vel.y += 2 + rand_num(5) # otherwise flap self.setState(data, common_data, eStates.stationary, force_new_state=True) data.cooldown = 0.2 if common_data.pos.y > 0: px_controller.friction(data.vel, 0.01) else: px_controller.friction(data.vel, 0.1) px_controller.basic_gravity(data.vel) px_controller.basic_physics(common_data.pos, data.vel) restrictToArena(common_data.pos, data.vel)
def update(self, data, common_data, dt): return if common_data.pos.y>100: data.vel.y = -0.2 elif common_data.pos.y<20: data.vel.y = 0.2 px_controller.basic_physics(common_data.pos, data.vel)
def update(self, data, common_data, dt): speed = 0.3 # if doing something that can't be interrupted then countdown to end of it if self.coolDown(data, dt): pass else: if data.health <= 0: self.setState(data, common_data, px_entity.eStates.dead) return self.setState(data, common_data, px_entity.eStates.stationary) if rand_num(10)==0: # self.setState(data, common_data, eStates.stationary) data.vel = Vec3(0,0,0) data.cooldown = rand_num(10)/8.0+0.3 else: # chase hero target = common_data.game.requestTarget(common_data.pos) if(target.x<common_data.pos.x): # self.setState(data, common_data, eStates.runLeft) data.vel = Vec3(-speed, 0, 0) data.facing = px_entity.eDirections.left else: # self.setState(data, common_data, eStates.runRight) data.vel = Vec3(speed, 0, 0) data.facing = px_entity.eDirections.right if(target.z<common_data.pos.z): data.vel.z = -speed else: data.vel.z = speed if common_data.pos.distSq(Vec3(target.x,target.y,common_data.pos.y))<800: data.vel.y = 0 # drop on target elif (common_data.pos.z<80) and (data.vel.z<3): data.vel.y += 2 # otherwise flap # common_data.entity.graphics.startAnim(data = common_data.entity.graphics_data) self.setState(data, common_data, px_entity.eStates.stationary, force_new_state=True) common_data.entity.sounds.playEvent(data, common_data, eEvents.flap) data.cooldown = 0.2 if common_data.pos.y>0: px_controller.friction(data.vel, 0.01) else: px_controller.friction(data.vel, 0.1) px_controller.basic_gravity(data.vel) px_controller.basic_physics(common_data.pos, data.vel) background.restrictToArena(common_data.pos, data.vel)
def update(self, data, common_data, dt): speed = 0.3 # if doing something that can't be interrupted then countdown to end of it if self.coolDown(data, dt): pass else: if data.health <= 0: self.setState(data, common_data, eStates.dead) return self.setState(data, common_data, eStates.stationary) if rand_num(10) == 0: # self.setState(data, common_data, eStates.stationary) data.vel = Vec3(0, 0, 0) data.cooldown = rand_num(5) / 10.0 + 0.1 else: # chase hero target = common_data.game.requestTarget(common_data.pos) if (target.x < common_data.pos.x): # self.setState(data, common_data, eStates.runLeft) data.vel = Vec3(-speed, 0, 0) data.facing = eDirections.left else: # self.setState(data, common_data, eStates.runRight) data.vel = Vec3(speed, 0, 0) data.facing = eDirections.right if (target.y < common_data.pos.y): data.vel.y = -speed else: data.vel.y = speed if common_data.pos.distSq( Vec3(target.x, target.y, common_data.pos.z)) < 800: data.vel.z = 0 # drop on target elif (common_data.pos.z < 80) and (data.vel.z < 3): data.vel.z += 2 # otherwise flap common_data.entity.graphics.startAnim( data=common_data.entity.graphics_data) data.cooldown = 0.2 if common_data.pos.z > 0: friction(data.vel, 0.01) else: friction(data.vel, 0.1) basic_gravity(data.vel) basic_physics(common_data.pos, data.vel) restrictToArena(common_data.pos, data.vel)
def restrictToArena(pos, vel): # stop running through walls at either side # if pos on left side of line then force to right side while pos.whichSidePlane(px_vector.Plane(1, 0, -1, 0)): px_controller.basic_physics(pos, Vec3(0.1, 0, -0.1)) # normal vector to plane # stop running through walls at either side # if pos on left side of line then force to right side while not pos.whichSidePlane(px_vector.Plane(1, 0, 1, -320)): px_controller.basic_physics(pos, Vec3(-0.1, 0, -0.1)) # normal vector to plane # stop running off screen bottom, top and sides pos.clamp(Vec3(0, 0, 0), Vec3(320, 200, 60))
def update(self, data, common_data, dt): # deal with things that can interrupt actions e.g. landing if common_data.pos.y <= 0: # on the ground self.setState(data, common_data, self.state_pool, self.pool_cooldown) data.vel = Vec3(0, 0, 0) else: # falling pass # deal with things that can't interrupt actions that are already happening if not self.coolDown(data, dt): if common_data.state == Controller.state_pool: self.setState(data, common_data, eStates.dead) px_controller.basic_physics(common_data.pos, data.vel)
def update(self, data, common_data, dt): if data.game_pad.actions[eActions.left]: self.updateState(data, common_data, DuckStates.swimLeft) data.faceleft = True data.vel.x = -DuckController.speed elif data.game_pad.actions[eActions.right]: self.updateState(data, common_data, DuckStates.swimRight) data.faceleft = False data.vel.x = DuckController.speed if data.game_pad.actions[eActions.up]: data.vel.z = DuckController.speed elif data.game_pad.actions[eActions.down]: data.vel.z = -DuckController.speed basic_physics(common_data.pos, data.vel) friction(data.vel)
def update(self, data, common_data, dt): data.facing = eDirections.left if data.vel.x < 0 else eDirections.right if common_data.state == eStates.fallLeft or common_data.state == eStates.fallRight: if not self.coolDown(data, dt): self.setState(data, common_data, eStates.dead) px_controller.friction(data.vel, 0.1) elif common_data.pos.y > 0: px_controller.friction(data.vel, 0.01) else: self.setState( data, common_data, eStates.fallLeft if data.facing == eDirections.left else eStates.fallRight) data.cooldown = 1 px_controller.friction(data.vel, 0.1) px_controller.basic_gravity(data.vel) px_controller.basic_physics(common_data.pos, data.vel) background.restrictToArena(common_data.pos, data.vel)
def update(self, data, common_data, dt): speed = 0.3 # if doing something that can't be interrupted then countdown to end of it if self.coolDown(data, dt): pass else: if data.health <= 0: self.setState(data, common_data, eStates.dead) return if rand_num(10) == 0: self.setState(data, common_data, eStates.stationary) data.vel = Vec3(0, 0, 0) data.cooldown = rand_num(1) + 2 else: # chase hero target = common_data.game.requestTarget(common_data.pos) if (target.x < common_data.pos.x): self.setState(data, common_data, eStates.runLeft) data.vel = Vec3(-speed, 0, 0) data.facing = eDirections.left else: self.setState(data, common_data, eStates.runRight) data.vel = Vec3(speed, 0, 0) data.facing = eDirections.right if (target.y < common_data.pos.y): data.vel.y = -speed else: data.vel.y = speed data.cooldown = 0.5 basic_physics(common_data.pos, data.vel) restrictToArena(common_data.pos, data.vel) friction(data.vel)
def update(self, data, common_data, dt): fire_cool = 1.4 if self.coolDown(data, dt): if data.fired and (common_data.state not in [ px_entity.eStates.fallLeft, px_entity.eStates.fallRight, px_entity.eStates.dead ]): if data.cooldown < 0.9: self.shoot(data, common_data, data.facingleft) data.fired = False else: if data.health <= 0: self.setState(data, common_data, px_entity.eStates.dead) return # fire at hero if in range target = common_data.game.requestTarget(common_data.pos) data.facingleft = (target.x < common_data.pos.x) if abs(target.x - common_data.pos.x) < 200 and abs( target.z - common_data.pos.z) < 20: self.setState( data, common_data, px_entity.eStates.attackSmallLeft if data.facingleft else px_entity.eStates.attackSmallRight) data.fired = True data.cooldown = fire_cool else: self.setState( data, common_data, px_entity.eStates.standLeft if data.facingleft else px_entity.eStates.standRight) data.cooldown = rand_num(1) + 2 px_controller.friction(data.vel) px_controller.basic_gravity(data.vel) px_controller.basic_physics(common_data.pos, data.vel) background.restrictToArena(common_data.pos, data.vel)
def update(self, entity, dt): bunny_speed = 1 map_entity = entity.parent map_controller = map_entity.getComponent('controller') if entity.state == PacBun.eStates.dead: return if entity.state in (PacBun.eStates.enterHoleUp, PacBun.eStates.enterHoleLeft, PacBun.eStates.enterHoleDown, PacBun.eStates.enterHoleRight): if self.coolDown(entity, dt): return if not entity.hole: # bunny didn't exit hole (end of map) # remove bunny self.setState(entity, PacBun.eStates.dead) # signal scene end entity.game.setFlag('next_scene') return self.setState(entity, ( PacBun.eStates.leavesHoleDown, PacBun.eStates.leavesHoleLeft, PacBun.eStates.leavesHoleUp, PacBun.eStates.leavesHoleRight, )[entity.hole.direction], cooldown=0.7) entity.pos = copy.deepcopy(entity.hole.exit) return if entity.state in (PacBun.eStates.leavesHoleUp, PacBun.eStates.leavesHoleLeft, PacBun.eStates.leavesHoleDown, PacBun.eStates.leavesHoleRight): if self.coolDown(entity, dt): return entity.invulnerable = False entity.facing = entity.hole.direction # avoid any unexpected turns that were already queued entity.queued_facing = entity.hole.direction entity.queued_state = (PacBun.eStates.runDown, PacBun.eStates.runLeft, PacBun.eStates.runUp, PacBun.eStates.runRight)[entity.facing] # actually set the state self.setState(entity, entity.queued_state) # get input and set up an action if entity.game_pad: # i.e. this bunny is being controlled by a player with a game_pad # player wants to go left at next opportunity if entity.game_pad.actions[px_game_pad.eActions.left]: # queue up that direction entity.queued_facing = px_entity.eDirections.left entity.queued_vel.x = -bunny_speed entity.queued_vel.y = 0 entity.queued_state = PacBun.eStates.runLeft # right elif entity.game_pad.actions[px_game_pad.eActions.right]: entity.queued_facing = px_entity.eDirections.right entity.queued_vel.x = bunny_speed entity.queued_vel.y = 0 entity.queued_state = PacBun.eStates.runRight # up elif entity.game_pad.actions[px_game_pad.eActions.up]: entity.queued_facing = px_entity.eDirections.up entity.queued_vel.x = 0 entity.queued_vel.y = bunny_speed entity.queued_state = PacBun.eStates.runUp # down elif entity.game_pad.actions[px_game_pad.eActions.down]: entity.queued_facing = px_entity.eDirections.down entity.queued_vel.x = 0 entity.queued_vel.y = -bunny_speed entity.queued_state = PacBun.eStates.runDown x, y = map_controller.getCoordFromPos(entity.pos) current_tile = map_controller.getTileFromCoord(map_entity, x, y) exits = current_tile.process('getExits') x_in_tile = entity.pos.x % 16 y_in_tile = entity.pos.y % 16 # decide if bunny is near middle of tile and if we should do womething special with the tile it's in if (6 < x_in_tile < 10) and (6 < y_in_tile < 10): if current_tile.state == tile.eTileStates.path: map_controller.poo(map_entity, current_tile, entity) # returns true if count of poos reaches number of empty spaces # i.e. won elif current_tile.state == tile.eTileStates.hole: # gone down a hole so find the next hole for the bunny to exit from # and setup the direction for the bunny to run from the entity for that hole # if self.game.game_mode == PacBun.eGameModes.escape: # print("Escaped!") entity.hole = map_controller.getNextHole(map_entity, x, y) self.setState(entity, { px_entity.eDirections.up: PacBun.eStates.enterHoleUp, px_entity.eDirections.down: PacBun.eStates.enterHoleDown, px_entity.eDirections.left: PacBun.eStates.enterHoleLeft, px_entity.eDirections.right: PacBun.eStates.enterHoleRight, }[entity.facing], cooldown=0.2) entity.invulnerable = True if (((entity.facing == px_entity.eDirections.left and entity.queued_facing == px_entity.eDirections.right) or (entity.facing == px_entity.eDirections.right and entity.queued_facing == px_entity.eDirections.left) or (entity.facing == px_entity.eDirections.up and entity.queued_facing == px_entity.eDirections.down) or (entity.facing == px_entity.eDirections.down and entity.queued_facing == px_entity.eDirections.up)) or (x_in_tile == 8 and y_in_tile == 8 and (((entity.queued_facing == px_entity.eDirections.left) and (px_entity.eDirections.left in exits)) or ((entity.queued_facing == px_entity.eDirections.right) and (px_entity.eDirections.right in exits)) or ((entity.queued_facing == px_entity.eDirections.up) and (px_entity.eDirections.up in exits)) or ((entity.queued_facing == px_entity.eDirections.down) and (px_entity.eDirections.down in exits))))): entity.facing = entity.queued_facing self.setState(entity, entity.queued_state) if entity.facing == px_entity.eDirections.left: entity.vel.x = -bunny_speed entity.vel.y = 0 elif entity.facing == px_entity.eDirections.right: entity.vel.x = bunny_speed entity.vel.y = 0 elif entity.facing == px_entity.eDirections.up: entity.vel.y = bunny_speed entity.vel.x = 0 elif entity.facing == px_entity.eDirections.down: entity.vel.y = -bunny_speed entity.vel.x = 0 # if at the centre of a tile then check if there's an exit in the direction the bunny # is running - if not stop the bunny if x_in_tile == 8 and y_in_tile == 8: if ((entity.facing == px_entity.eDirections.left) and not (px_entity.eDirections.left in exits)) \ or ((entity.facing == px_entity.eDirections.right) and not (px_entity.eDirections.right in exits)): entity.vel.x = 0 if ((entity.facing == px_entity.eDirections.up) and not (px_entity.eDirections.up in exits)) \ or ((entity.facing == px_entity.eDirections.down) and not (px_entity.eDirections.down in exits)): entity.vel.y = 0 if entity.vel.x == 0 and entity.vel.y == 0: entity.setState(PacBun.eStates.idle) px_controller.basic_physics(entity.pos, entity.vel)
def update(self, entity, dt): if entity.state in [PacBun.eStates.caughtBlue,PacBun.eStates.caughtBowie,PacBun.eStates.caughtPacBun,PacBun.eStates.caughtPinkie]: return # pause foxes every so often # todo: foxes shouldn't pause if the bunny is in sight entity.AI_cooldown -= dt if entity.AI_cooldown<=0: entity.AI_cooldown= 10 + px_vector.rand_num(10) if entity.fox_speed==1: entity.fox_speed=0 entity.AI_cooldown = entity.pause if entity.pause>0: entity.pause -=1 self.setState(entity, [PacBun.eStates.idle, PacBun.eStates.idle, PacBun.eStates.idle, PacBun.eStates.idle, PacBun.eStates.idle, PacBun.eStates.cleanRight, PacBun.eStates.cleanLeft][px_vector.rand_num(7)] # todo: find better way to make idle more likely ) else: entity.fox_speed=1 if entity.fox_speed==0: return fox_speed = entity.fox_speed map_controller = entity.parent.getComponent('controller') entity.bunny = map_controller.getNearestBunny(entity.parent, entity.pos) bunny_pos = copy.deepcopy(entity.bunny.getPos()) if entity.type==eFoxTypes.ahead: # aim at a position ahead of the bunny if entity.bunny.facing: bunny_pos+= ( Vec3(0,-96,0), Vec3(-96,0, 0), Vec3(0, 96, 0), Vec3(96, 0, 0), )[entity.bunny.facing] current_tile = map_controller.getTileFromPos(entity.parent, entity.pos) exits = current_tile.process('getExits') x_in_tile = entity.pos.x % 16 y_in_tile = entity.pos.y % 16 # work out preferred direction if bunny_pos.x>entity.pos.x: pref_dir=px_entity.eDirections.right else: pref_dir = px_entity.eDirections.left if bunny_pos.y > entity.pos.y: sec_dir = px_entity.eDirections.up else: sec_dir = px_entity.eDirections.down if (abs(bunny_pos.x-entity.pos.x)<abs(bunny_pos.y-entity.pos.y)): # choose most optimal axis pref_dir, sec_dir = sec_dir, pref_dir if entity.type==eFoxTypes.axis_swap: # swap to less optimal axis for this fox pref_dir, sec_dir = sec_dir, pref_dir elif entity.type==eFoxTypes.cowardly: # reverse directions so fox runs away pref_dir = ( px_entity.eDirections.up, px_entity.eDirections.right, px_entity.eDirections.down, px_entity.eDirections.left, )[pref_dir] sec_dir = ( px_entity.eDirections.up, px_entity.eDirections.right, px_entity.eDirections.down, px_entity.eDirections.left, )[sec_dir] # try to go that way if ((x_in_tile-8)%16==0) and ((y_in_tile-8)%16==0): if pref_dir in exits: entity.facing = pref_dir elif sec_dir in exits: entity.facing = sec_dir else: entity.facing = exits[0] entity.vel = ( Vec3(0,-fox_speed,0), Vec3(-fox_speed,0, 0), Vec3(0, fox_speed, 0), Vec3(fox_speed,0, 0), )[entity.facing] self.setState(entity,( PacBun.eStates.runDown, PacBun.eStates.runLeft, PacBun.eStates.runUp, PacBun.eStates.runRight )[entity.facing]) px_controller.basic_physics(entity.pos, entity.vel)