mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-05-23 07:16:44 +02:00
fixed rendering order for agent view
This commit is contained in:
parent
e25290ad41
commit
a5286d9de6
Binary file not shown.
Before Width: | Height: | Size: 274 KiB After Width: | Height: | Size: 38 KiB |
@ -2,6 +2,7 @@ import sys
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from collections import deque
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
@ -14,8 +15,9 @@ class Entity:
|
|||||||
|
|
||||||
|
|
||||||
class Renderer:
|
class Renderer:
|
||||||
BG_COLOR = (99, 110, 114)
|
BG_COLOR = (178, 190, 195)#(99, 110, 114)
|
||||||
WHITE = (200, 200, 200)
|
WHITE = (200, 200, 200)
|
||||||
|
AGENT_VIEW_COLOR = (9, 132, 227)
|
||||||
|
|
||||||
def __init__(self, grid_w=16, grid_h=16, cell_size=40, fps=4, grid_lines=True, view_radius=2):
|
def __init__(self, grid_w=16, grid_h=16, cell_size=40, fps=4, grid_lines=True, view_radius=2):
|
||||||
self.grid_h = grid_h
|
self.grid_h = grid_h
|
||||||
@ -69,16 +71,18 @@ class Renderer:
|
|||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
self.fill_bg()
|
self.fill_bg()
|
||||||
|
blits = deque()
|
||||||
for asset, entities in pos_dict.items():
|
for asset, entities in pos_dict.items():
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
bp = self.blit_params(entity)
|
bp = self.blit_params(entity)
|
||||||
if 'agent' in asset and self.view_radius > 0:
|
if 'agent' in entity.name and self.view_radius > 0:
|
||||||
visibility_rect = bp['dest'].inflate((self.view_radius*2)*self.cell_size, (self.view_radius*2)*self.cell_size)
|
visibility_rect = bp['dest'].inflate((self.view_radius*2)*self.cell_size, (self.view_radius*2)*self.cell_size)
|
||||||
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
|
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
|
||||||
pygame.draw.rect(shape_surf, self.WHITE, shape_surf.get_rect())
|
pygame.draw.rect(shape_surf, self.AGENT_VIEW_COLOR, shape_surf.get_rect())
|
||||||
shape_surf.set_alpha(70)
|
shape_surf.set_alpha(64)
|
||||||
self.screen.blit(shape_surf, visibility_rect)
|
blits.appendleft(dict(source=shape_surf, dest=visibility_rect))
|
||||||
self.screen.blit(**bp)
|
blits.append(bp)
|
||||||
|
for blit in blits: self.screen.blit(**blit)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
self.clock.tick(self.fps)
|
self.clock.tick(self.fps)
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ class GettingDirty(BaseFactory):
|
|||||||
height, width = self.state.shape[1:]
|
height, width = self.state.shape[1:]
|
||||||
self.renderer = Renderer(width, height, view_radius=2)
|
self.renderer = Renderer(width, height, view_radius=2)
|
||||||
|
|
||||||
dirt = [Entity('dirt', [x, y], min(1.5*self.state[DIRT_INDEX, x, y], 1), 'opacity')
|
dirt = [Entity('dirt', [x, y], min(0.15+self.state[DIRT_INDEX, x, y], 1.5), 'scale')
|
||||||
for x, y in np.argwhere(self.state[DIRT_INDEX] > h.IS_FREE_CELL)]
|
for x, y in np.argwhere(self.state[DIRT_INDEX] > h.IS_FREE_CELL)]
|
||||||
walls = [Entity('wall', pos) for pos in np.argwhere(self.state[h.LEVEL_IDX] > h.IS_FREE_CELL)]
|
walls = [Entity('wall', pos) for pos in np.argwhere(self.state[h.LEVEL_IDX] > h.IS_FREE_CELL)]
|
||||||
asset_str = lambda agent: f'agent{agent.i+1}violation' if (not agent.action_valid or agent.collision_vector[h.LEVEL_IDX] > 0)\
|
asset_str = lambda agent: f'agent{agent.i+1}violation' if (not agent.action_valid or agent.collision_vector[h.LEVEL_IDX] > 0)\
|
||||||
|
Loading…
x
Reference in New Issue
Block a user