generated from mwc/project_game
94 lines
3.0 KiB
Python
94 lines
3.0 KiB
Python
from common import PLAYER_COLORS
|
|
|
|
|
|
class Token:
|
|
def __init__(self, player, number, pad):
|
|
self.home_position = (pad+number-1, pad+20+player+2)
|
|
self.position = self.home_position
|
|
self.player = player
|
|
self.color = PLAYER_COLORS[player]
|
|
self.character = str(number)
|
|
self.name = f'{player}_{number}'
|
|
self.z = 10
|
|
self.path = 'none'
|
|
self.step = -1
|
|
|
|
def go_home(self):
|
|
self.position = self.home_position
|
|
self.path = 'none'
|
|
self.step = -1
|
|
|
|
def step_forward(self, game):
|
|
if self.path == 'peri':
|
|
path_to_search = 'peri'
|
|
self.step = self.step + 1
|
|
if self.step == 20:
|
|
self.step = 0
|
|
elif self.path == 'diag1':
|
|
path_to_search = 'diag1'
|
|
if self.step == 5:
|
|
self.step = 0
|
|
self.path = 'peri'
|
|
path_to_search = 'peri'
|
|
else:
|
|
self.step = self.step + 1
|
|
elif self.path == 'diag2':
|
|
path_to_search = 'diag2'
|
|
if self.step == 5:
|
|
self.step = 15
|
|
self.path = 'peri'
|
|
path_to_search = 'peri'
|
|
else:
|
|
self.step = self.step + 1
|
|
elif self.path == 'diag2rev':
|
|
path_to_search = 'diag2'
|
|
if self.step == 1:
|
|
self.step = 5
|
|
self.path = 'peri'
|
|
path_to_search = 'peri'
|
|
else:
|
|
self.step = self.step - 1
|
|
elif self.path == 'diag1rev':
|
|
path_to_search = 'diag1'
|
|
if self.step == 1:
|
|
self.step = 10
|
|
self.path = 'peri'
|
|
path_to_search = 'peri'
|
|
else:
|
|
self.step = self.step - 1
|
|
|
|
tile_name = f'{path_to_search}_{self.step}'
|
|
game.log(f'Going to tile {tile_name}')
|
|
dest_tile = game.get_agent_by_name(tile_name)
|
|
self.position = dest_tile.position
|
|
self.dest_position = dest_tile.position
|
|
|
|
def kick_out_enemy_tokens(self, game):
|
|
tokens = [
|
|
agent for agent in game.agents
|
|
if isinstance(agent, Token)
|
|
and self.path == agent.path
|
|
and self.step == agent.step
|
|
]
|
|
for token in tokens:
|
|
if token.player != self.player:
|
|
token.go_home()
|
|
|
|
def adjust_position(self, game):
|
|
tokens = [
|
|
agent for agent in game.agents
|
|
if isinstance(agent, Token)
|
|
and self.path == agent.path
|
|
and self.step == agent.step
|
|
]
|
|
if len(tokens) == 1:
|
|
return
|
|
max_x = max([token.position[0] for token in tokens])
|
|
min_y = min([token.position[1] for token in tokens])
|
|
if self.path == 'peri':
|
|
if self.step >= 0 and self.step < 5 or self.step > 10 and self.step < 15:
|
|
new_position = (self.position[0], min_y - 1)
|
|
self.position = new_position
|
|
|
|
|