-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.py
66 lines (48 loc) · 2.33 KB
/
env.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
66
# Author: Teshan Liyanage <teshanl@zone24x7.com>
import pygame
import numpy as np
from shape import BaseShape
colors = pygame.color.THECOLORS
class Env:
_intd = False
def __init__(self, size: tuple, block_factories: tuple, img_size: tuple):
assert not Env._intd, "Singleton class"
self.size = size
self.env_arr = np.ones(size, np.float32)
self.global_map = BaseShape(shape=img_size)
self.block_size = (self.size[0] // self.global_map.shape[0],
self.size[1] // self.global_map.shape[1])
print(f"Env size={self.size} map size={self.global_map.shape} block size={self.block_size}")
self.screen = pygame.display.set_mode(size)
self.block_factories = tuple(self.mpos2bpos(bf) for bf in block_factories)
self._placed_blocks = []
self._block_factory_rects = tuple(self.get_block_rect(p) for p in self.block_factories)
Env._intd = True
def reset(self):
self.global_map.reset()
self._placed_blocks = []
def bpos2mpos(self, pos) -> tuple:
"""Convert block position to map position"""
return (pos[0] * self.block_size[0] + self.block_size[0] // 2,
pos[1] * self.block_size[1] + self.block_size[1] // 2)
def mpos2bpos(self, pos) -> tuple:
"""Convert map position to block position"""
return int(pos[0] // self.block_size[0]), int(pos[1] // self.block_size[1])
def get_closest_block_factory(self, pos: tuple):
dists = [np.linalg.norm((x - pos[0], y - pos[1])) for x, y in self.block_factories]
return self.block_factories[np.argmin(dists)]
def is_occupied(self, pos: tuple):
return self.global_map[pos] == np.inf
def get_block_rect(self, pos: tuple):
sx, sy = pos[0] * self.block_size[0], pos[1] * self.block_size[1]
return sx, sy, *self.block_size
def place_block(self, pos: tuple):
self._placed_blocks.append(self.get_block_rect(pos))
self.global_map[pos] = np.inf
def blit(self):
self.screen.fill(colors['grey68'])
for b in self._placed_blocks:
pygame.draw.rect(self.screen, colors['black'], b)
for b in self._block_factory_rects:
pygame.draw.rect(self.screen, colors['red'], b)
pygame.draw.rect(self.screen, colors['brown'], b, 10)