Beispiel #1
0
    def update(self, dt):
        """Update the character.

        This method computes character's game logic as a function of time.

        :param dt: Time delta from last update.
        :type dt: float
        """
        self[Movable].update(dt)
        x, y = self[Movable].position

        # FIXME: I don't like the idea of saving the group node here. We need
        # something better here.
        g_t = self.group_node.transform
        g_t.identity()
        g_t.translate(to_scene(x, y))

        rot, scale = self.TRANSFORMS[self.actor_type]
        t = self[Renderable].transform
        t.identity()
        t *= self.transform
        t.rotate(Vec(0, 1, 0), rot)  # FIXME
        t.rotate(Vec(0, 1, 0), -self.heading)
        t.scale(Vec(scale, scale, scale))

        self.orientate()

        # Update the health bar
        self.health_bar.update(dt)

        # play animation
        if self.current_anim:
            self.current_anim.play(dt)
Beispiel #2
0
    def __init__(self, resource, position, progress, completed, parent_node):
        """Constructor.

        :param resource: The building resource
        :type resource: :class:`loaders.Resource`

        :param position: The position of the building
        :type position: :class:`tuple`

        :param progress: The current amount of hp and the total one
        :type progress: :class:`tuple`

        :param completed: Whether the building is completed or not
        :type completed: :class:`bool`

        :param parent_node: The parent node in the scene graph
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        self._position = position
        # Progress is going to be a property used to update only when necessary
        # the health bar.
        self._progress = progress
        self.completed = completed

        shader = resource['shader']
        texture = Texture.from_image(resource['texture'])
        self.mesh_project = resource['model_project']
        self.mesh_complete = resource['model_complete']

        # Setup the group node and add the health bar
        group_node = SceneNode()
        g_transform = group_node.transform
        g_transform.translate(to_scene(*position))
        parent_node.add_child(group_node)

        self.health_bar = HealthBar(
            resource['health_bar'], progress[0] / progress[1], group_node)

        params = {
            'tex': texture,
        }

        # create components
        renderable = Renderable(
            group_node,
            self.mesh,
            shader,
            params,
            textures=[texture],
            enable_light=True)

        # initialize entity
        super().__init__(renderable)

        # FIXME: hardcoded bounding box
        self._bounding_box = Vec(-0.5, 0, -0.5), Vec(0.5, 2, 0.5)
Beispiel #3
0
    def update(self, dt):
        """Update the local player.

        This method computes player's game logic as a function of time and sends
        the appropriate event.

        :param dt: Time delta from last update.
        :type dt: float
        """
        super(Player, self).update(dt)

        x, y = self.position

        # update camera position
        context = Context.get_instance()
        context.camera.set_position(to_scene(x, y))
Beispiel #4
0
    def __init__(self, resource, scene, position, progress, completed):
        """Constructor.

        :param resource: The building resource
        :type resource: :class:`loaders.Resource`

        :param scene: Scene to add the building bar to.
        :type scene: :class:`renderlib.scene.Scene`

        :param position: The position of the building
        :type position: :class:`tuple`

        :param progress: The current amount of hp and the total one
        :type progress: :class:`tuple`

        :param completed: Whether the building is completed or not
        :type completed: :class:`bool`
        """
        super().__init__()

        self.scene = scene
        self.obj = None
        self._position = to_scene(*position)
        self._completed = None

        # create texture
        texture = Texture.from_image(resource['texture'],
                                     Texture.TextureType.texture_2d)
        self.mesh_project = resource['model_project']
        self.mesh_complete = resource['model_complete']

        # create material
        material = Material()
        material.texture = texture

        # create render props
        self.props = MeshProps()
        self.props.material = material

        # set initial building status
        self.completed = completed

        # FIXME: hardcoded bounding box
        self._bounding_box = Vec(-0.5, 0, -0.5), Vec(0.5, 2, 0.5)
    def update(self, dt):
        """Update the building template.

        This method just applies the current position of the entity to the
        renderable transform.

        :param dt: Time delta from last update.
        :type dt: float
        """
        x, y = self.pos

        m_x, m_y = to_matrix(x, y, self.scale_factor)
        if not in_matrix(self.matrix, m_x, m_y) or not self.matrix[m_y][m_x]:
            self.props.material.color = self.NON_BUILDABLE_COLOR
        else:
            self.props.material.color = self.BUILDABLE_COLOR

        self.obj.position = to_scene(x, y)
        self.obj.scale = Vec(1.05, 1.05, 1.05)
Beispiel #6
0
    def __init__(self, resource, scene, position, progress, completed):
        """Constructor.

        :param resource: The building resource
        :type resource: :class:`loaders.Resource`

        :param scene: Scene to add the building bar to.
        :type scene: :class:`renderlib.scene.Scene`

        :param position: The position of the building
        :type position: :class:`tuple`

        :param progress: The current amount of hp and the total one
        :type progress: :class:`tuple`

        :param completed: Whether the building is completed or not
        :type completed: :class:`bool`
        """
        super().__init__()

        self.scene = scene
        self.obj = None
        self._position = to_scene(*position)
        self._completed = None

        # create texture
        texture = Texture.from_image(resource['texture'], Texture.TextureType.texture_2d)
        self.mesh_project = resource['model_project']
        self.mesh_complete = resource['model_complete']

        # create material
        material = Material()
        material.texture = texture

        # create render props
        self.props = MeshProps()
        self.props.material = material

        # set initial building status
        self.completed = completed

        # FIXME: hardcoded bounding box
        self._bounding_box = Vec(-0.5, 0, -0.5), Vec(0.5, 2, 0.5)
Beispiel #7
0
    def update(self, dt):
        """Update the character.

        This method computes character's game logic as a function of time.

        :param dt: Time delta from last update.
        :type dt: float
        """
        self[Movable].update(dt)
        x, y = self[Movable].position

        rot, scale = self.TRANSFORMS[self.actor_type]
        self.obj.position = to_scene(x, y)
        self.obj.rotation.rotatev(Vec(0, 1, 0), rot + -self.heading)
        self.obj.scale = Vec(scale, scale, scale)

        self.orientate()

        # play animation
        if self.current_anim:
            self.current_anim.play(dt)
Beispiel #8
0
    def update(self, dt):
        """Update the building template.

        This method just applies the current position of the entity to the
        renderable transform.

        :param dt: Time delta from last update.
        :type dt: float
        """
        x, y = self.pos

        m_x, m_y = to_matrix(x, y, self.scale_factor)
        if not in_matrix(self.matrix, m_x, m_y) or not self.matrix[m_y][m_x]:
            self[Renderable].node.params.update(self.NON_BUILDABLE_COLOR)
        else:
            self[Renderable].node.params.update(self.BUILDABLE_COLOR)

        t = self[Renderable].transform
        t.identity()
        t.translate(to_scene(x, y))
        t.scale(Vec(1.05, 1.05, 1.05))
Beispiel #9
0
    def update(self, dt):
        """Update the character.

        This method computes character's game logic as a function of time.

        :param dt: Time delta from last update.
        :type dt: float
        """
        self[Movable].update(dt)
        x, y = self[Movable].position

        rot, scale = self.TRANSFORMS[self.actor_type]
        self.obj.position = to_scene(x, y)
        self.obj.rotation.rotatev(Vec(0, 1, 0), rot + -self.heading)
        self.obj.scale = Vec(scale, scale, scale)

        self.orientate()

        # play animation
        if self.current_anim:
            self.current_anim.play(dt)
Beispiel #10
0
    def __init__(self, resource, scene, parameters):
        """Constructor.

        :param resource: Resource containing the object data.
        :type resource: :class:`resource_manager.Resource`

        :param scene: Scene to add the health bar to.
        :type scene: :class:`renderlib.scene.Scene`

        :param parameters: Parameters for the object.
        :type parameters: :class:`dict`
        """
        super().__init__()

        mesh = resource['model']
        texture = Texture.from_image(
            resource['texture'],
            Texture.TextureType.texture_2d)

        material = Material()
        material.texture = texture
        material.receive_light = True

        props = MeshProps()
        props.material = material
        props.cast_shadows = True
        props.receive_shadows = True

        self.obj = scene.add_mesh(mesh, props)
        self.obj.position = to_scene(*parameters['pos'])
        # FIXME: an added offset to match the level displacement
        self.obj.position.z += 1

        if 'rotation' in parameters:
            self.obj.rotation.rotatev(
                Y_AXIS, parameters['rotation'] * pi / 180)

        # FIXME: hardcoded bounding box
        self._position = parameters['pos']
        self._bounding_box = Vec(-0.5, 0.5, -0.5), Vec(0.5, 1.5, 0.5)
Beispiel #11
0
    def __init__(self, resource, parameters, parent_node):
        """Constructor.

        :param resource: Resource containing the object data.
        :type resource: :class:`resource_manager.Resource`

        :param parameters: Parameters for the object.
        :type parameters: :class:`dict`

        :param parent_node: Node to attach the object to.
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        mesh = resource["model"]
        shader = resource["shader"]
        texture = Texture.from_image(resource["texture"])

        # shader params
        params = {
            "tex": texture,
            "animate": 0,
            "opacity": 1.0,
            "color_ambient": Vec(0, 0, 0, 1),
            "color_diffuse": Vec(0, 0, 0, 1),
            "color_specular": Vec(0.1, 0.1, 0.1, 1),
        }

        renderable = Renderable(parent_node, mesh, shader, params, textures=[texture], enable_light=True)

        super().__init__(renderable)

        self[Renderable].transform.translate(to_scene(*parameters["pos"]))

        if "rotation" in parameters:
            self[Renderable].transform.rotate(Y_AXIS, parameters["rotation"] * pi / 180)

        # FIXME: hardcoded bounding box
        self._position = parameters["pos"]
        self._bounding_box = Vec(-0.5, 0.5, -0.5), Vec(0.5, 1.5, 0.5)
Beispiel #12
0
    def __init__(self, resource, actor_type, health, parent_node):
        """Constructor.

        :param resource: The character resource
        :type resource: :class:`loaders.Resource`

        :param health: The current amount of hp and the total one
        :type health: :class:`tuple`

        :param parent_node: The parent node in the scene graph
        :type parent_node: :class:`renderer.scene.SceneNode`
        """
        self.actor_type = actor_type

        # Health is going to be a property used to update only when necessary
        # the health bar.
        self._health = health

        shader = resource['shader']
        mesh = resource['model']['mesh']
        md = resource['model']['mesh_data']

        # root transformation to apply to the mesh
        self.transform = md.transform

        # instantiate animations
        self.init_animations(md)
        self.current_anim = self.animations[action_anim_index(ActionType.idle)]

        texture = Texture.from_image(resource['texture'])

        # shader params
        params = {
            'tex': texture,
            'opacity': 1.0,
            'color_ambient': Vec(0, 0, 0, 1),
            'color_diffuse': Vec(0, 0, 0, 1),
            'color_specular': Vec(0.1, 0.1, 0.1, 1),
        }

        # Initialize movable component
        movable = Movable((0.0, 0.0))

        # Setup the group node and add the health bar
        # FIXME: I don't like the idea of saving the group node here. We need
        # something better here.
        self.group_node = SceneNode()
        g_transform = self.group_node.transform
        g_transform.translate(to_scene(*movable.position))
        parent_node.add_child(self.group_node)

        self.health_bar = HealthBar(
            resource['health_bar'], health[0] / health[1], self.group_node,
            resource.data.get('hb_y_offset'))

        # create components
        renderable = Renderable(
            self.group_node,
            mesh,
            shader,
            params,
            textures=[texture],
            enable_light=True,
            animation=self.current_anim)

        # by default, start with playing animation
        renderable.animate = True

        # initialize actor
        super().__init__(renderable, movable)

        # FIXME: hardcoded bounding box
        self._bounding_box = Vec(-0.5, 0, -0.5), Vec(0.5, 2, 0.5)

        self.heading = 0.0