Projectile

class gamelib.Movable.Projectile(name='projectile', direction=10000100, step=1, range=5, model='⌁', movement_animation=None, hit_animation=None, hit_model=None, hit_callback=None, is_aoe=False, aoe_radius=0, parent=None, *args)

A class representing a projectile type board item. That class can be sub-classed to represent all your needs (fireballs, blasters shots, etc.).

That class support the 2 types of representations: model and animations. The animation cases are slightly more evolved than the regular item.animation. It does use the item.animation but with more finesse as a projectile can travel in many directions. So it also keeps track of models and animation per travel direction.

You probably want to subclass Projectile. It is totally ok to use it as it, but it is easier to create a subclass that contains all your Projectile information and let the game engine deal with orientation, range keeping, etc. Please see examples/07_projectiles.py for a good old fireball example.

By default, Projectile travels in straight line in one direction. This behavior can be overwritten by setting a specific actuator (a projectile is a Movable so you can use my_projectile.actuator).

The general way to use it is as follow:

  • Create a factory object with your static content (usually the static models, default direction and hit callback)
  • Add the direction related models and/or animation (keep in mind that animation takes precedence over static models)
  • deep copy that object when needed and add it to the projectiles stack of the game object.
  • use Game.actuate_projectiles(level) to let the Game engine do the heavy lifting.

The Projectile constructor takes the following parameters:

Parameters:
  • direction (int) – A direction from the Constants module
  • range (int) – The maximum range of the projectile in number of cells that can be crossed. When range is attained the hit_callback is called with a BoardItemVoid as a collision object.
  • step (int) – the amount of cells a projectile can cross in one turn
  • model (str) – the default model of the projectile.
  • movement_animation (Animation) – the default animation of a projectile. If a projectile is sent in a direction that has no explicit and specific animation, then movement_animation is used if defined.
  • hit_animation (Animation) – the animation used when the projectile collide with something.
  • hit_model (str) – the model used when the projectile collide with something.
  • hit_callback (function) – A reference to a function that will be called upon collision. The hit_callback is receiving the object it collides with as first parameter.
  • is_aoe (bool) – Is this an ‘area of effect’ type of projectile? Meaning, is it doing something to everything around (mass heal, exploding rocket, fireball, etc.)? If yes, you must set that parameter to True and set the aoe_radius. If not, the Game object will only send the colliding object in front of the projectile.
  • aoe_radius (int) – the radius of the projectile area of effect. This will force the Game object to send a list of all objects in that radius.
  • args – extra parameters to pass to hit_callback.
  • parent – The parent object (usually a Board object or some sort of BoardItem).

Important

The effects of a Projectile are determined by the callback. No callback == no effect!

Example:

fireball = Projectile(
                        name="fireball",
                        model=Utils.red_bright(black_circle),
                        hit_model=Sprites.EXPLOSION,
                    )
fireball.set_direction(Constants.RIGHT)
my_game.add_projectile(1, fireball,
                       my_game.player.pos[0], my_game.player.pos[1] + 1)
__init__(name='projectile', direction=10000100, step=1, range=5, model='⌁', movement_animation=None, hit_animation=None, hit_model=None, hit_callback=None, is_aoe=False, aoe_radius=0, parent=None, *args)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__([name, direction, step, range, …]) Initialize self.
add_directional_animation(direction, animation) Add an animation for a specific direction.
add_directional_model(direction, model) Add an model for a specific direction.
can_move() Movable implements can_move().
debug_info() Return a string with the list of the attributes and their current value.
directional_animation(direction) Return the animation for a specific direction.
directional_model(direction) Return the model for a specific direction.
display() Print the model WITHOUT carriage return.
has_inventory() Projectile cannot have inventory by default.
hit(objects) A method that is called when the projectile hit something.
overlappable() Projectile are overlappable by default.
pickable() This is a virtual method that must be implemented in deriving class.
remove_directional_animation(direction) Remove an animation for a specific direction.
remove_directional_model(direction) Remove the model for a specific direction.
restorable() We assume that by default, Projectiles are restorable.
set_direction(direction) Set the direction of a projectile
size() This is a virtual method that must be implemented in deriving class.
store_position(row, column) Store the BoardItem position for self access.