def __update_position__(self, dt): """ Based on the current L{Velocity<Actor.Actor.Velocity>} update L{Position<Actor.Actor.Position>}. Gravity is also applied during this call. @type dt: C{float} @param dt: Time in seconds since the last frame refresh. """ self.Velocity += (self.Map.gravity * dt) if self.Velocity.y > self._maxFallVel: self.Velocity = Vector((self.Velocity.x, self._maxFallVel)) Actor.__update_position__(self, dt)
def __resolve_tile_collisions__(self, sideTiles, aboveBelowTiles, previousBB, collisionLayer): """ Resolves collisions with Spike Tiles first, and sets State based on collision results. These State changes would be appropriate for a platformer. """ originalX, originalY = self.Position allTiles = set() allTiles.update(sideTiles) allTiles.update(aboveBelowTiles) for tile in allTiles: if type(tile) in self._specialTiles: self.ResolveCollision(tile) tile.ResolveCollision(self) # after resolving, get rid of all the tiles of that type so we only collide once for sideTile in list(sideTiles): if isinstance(sideTile, type(tile)): sideTiles.remove(sideTile) for aboveBelowTile in list(aboveBelowTiles): if isinstance(aboveBelowTile, type(tile)): aboveBelowTiles.remove(aboveBelowTile) break # collides with normal square tiles if collisionLayer.Name == Constants.EditorConstants.LAYER_NAME_COLLISION_TILES: Actor.__resolve_tile_collisions__(self, sideTiles, aboveBelowTiles, previousBB, collisionLayer) # For a platformer, switch to the land state when hitting the ground if (originalY > self.Position[1]): # got pushed up, must be on ground if not self._onGround: #self.ChangeState('land') self._onGround = True else: ''' # little hack to make falling off things slower - CAD if (self._onGround): self.Velocity = Vector((self.Velocity.x, self.Velocity.y * 0.60)) ''' if (self.Velocity.y > 0.0): self._onGround = False if (originalY < self.Position[1]): # hit a ceiling, stop moving up self.Velocity = Vector((self.Velocity.x, 0)) if (originalX != self.Position[0]): # hit a wall, Velocity goes to 0? self.Velocity = Vector((0, self.Velocity.y))
def __init__(self, position, width, height, name, collisionGroupNames, transferName, stateMappings, startStateName, image=None, animationMappings=None, soundMappings=None): ''' Constructor ''' self._facingRight = True self._onGround = False # set some default max speeds self._maxXVel = Constants.PlayerConstants.MAX_X_VELOCITY self._maxYVel = Constants.PlayerConstants.MAX_Y_VELOCITY self._maxFallVel = Constants.PlayerConstants.MAX_FALL_VELOCITY self._jumpVel = Constants.PlayerConstants.MAX_JUMP_VELOCITY self._runVel = Constants.PlayerConstants.MAX_RUN_VELOCITY Actor.__init__(self, position, width, height, name, collisionGroupNames, transferName, stateMappings, startStateName, image, animationMappings, soundMappings) from Example.BounceTile import BounceTile from Example.ExitTile import ExitTile from Example.KillTile import KillTile self._specialTiles = [BounceTile, ExitTile, KillTile]
def __check_resolve_env_collisions__(self, collisionLayer): """ Overriden to use our own collision resolution method. """ previousBB = pygame.Rect(self._prevPosition, (self.boundingBox.width, self.boundingBox.height)) # if we haven't moved, don't bother if (self.boundingBox.topleft == previousBB.topleft): return sideTiles, aboveBelowTiles = Actor.__get_colliding_tiles__(self, collisionLayer) # prioritize and resolve collisions if there are any # CAD - optimization breaks falling #if sideTiles or aboveBelowTiles: self.__resolve_tile_collisions__(sideTiles, aboveBelowTiles, previousBB, collisionLayer)
def __init__(self, position, width, height, name, collisionGroupNames, transferName, playerNum, controller, stateMappings={}, startStateName='', image=None, animationMappings=None, soundMappings=None): """ Creates a new Player at the given position, with a bounding box having the given width and height, and associated controller for input gathering. @type position: C{(int, int) | L{Vector<Utilities.vector.Vector>}} @param position: World coordinates of the top left corner of the object's bounding box @type width: C{int} @param width: Width of the object's bounding box in pixels. @type height: C{int} @param height: Height of the object's bounding box in pixels. @type collisionGroupNames: C{list} @param collisionGroupNames: List of names (C{str}) of L{CollisionGroup<CollisionGroup.CollisionGroup>}s that this Actor should be part of. @type transferName: C{str} @param transferName: Name of the Actor to transfer attributes from when switching between maps. @type playerNum: C{int} @param playerNum: Player number from the U{Tiled's<http://mapeditor.org/>} .TMX map file. In the editor this is 1-based, but internally it is 0-based. @type controller: L{Controller<Utilities.Controller.Controller.Controller>} @param controller: Where the Player will be getting its input. When loading a Player from a map, this will be C{None}. The Controller will be associated to the player during map load instead of when the constructor is called. @type stateMappings: C{list} @param stateMappings: List of pairs in the form C{str, L{State<State.State>}} which are the States available to this Actor. In detail they are: - C{str} - Name to use to refer to this State. - C{State} - The State object. @type startStateName: C{str} @param startStateName: Name of the L{State<State.State>} the Actor will start in. @type image: U{C{pygame.Surface}<http://www.pygame.org/docs/ref/surface.html>} @param image: The static image that should be drawn at the object's L{Position}. @type animationMappings: C{list} @param animationMappings: List of tuples in the form C{(str, L{Animation<Animation.Animation>}, (int, int))}. In detail they are: - C{str} - Name to use to refer to this Animation. - C{Animation} - Animation object itself. - C{(int, int)} - Offset from the GameObject's L{Position} where the top-left of the Animation's frames should be drawn. @type soundMappings: C{list} @param soundMappings: List of tuples in the form C{(str, L{Sound<Sound.Sound>})}. In detail they are: - C{str} - Name to use to refer to this Sound. - C{Sound} - Sound object itself. """ # pass up the states Actor.__init__(self, position, width, height, name, collisionGroupNames, transferName, stateMappings, startStateName, image, animationMappings, soundMappings) # we have a controller self._controller = controller # for loading from editor self.PlayerNum = playerNum