2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.interfaces import Map
|
2020-11-10 17:08:06 +00:00
|
|
|
from .display import Display
|
2020-12-18 14:07:09 +00:00
|
|
|
from ..game import Game
|
2020-11-06 16:43:30 +00:00
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
class MapDisplay(Display):
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
A class to handle the display of the map.
|
|
|
|
"""
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-12-18 14:15:47 +00:00
|
|
|
map: Map
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
def __init__(self, *args):
|
|
|
|
super().__init__(*args)
|
|
|
|
|
2020-12-18 14:07:09 +00:00
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
self.map = game.map
|
|
|
|
self.pad = self.newpad(self.map.height,
|
|
|
|
self.pack.tile_width * self.map.width + 1)
|
2020-11-06 14:34:24 +00:00
|
|
|
|
2020-11-06 20:15:09 +00:00
|
|
|
def update_pad(self) -> None:
|
2020-12-18 16:04:45 +00:00
|
|
|
for j in range(len(self.map.tiles)):
|
|
|
|
for i in range(len(self.map.tiles[j])):
|
2020-12-18 20:21:00 +00:00
|
|
|
if not self.map.seen_tiles[j][i]:
|
|
|
|
continue
|
2021-01-07 15:49:40 +00:00
|
|
|
fg, bg = self.map.tiles[j][i].visible_color(self.pack) if \
|
|
|
|
self.map.visibility[j][i] else \
|
|
|
|
self.map.tiles[j][i].hidden_color(self.pack)
|
2020-12-18 16:04:45 +00:00
|
|
|
self.addstr(self.pad, j, self.pack.tile_width * i,
|
2021-01-07 15:49:40 +00:00
|
|
|
self.map.tiles[j][i].char(self.pack), fg, bg)
|
2020-11-06 14:34:24 +00:00
|
|
|
for e in self.map.entities:
|
2020-12-18 20:21:00 +00:00
|
|
|
if self.map.visibility[e.y][e.x]:
|
|
|
|
self.addstr(self.pad, e.y, self.pack.tile_width * e.x,
|
|
|
|
self.pack[e.name.upper()],
|
|
|
|
self.pack.entity_fg_color,
|
|
|
|
self.pack.entity_bg_color)
|
2020-12-10 21:21:09 +00:00
|
|
|
|
2020-12-12 12:46:45 +00:00
|
|
|
# Display Path map for debug purposes
|
2020-12-10 21:21:09 +00:00
|
|
|
# from squirrelbattle.entities.player import Player
|
|
|
|
# players = [ p for p in self.map.entities if isinstance(p,Player) ]
|
|
|
|
# player = players[0] if len(players) > 0 else None
|
|
|
|
# if player:
|
|
|
|
# for x in range(self.map.width):
|
|
|
|
# for y in range(self.map.height):
|
|
|
|
# if (y,x) in player.paths:
|
|
|
|
# deltay, deltax = (y - player.paths[(y, x)][0],
|
|
|
|
# x - player.paths[(y, x)][1])
|
|
|
|
# if (deltay, deltax) == (-1, 0):
|
|
|
|
# character = '↓'
|
|
|
|
# elif (deltay, deltax) == (1, 0):
|
|
|
|
# character = '↑'
|
|
|
|
# elif (deltay, deltax) == (0, -1):
|
|
|
|
# character = '→'
|
|
|
|
# else:
|
|
|
|
# character = '←'
|
|
|
|
# self.addstr(self.pad, y, self.pack.tile_width * x,
|
2020-12-12 12:46:45 +00:00
|
|
|
# character, self.pack.tile_fg_color,
|
|
|
|
# self.pack.tile_bg_color)
|
2020-11-06 14:34:24 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
def display(self) -> None:
|
2020-11-11 14:10:46 +00:00
|
|
|
y, x = self.map.currenty, self.pack.tile_width * self.map.currentx
|
2020-11-06 20:15:09 +00:00
|
|
|
deltay, deltax = (self.height // 2) + 1, (self.width // 2) + 1
|
|
|
|
pminrow, pmincol = y - deltay, x - deltax
|
2020-11-06 14:34:24 +00:00
|
|
|
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)
|
|
|
|
deltay, deltax = self.height - deltay, self.width - deltax
|
2020-11-06 20:15:09 +00:00
|
|
|
smaxrow = self.map.height - (y + deltay) + self.height - 1
|
|
|
|
smaxrow = min(smaxrow, self.height - 1)
|
2020-11-10 21:01:57 +00:00
|
|
|
smaxcol = self.pack.tile_width * self.map.width - \
|
|
|
|
(x + deltax) + self.width - 1
|
2020-11-26 20:59:48 +00:00
|
|
|
|
|
|
|
# Wrap perfectly the map according to the width of the tiles
|
|
|
|
pmincol = self.pack.tile_width * (pmincol // self.pack.tile_width)
|
|
|
|
smincol = self.pack.tile_width * (smincol // self.pack.tile_width)
|
|
|
|
smaxcol = self.pack.tile_width \
|
|
|
|
* (smaxcol // self.pack.tile_width + 1) - 1
|
|
|
|
|
2020-11-06 20:15:09 +00:00
|
|
|
smaxcol = min(smaxcol, self.width - 1)
|
|
|
|
pminrow = max(0, min(self.map.height, pminrow))
|
2020-11-10 21:01:57 +00:00
|
|
|
pmincol = max(0, min(self.pack.tile_width * self.map.width, pmincol))
|
2020-11-26 20:59:48 +00:00
|
|
|
|
2020-11-26 21:20:14 +00:00
|
|
|
self.pad.erase()
|
2020-11-06 14:34:24 +00:00
|
|
|
self.update_pad()
|
2020-11-26 19:58:46 +00:00
|
|
|
self.refresh_pad(self.pad, pminrow, pmincol, sminrow, smincol, smaxrow,
|
|
|
|
smaxcol)
|