-
Notifications
You must be signed in to change notification settings - Fork 1
/
world.py
65 lines (54 loc) · 1.82 KB
/
world.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
64
65
from agents import Car, Pedestrian, RectangleBuilding
from entities import Entity
from typing import Union
from visualizer import Visualizer
class World:
def __init__(self, dt: float, width: float, height: float, ppm: int = 8): # ppm: float (original)
self.dynamic_agents = []
self.static_agents = []
self.t = 0 # simulation time
self.dt = dt # simulation time step
self.width = width
self.height = height
self.visualizer = Visualizer(width, height, ppm=ppm)
def add(self, entity: Entity):
if entity.movable:
self.dynamic_agents.append(entity)
else:
self.static_agents.append(entity)
def tick(self):
for agent in self.dynamic_agents:
agent.tick(self.dt)
self.t += self.dt
def render(self):
self.visualizer.create_window(bg_color='gray')
self.visualizer.update_agents(self.agents)
@property
def agents(self):
return self.static_agents + self.dynamic_agents
def collision_exists(self, agent=None):
if agent is None:
for i in range(len(self.dynamic_agents)):
for j in range(i + 1, len(self.dynamic_agents)):
if self.dynamic_agents[i].collidable and self.dynamic_agents[j].collidable:
if self.dynamic_agents[i].collidesWith(self.dynamic_agents[j]):
return True
for j in range(len(self.static_agents)):
if self.dynamic_agents[i].collidable and self.static_agents[j].collidable:
if self.dynamic_agents[i].collidesWith(self.static_agents[j]):
return True
return False
if not agent.collidable:
return False
for i in range(len(self.agents)):
if self.agents[i] is not agent and self.agents[i].collidable and agent.collidesWith(self.agents[i]):
return True
return False
def close(self):
self.reset()
self.static_agents = []
if self.visualizer.window_created:
self.visualizer.close()
def reset(self):
self.dynamic_agents = []
self.t = 0