-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
63 lines (55 loc) · 2.39 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from rendering import Renderer, RenderingObject
import time
class Node():
def __init__(self, position, texture, collision_box = None):
self.position = position
self.texture = texture
self.collision_box = collision_box
self.renderobject = RenderingObject(self.position, texture)
def _process(self, scene, delta):
pass
def _on_collision(self, scene, collider):
pass
class Scene():
def __init__(self, manager, name):
self.nodes = []
self.renderer = Renderer([60, 30])
self.stop_flag = False
self.manager = manager
self.manager.scenes[name] = self
def start(self):
loop_start_time = 0
delta = 0
while not self.stop_flag:
collision_boxes = []
for node in self.nodes:
node._process(self, delta)
if node.collision_box:
for collision_box in collision_boxes:
position_cb = [[axis + collision_box[0].position[i] for i, axis in enumerate(coord)] for coord in collision_box[1]]
node_position_cb = [[axis + node.position[i] for i, axis in enumerate(coord)] for coord in node.collision_box]
if (min(position_cb[1][0], node_position_cb[1][0]) > max(position_cb[0][0], node_position_cb[0][0])) and (min(position_cb[1][1], node_position_cb[1][1]) > max(position_cb[0][1], node_position_cb[0][1])):
node._on_collision(self, collision_box[0])
collision_box[0]._on_collision(self, node)
collision_boxes.append([node, node.collision_box])
self.renderer.render_frame()
delta = time.time() - loop_start_time
loop_start_time = time.time()
def stop(self):
self.stop_flag = True
def add_node(self, node: Node):
self.nodes.append(node)
self.renderer.add_object(node.renderobject)
def get_node(self, id: int):
return self.nodes[id]
def remove_node(self, node: Node):
self.nodes.remove(node)
self.renderer.remove_object(node.renderobject)
class SceneManager():
def __init__(self):
self.scenes = {}
self.current_scene = None
def change_current_scene(self, new_scene):
if self.current_scene:
self.current_scene.stop()
self.scenes[new_scene].start()