Example #1
0
class PowerUp (BasicEntity):
    def __init__(self, x, y, texture_name, lifetime, effect, name, effectCreationFunc):
        r = 15.0
        BasicEntity.__init__(self, x, y, texture_name, r, 1)
        self.life_hud.node.set_active(False)
        self.max_velocity = 135.0

        self.lifespan = lifetime
        self.lifetime = lifetime
        self.effect = effect
        self.effect.creation_func = effectCreationFunc
        self.wasApplied = False
        self.blink_time = 0.0

        self.text = Engine_reference().text_manager().GetText(name)
        self.textNode = Node(self.text)
        self.textNode.modifier().set_offset( Vector2D(-self.text.width()/2.0, 0.0 ) ) # TODO: text hotspot!
        self.textNode.set_active(False)
        self.node.AddChild(self.textNode)

    def setupCollisionObject(self):
        self.collision_object = CollisionObject(getCollisionManager(), self)  #initialize collision object, second arg is passed to collisionlogic to handle collisions
        self.collision_object.InitializeCollisionClass("PowerUp")              # define the collision class
        self.collision_object.set_shape(self.geometry)                # set our shape
        self.collision_object.AddCollisionLogic("Entity", BasicColLogic(self) )
        self.collision_object.thisown = 0

    def Update(self, dt):
        if not self.wasApplied:
            BasicEntity.Update(self, dt)
        self.lifetime -= dt
        if self.lifetime < self.lifespan*0.15 and not self.wasApplied:
            self.blink_time += dt
            if self.blink_time > self.lifetime/self.lifespan:
                self.blink_time = 0.0
                self.node.set_active( not self.node.active() )
        if self.lifetime < 0:
            self.Delete()

    def HandleCollision(self, target):
        if target.CheckType("Ship") and not self.wasApplied:
            self.effect.SetTarget(target)
            target.ApplyEffect(self.effect)
            self.wasApplied = True
            self.lifetime = 3.0
            self.textNode.set_active(True)
            color = self.node.modifier().color()
            color.set_a(0.2)
            self.node.modifier().set_color(color)
            self.node.set_active(True)
Example #2
0
class EntityInterface (Entity):
    nextID = 1
    def __init__(self, x, y, radius):
        self.radius = radius
        self.is_destroyed = False
        self.to_be_removed = False
        self.is_collidable = True
        self.collision_object = None

        self.hud_node = Node()
        self.node = Node()
        self.SetPos( Vector2D(x,y) )

        # Calculating Type
        self.type = str(self.__class__)
        if len(self.type.split("'")) > 1:
            self.type = self.type.split("'")[1]
        self.type = self.type.split(".")[1]

        # Get Unique ID
        self.id = EntityInterface.nextID
        EntityInterface.nextID += 1

    def CheckType(self, typestr):
        return self.type.count(typestr) > 0

    def ClearNewObjects(self):
        self.new_objects = []

    def GetNode(self):  return self.node
    def GetHUDNode(self):   return self.hud_node

    def GetPos(self):
        return self.node.modifier().offset()

    def SetPos(self, pos):
        self.node.modifier().set_offset(pos)
        self.hud_node.modifier().set_offset(pos)
        if self.collision_object != None:
            self.collision_object.MoveTo(pos)

    def GetDirection(self):
        return Vector2D(0.0, 1.0)

    def GetPointsValue(self):
        return 0

    def HandleMapBoundaries(self, pos):
        max = Config.gamesize

        passedBoundary = False
        # checking for horizontal map boundaries
        if pos.get_x() < 0.0:
            pos.set_x( max.get_x() + pos.get_x() )
            passedBoundary = True
        if pos.get_x() > max.get_x():
            pos.set_x( pos.get_x() - max.get_x() )
            passedBoundary = True

        # checking for vertical map boundaries
        if pos.get_y() < 0.0:
            pos.set_y( max.get_y() + pos.get_y() )
            passedBoundary = True
        if pos.get_y() > max.get_y():
            pos.set_y( pos.get_y() - max.get_y() )
            passedBoundary = True
        return passedBoundary
            
    def Update(self, dt):
        pass

    def HandleCollision(self, target):
        print self.type, " HandleCollision NOT IMPLEMENTED"
        
    def Delete(self):
        if hasattr(self, "node") and self.node != None:
            self.node.set_active(False)
        if self.is_destroyed:   return
        self.is_destroyed = True        

    def __repr__(self):
        return "<%s #%s>" % (self.type, self.id)
        
    def __str__(self): return self.__repr__()