forked from Cryz/battle_royale_sim
show map with players on telegram bot
This commit is contained in:
97
entities/gamemap.py
Normal file
97
entities/gamemap.py
Normal file
@@ -0,0 +1,97 @@
|
||||
import random as _random
|
||||
import copy as _copy
|
||||
from utils import logs as _logs
|
||||
|
||||
|
||||
class BrSimMap():
|
||||
|
||||
def __init__(self, players= None, items= None):
|
||||
self.players= players or []
|
||||
self.items= items or []
|
||||
self.world_width= 10 #seems a reasonable width for smartphones larger maps would go on a new line
|
||||
self.world_height= 25
|
||||
self.game_map= []
|
||||
self.field_sym= '🟩'
|
||||
self.player_male_sym= '♂️'
|
||||
self.player_female_sym= '♀️'
|
||||
self.player_nonbinary_sym= '⚧️'
|
||||
self.dead_player_sym= '💀'
|
||||
self.item_sym= '📦'
|
||||
self.mountain_sym = '⛰️'
|
||||
self.init_map_matrix()
|
||||
self.init_players_coordinates()
|
||||
self.init_items_coordinates()
|
||||
self.populate_map()
|
||||
|
||||
def init_map_matrix(self):
|
||||
# show a matrix representing the game's map
|
||||
# 🟩 is and empty cell
|
||||
# (tomorrow we can choose different colors for different locations
|
||||
# 🟠 this is a player (we could use different colors for different genders)
|
||||
# 📦 this is an item (weapon or another item)
|
||||
# 💀 this is icon when the player is dead
|
||||
# ⛰️ this is icon for the mountain (We can prevent players from passing through the mountains and thus use them for map boundaries.)
|
||||
|
||||
width= []
|
||||
mon = []
|
||||
for i in range(self.world_width):
|
||||
mon.append(self.mountain_sym)
|
||||
for i in range(self.world_width):
|
||||
if i == 0 or i == self.world_width - 1: width.append(self.mountain_sym)
|
||||
else: width.append(self.field_sym)
|
||||
for i in range(self.world_height):
|
||||
if i == 0 or i == self.world_height - 1: self.game_map.append(mon)
|
||||
else: self.game_map.append(_copy.deepcopy(width))
|
||||
_logs.log_debug(f'init_map_matrix: {self.game_map}')
|
||||
|
||||
def populate_map(self):
|
||||
for player in self.players:
|
||||
p_coord_x, p_coord_y= player.get_player_coordinates()
|
||||
if player.player_gender_is_male(): self.game_map[p_coord_y][p_coord_x]= self.player_male_sym
|
||||
elif player.player_gender_is_female(): self.game_map[p_coord_y][p_coord_x]= self.player_female_sym
|
||||
else: self.game_map[p_coord_y][p_coord_x]= self.player_nonbinary_sym
|
||||
for item in self.items:
|
||||
i_coord_x, i_coord_y= item.get_item_coordinates()
|
||||
self.game_map[i_coord_y][i_coord_x]= self.item_sym
|
||||
|
||||
def _set_coordinates(self, target):
|
||||
x= _random.randint(1, self.world_width -2) # -2 because 1 cell is occupied by the mountain
|
||||
y= _random.randint(1, self.world_height -2)
|
||||
while self.get_map_matrix()[y][x] != self.field_sym:
|
||||
print('init_players_coordinates: collision, regenerate coordinates')
|
||||
x= _random.randint(1, self.world_width -2)
|
||||
y= _random.randint(1, self.world_height -2)
|
||||
target.set_player_coordinates(x, y)
|
||||
|
||||
def init_players_coordinates(self):
|
||||
# XXX init random player.coord_x and player.coord_y (of course not already used coords)
|
||||
# parse all self.players and define random coordinates (player.coord_x, and player.coord_y)
|
||||
for player in self.players:
|
||||
self._set_coordinates(target)
|
||||
|
||||
def init_items_coordinates(self):
|
||||
# XXX init random item.coord_x and item.coord_y (of course not already used coords)
|
||||
# parse all self.items and define random coordinates (item.coord_x, and item.coord_y)
|
||||
for item in self.items:
|
||||
self._set_coordinates(item)
|
||||
|
||||
def add_player_to_map(self, player):
|
||||
self.players.append(player)
|
||||
self._set_coordinates(player)
|
||||
|
||||
def add_item_to_map(self, item):
|
||||
self.items.append(item)
|
||||
self._set_coordinates(item)
|
||||
|
||||
def get_map_matrix(self):
|
||||
return self.game_map
|
||||
|
||||
def get_renderized_map(self):
|
||||
res= ''
|
||||
self.populate_map()
|
||||
game_map= self.get_map_matrix()
|
||||
for y in game_map:
|
||||
for x in y:
|
||||
res+= x
|
||||
res+= '\n'
|
||||
return res
|
||||
Reference in New Issue
Block a user