fixed rendering order for agent view

This commit is contained in:
romue 2021-05-18 16:48:45 +02:00
parent e25290ad41
commit a5286d9de6
3 changed files with 11 additions and 7 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 274 KiB

After

Width:  |  Height:  |  Size: 38 KiB

View File

@ -2,6 +2,7 @@ import sys
from dataclasses import dataclass
import numpy as np
from pathlib import Path
from collections import deque
import pygame
@ -14,8 +15,9 @@ class Entity:
class Renderer:
BG_COLOR = (99, 110, 114)
BG_COLOR = (178, 190, 195)#(99, 110, 114)
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):
self.grid_h = grid_h
@ -69,16 +71,18 @@ class Renderer:
pygame.quit()
sys.exit()
self.fill_bg()
blits = deque()
for asset, entities in pos_dict.items():
for entity in entities:
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)
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, self.WHITE, shape_surf.get_rect())
shape_surf.set_alpha(70)
self.screen.blit(shape_surf, visibility_rect)
self.screen.blit(**bp)
pygame.draw.rect(shape_surf, self.AGENT_VIEW_COLOR, shape_surf.get_rect())
shape_surf.set_alpha(64)
blits.appendleft(dict(source=shape_surf, dest=visibility_rect))
blits.append(bp)
for blit in blits: self.screen.blit(**blit)
pygame.display.flip()
self.clock.tick(self.fps)

View File

@ -39,7 +39,7 @@ class GettingDirty(BaseFactory):
height, width = self.state.shape[1:]
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)]
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)\