-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.py
36 lines (32 loc) · 1.07 KB
/
tile.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
import random
import math
class Tile(object):
def __init__(self, world, x, y, width, height, size):
self.world = world
self.x = x
self.y = y
self.width = width
self.height = height
self.size = size
self.nutrition = random.random()
self.agents = set()
def to_dict(self):
keys = "x", "y", "width", "height", "nutrition"
result = {key: self.__dict__[key] for key in keys}
result['n_agents'] = len(self.agents)
return result
def neighbors(self, distance):
nblk = math.ceil(distance / self.size)
x = int(self.x / self.size)
y = int(self.y / self.size)
tiles = []
for dx in range(-nblk, nblk+1):
for dy in range(-nblk, nblk+1):
nx = x + dx
ny = y + dy
if nx >= self.world.tiles_x:
nx -= self.world.tiles_x
if ny >= self.world.tiles_y:
ny -= self.world.tiles_y
tiles.append(self.world.tiles[nx][ny])
return tiles