annotate src/parpg/bGrease/impl/world.py @ 70:ee350343b69b

Changed data subrepo to a grease specific one
author KarstenBock@gmx.net
date Tue, 06 Sep 2011 15:39:27 +0200
parents 96af64cf3b81
children 0f659c7675f6
rev   line source
27
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
1 #############################################################################
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
2 #
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
3 # Copyright (c) 2010 by Casey Duncan and contributors
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
4 # All Rights Reserved.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
5 #
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
6 # This software is subject to the provisions of the MIT License
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
7 # A copy of the license should accompany this distribution.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
8 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
9 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
10 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
11 #
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
12 #############################################################################
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
13 """Worlds are environments described by a configuration of components, systems and
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
14 renderers. These parts describe the data, behavioral and presentation aspects
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
15 of the world respectively.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
16
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
17 The world environment is the context within which entities exist. A typical
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
18 application consists of one or more worlds containing entities that evolve
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
19 over time and react to internal and external interaction.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
20
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
21 See :ref:`an example of world configuration in the tutorial <tut-world-example>`.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
22 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
23
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
24 __version__ = '$Id$'
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
25
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
26 import itertools
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
27 import pyglet
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
28 from pyglet import gl
66
96af64cf3b81 Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents: 27
diff changeset
29 from bGrease.world import *
96af64cf3b81 Renamed grease to bGrease (Basic Grease) to get rid of conflicts with an already installed grease.
KarstenBock@gmx.net
parents: 27
diff changeset
30 from bGrease.impl import Mode
27
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
31
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
32 class World(Mode, BaseWorld):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
33 """A coordinated collection of components, systems and entities
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
34
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
35 A world is also a mode that may be pushed onto a
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
36 :class:`grease.mode.Manager`
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
37
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
38 :param step_rate: The rate of :meth:`step()` calls per second.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
39
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
40 :param master_clock: The :class:`pyglet.clock.Clock` interface used
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
41 as the master clock that ticks the world's clock. This
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
42 defaults to the main pyglet clock.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
43 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
44
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
45 clock = None
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
46 """:class:`pyglet.clock` interface for use by constituents
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
47 of the world for scheduling
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
48 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
49
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
50 time = None
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
51 """Current clock time of the world, starts at 0 when the world
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
52 is instantiated
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
53 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
54
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
55 running = True
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
56 """Flag to indicate that the world clock is running, advancing time
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
57 and stepping the world. Set running to False to pause the world.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
58 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
59
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
60 def __init__(self, step_rate=60, master_clock=pyglet.clock,
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
61 clock_factory=pyglet.clock.Clock):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
62 Mode.__init__(self, step_rate, master_clock, clock_factory)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
63 BaseWorld.__init__(self)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
64
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
65 def activate(self, manager):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
66 """Activate the world/mode for the given manager, if the world is already active,
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
67 do nothing. This method is typically not used directly, it is called
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
68 automatically by the mode manager when the world becomes active.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
69
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
70 The systems of the world are pushed onto `manager.event_dispatcher`
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
71 so they can receive system events.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
72
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
73 :param manager: :class:`mode.BaseManager` instance
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
74 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
75 if not self.active:
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
76 for system in self.systems:
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
77 manager.event_dispatcher.push_handlers(system)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
78 super(World, self).activate(manager)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
79
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
80 def deactivate(self, manager):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
81 """Deactivate the world/mode, if the world is not active, do nothing.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
82 This method is typically not used directly, it is called
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
83 automatically by the mode manager when the world becomes active.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
84
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
85 Removes the system handlers from the `manager.event_dispatcher`
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
86
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
87 :param manager: :class:`mode.BaseManager` instance
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
88 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
89 for system in self.systems:
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
90 manager.event_dispatcher.remove_handlers(system)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
91 super(World, self).deactivate(manager)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
92
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
93 def tick(self, dt):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
94 """Tick the mode's clock, but only if the world is currently running
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
95
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
96 :param dt: The time delta since the last tick
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
97 :type dt: float
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
98 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
99 if self.running:
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
100 super(World, self).tick(dt)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
101
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
102 def step(self, dt):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
103 """Execute a time step for the world. Updates the world `time`
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
104 and invokes the world's systems.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
105
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
106 Note that the specified time delta will be pinned to 10x the
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
107 configured step rate. For example if the step rate is 60,
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
108 then dt will be pinned at a maximum of 0.1666. This avoids
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
109 pathological behavior when the time between steps goes
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
110 much longer than expected.
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
111
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
112 :param dt: The time delta since the last time step
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
113 :type dt: float
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
114 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
115 dt = min(dt, 10.0 / self.step_rate)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
116 for component in self.components:
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
117 if hasattr(component, "step"):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
118 component.step(dt)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
119 for system in self.systems:
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
120 if hasattr(system, "step"):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
121 system.step(dt)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
122
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
123 def on_draw(self, gl=pyglet.gl):
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
124 """Clear the current OpenGL context, reset the model/view matrix and
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
125 invoke the `draw()` methods of the renderers in order
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
126 """
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
127 gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
128 gl.glLoadIdentity()
09b581087d68 Added base files for grease
KarstenBock@gmx.net
parents:
diff changeset
129 BaseWorld.draw_renderers(self)