Entity System

Pyglet and ecs Part 2: Flying Around

So right now we can give entities images. However, they don’t do anything on screen other than stay very still in the location we put them. To change this we need to give the entities a Physics component this will allow for the entity to update its position.


#components.py

class Physics(Component):
    def __init__(self, vx=0.0, vy=0.0, thrust=100.0, rotate_speed=100.0):
        self.vx = vx #the velocity in the x axis
        self.vy = vy #the velocity in the y axis
        self.thrust = thrust #the amount of force we can apply
        self.rotate_speed = rotate_speed #how quickly we can rotate.

We also need to build the accompanying PhysicsSystem, which will require the use of Physics component and the Transform component.


#systems.py
class PhysicsSystem(System):
    def __init__(self):
        System.__init__(self)
        self.drag = 0.4

    def update(self, dt):
        for entity, phys_comp  in self.entity_manager.pairs_for_type(Physics):
            #we need our transform component to update the position of the entity
            pos_comp= self.entity_manager.component_for_entity(entity, Transform)

            #times it by the delta time to get frame rate independent movement.
            pos_comp.x += phys_comp.vx * dt
            pos_comp.y += phys_comp.vy * dt

            #apply drag to velocity.
            phys_comp.vx -= phys_comp.vx * self.drag *dt
            phys_comp.vy -= phys_comp.vy * self.drag *dt

To get our player to start moving we need to update the entity factory create_player() method and add the system into our SystemsManager. To test the movement system is working we can give our player an initial velocity, it’s a one line addition after the other components

(more…)

Pyglet and ecs Part 1: Small Beginnings

I am not going to go through the specifics of setting up Python/Pyglet/IDE’s and ecs, but here are the links to all the appropriate sources:

  • Python (I’m using 2.7)
  • ecs (I used pip to install it, here is an easy install for pip)
  • Pyglet I am using the development version (pyglet 1.2 alpha1), and used pip on the command line to install as per instructions on the bottom of their download page.

QuickStart Guide for Pyglet: If you’ve never used Pyglet I would advise looking through this, just to get a feel for how Pyglet works. They can probably explain the basics better than I can!

The IDE I am (experimentally) using is PyCharm, though I have used Aptana Studio standalone, and both do a very good job.

This tutorial is based heavily upon this tutorial, but I have tried to go about extracting the game into components and systems.

(more…)

Combining Pyglet and ecs the Entity System

Entity Systems have fascinated me for a while, the idea of being able to effectively turn anything into anything else with a few removals and additions of components makes me happy. My knowledge on the Entity System and it’s pros and cons is somewhat limited. So if you want to find out more read these posts (from far more knowledgable others) :

This Guy Knows Lots
This Article

My intention is to experiment with the combination of ecs and pyglet to create a basic Asteroids type game, and I hope to document the process for others interested in following the same path.

PART 1: Small Beginnings – Introduces the Renderable and Transform components.
PART 2: Flying Around – Introduces the Physics and Player components.
PART 3: Crashing Into Things – Introduces the Bounds component.