#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Visual world items module.
Implements:
class WorldPlayer2D -- world item skeleton for Player sprite in 2D
world.
class WorldPlayer3D -- world item skeleton for Player sprite in 3D
world.
class WorldSprite2D -- sprite management for 2D graphical world
items.
class WorldSprite3D -- sprite management for 3D graphical world
items.
class WorldView -- world item skeleton for a View component (2D/3D
worlds).
"""
"""
The MIT License (MIT)
Copyright (c) 2015 Raphaël SEBAN
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
try:
from . import world
from . import geometry
except:
import world
import geometry
# end try
[docs]class WorldSprite2D (world.WorldItem):
"""Sprite management for 2D graphical world items.
A sprite object is a visual world item in contrast to other
non-visual world items such as event, image and sound managers,
movement, physics and collision processors and so on.
"""
DEFAULT_POSITION = None
DEFAULT_VELOCITY = None
DEFAULT_UNIT_VECTOR = (1, 1)
def __init__ (self, world, name=None, **options):
"""Class constructor.
Parameters:
world -- parent world containing world item.
name -- world item's arbitrary name (default: None). If
None or omitted, will be replaced by current class name.
options -- keyword arguments.
Supported keyword arguments:
position -- sprite's position (default: None).
unit vector -- sprite's unit vector (default: (1, 1)).
velocity -- sprite's velocity (default: None).
Notice: options are saved into self.options member attribute.
"""
super().__init__(world, name)
self.position = options.get("position")
self.unit_vector = options.get("unit_vector")
self.velocity = options.get("velocity")
# end def
@property
[docs] def point_type (self):
"""Preferred point type - read-only attribute.
Current implementation returns geometry.WorldPoint2D class
type.
This can be redefined in subclasses to fit more specific needs.
"""
return geometry.WorldPoint2D
# end def
@property
def position (self):
"""Sprite's position in world area - read-write attribute."""
return self.__position
# end def
@position.setter
[docs] def position (self, value):
self.__position = self.point_type(
value or self.DEFAULT_POSITION
)
# end def
@property
def unit_vector (self):
"""Sprite's local unit vector - read-write attribute.
Helper for local relative calculations.
"""
return self.__unit_vector
# end def
@unit_vector.setter
[docs] def unit_vector (self, value):
self.__unit_vector = self.vector_type(
value or self.DEFAULT_UNIT_VECTOR
)
# end def
@property
[docs] def vector_type (self):
"""Preferred vector type - read-only attribute.
Current implementation returns geometry.WorldVector2D class
type.
This can be redefined in subclasses to fit more specific needs.
"""
return geometry.WorldVector2D
# end def
@property
def velocity (self):
"""Sprite's velocity - read-write attribute."""
return self.__velocity
# end def
@velocity.setter
[docs] def velocity (self, value):
self.__velocity = self.vector_type(
value or self.DEFAULT_VELOCITY
)
# end def
# end class
[docs]class WorldSprite3D (WorldSprite2D):
"""Sprite management for 3D graphical world items.
A sprite object is a visual world item in contrast to other
non-visual world items such as event, image and sound managers,
movement, physics and collision processors and so on.
"""
DEFAULT_UNIT_VECTOR = (1, 1, 1)
@property
[docs] def point_type (self):
"""Preferred point type - read-only attribute.
Current implementation returns geometry.WorldPoint3D class
type.
This can be redefined in subclasses to fit more specific needs.
"""
return geometry.WorldPoint3D
# end def
@property
[docs] def vector_type (self):
"""Preferred vector type - read-only attribute.
Current implementation returns geometry.WorldVector3D class
type.
This can be redefined in subclasses to fit more specific needs.
"""
return geometry.WorldVector3D
# end def
# end class
[docs]class WorldPlayer2D (WorldSprite2D):
"""World item skeleton for Player sprite in 2D world.
Current implementation does nothing.
Please, refer to world.WorldItemInterface for implementation
details.
"""
pass # see world.WorldItemInterface for implementation details
# end class
[docs]class WorldPlayer3D (WorldSprite3D):
"""World item skeleton for Player sprite in 3D world.
Current implementation does nothing.
Please, refer to world.WorldItemInterface for implementation
details.
"""
pass # see world.WorldItemInterface for implementation details
# end class
[docs]class WorldView (world.WorldItem):
"""World item skeleton for a View component (2D/3D worlds).
Current implementation does nothing.
Please, refer to world.WorldItemInterface for implementation
details.
"""
pass # see world.WorldItemInterface for implementation details
# end class
[docs]def run_demo ():
class Player (WorldPlayer2D):
def initialize (self):
print("initializing", self.name)
self.position = self.world.area.center_xy
print(self.name, "position:", self.position)
self.velocity = (-1, 1)
print(self.name, "velocity:", self.velocity)
self.counter = 0
# end def
def do_step (self, *args, **kw):
self.counter += 1
if self.counter < 10:
print(
self.name, "doing action:", self.counter
)
self.position += self.unit_vector.cdot(self.velocity)
else:
self.world.end()
# end if
# end def
def finalize (self):
print("finalizing", self.name)
# end def
# end class
class View (WorldView):
def do_step (self, *args, **kw):
print(
self.name, "showing player's position:",
self.world.get("player").position
)
# end def
def initialize (self):
print("initializing", self.name)
# end def
def finalize (self):
print("finalizing", self.name)
# end def
# end class
print("starting demo.")
_world = world.World2D()
_player = Player(_world)
_view = View(_world)
_world.run(fps=5)
print("demo ended.")
# end def
if __name__ == "__main__":
run_demo()
# end if