Module game_qu.platformer.gravity_engine

Expand source code
from game_qu.paths.physics_followable_path import PhysicsFollowablePath


class GravityEngine:
    """Runs all the gravity for the objects"""

    game_object_to_physics_path = {}
    acceleration = 0

    def __init__(self, game_objects, acceleration):
        """Initializes the object"""

        self.acceleration = acceleration
        self.add_game_objects(game_objects)

    def add_game_objects(self, game_objects):
        """Adds game_objects so they are affected gravity"""

        for game_object in game_objects:
            physics_path = PhysicsFollowablePath()
            physics_path.acceleration = self.acceleration
            self.game_object_to_physics_path[game_object] = physics_path

    def run(self):
        """Runs all the gravity code"""

        for game_object in self.game_object_to_physics_path.keys():
            if not game_object.is_on_platform:
                # Each object has its own physics path because each one will have different top_edge's, time in air, etc.
                physics_path: PhysicsFollowablePath = self.game_object_to_physics_path[game_object]

                physics_path.run(game_object.is_on_platform, not game_object.is_on_platform)

                game_object.top_edge += physics_path.get_acceleration_displacement()

    def reset(self):
        """Resets everything back to the start of the game"""

        for physics_path in self.game_object_to_physics_path.values():
            return physics_path.reset()

Classes

class GravityEngine (game_objects, acceleration)

Runs all the gravity for the objects

Initializes the object

Expand source code
class GravityEngine:
    """Runs all the gravity for the objects"""

    game_object_to_physics_path = {}
    acceleration = 0

    def __init__(self, game_objects, acceleration):
        """Initializes the object"""

        self.acceleration = acceleration
        self.add_game_objects(game_objects)

    def add_game_objects(self, game_objects):
        """Adds game_objects so they are affected gravity"""

        for game_object in game_objects:
            physics_path = PhysicsFollowablePath()
            physics_path.acceleration = self.acceleration
            self.game_object_to_physics_path[game_object] = physics_path

    def run(self):
        """Runs all the gravity code"""

        for game_object in self.game_object_to_physics_path.keys():
            if not game_object.is_on_platform:
                # Each object has its own physics path because each one will have different top_edge's, time in air, etc.
                physics_path: PhysicsFollowablePath = self.game_object_to_physics_path[game_object]

                physics_path.run(game_object.is_on_platform, not game_object.is_on_platform)

                game_object.top_edge += physics_path.get_acceleration_displacement()

    def reset(self):
        """Resets everything back to the start of the game"""

        for physics_path in self.game_object_to_physics_path.values():
            return physics_path.reset()

Class variables

var acceleration
var game_object_to_physics_path

Methods

def add_game_objects(self, game_objects)

Adds game_objects so they are affected gravity

Expand source code
def add_game_objects(self, game_objects):
    """Adds game_objects so they are affected gravity"""

    for game_object in game_objects:
        physics_path = PhysicsFollowablePath()
        physics_path.acceleration = self.acceleration
        self.game_object_to_physics_path[game_object] = physics_path
def reset(self)

Resets everything back to the start of the game

Expand source code
def reset(self):
    """Resets everything back to the start of the game"""

    for physics_path in self.game_object_to_physics_path.values():
        return physics_path.reset()
def run(self)

Runs all the gravity code

Expand source code
def run(self):
    """Runs all the gravity code"""

    for game_object in self.game_object_to_physics_path.keys():
        if not game_object.is_on_platform:
            # Each object has its own physics path because each one will have different top_edge's, time in air, etc.
            physics_path: PhysicsFollowablePath = self.game_object_to_physics_path[game_object]

            physics_path.run(game_object.is_on_platform, not game_object.is_on_platform)

            game_object.top_edge += physics_path.get_acceleration_displacement()