def __init__(self, gameworld, **kargs): self.world = gameworld self.steering = SteeringBehaviour(self) #self.heading_smoother = Vec2D() self.smoothed_heading = Vec2D() self.smoothing_on = False self.time_elapsed = float(0.0) self.vehicle_verts = [] # Create the vehicle's shape self.initialize_buffer() MovingObject.__init__(self, **kargs)
class Vehicle(MovingObject): def __init__(self, gameworld, **kargs): self.world = gameworld self.steering = SteeringBehaviour(self) #self.heading_smoother = Vec2D() self.smoothed_heading = Vec2D() self.smoothing_on = False self.time_elapsed = float(0.0) self.vehicle_verts = [] # Create the vehicle's shape self.initialize_buffer() MovingObject.__init__(self, **kargs) def update(self, dt): self.time_elapsed = dt # Keep a record of its old position so we can update its cell later # in this method oldPos = self.pos # Calculate the combined force from each steering behavior in the # vehicle's list steeringForce = self.steering.calculate() # acceleration = force / mass acceleration = steeringForce / self.mass # Update velocity self.vel += acceleration * dt # Make sure the vehicle doesn't exceed the max velocity self.vel.truncate(self.maxSpeed) # Update the position self.pos += self.vel * dt # Update the heading if the vehicle has a non zero velocity if self.vel.length_sqrd() > 0.00000001: self.heading = self.vel.normalized_vec() self.side = self.heading.perp() # Treat the screen as a toroid wrap_around(self.pos, self.world.width, self.world.height) # update the vehicle's current cell if space partitioning is turned on '''if (Steering()->isSpacePartitioningOn()) { World()->CellSpace()->UpdateEntity(this, OldPos); } if self.smoothing_on: self.smoothed_heading = m_pHeadingSmoother->Update(Heading()); ''' def draw(self): # Holds the transformed vertices transVerts = [] if self.smoothing_on: transVerts = world_transform(self.vehicle_verts, self.pos, self.smoothed_heading, self.smoothed_heading.perp(), self.scale) else: transVerts = world_transform(self.vehicle_verts, self.pos, self.heading, self.side, self.scale) points = [v.tup() for v in transVerts] pygame.draw.polygon(self.world.screen, self.color, points, 2) def initialize_buffer(self): """ Fills the vehicle's shape buffer with its vertices """ verts = [Vec2D(-1.0, 0.7), Vec2D(1.0, 0.0), Vec2D(-1.0, -0.7)] # Setup the vertex buffer for v in verts: self.vehicle_verts.append(v)