Files
connect-the-current/game/cell.py

57 lines
2.1 KiB
Python

import arcade, arcade.gui
from utils.constants import ROTATIONS, NEIGHBOURS
from utils.preload import TEXTURE_MAP
def get_opposite(direction):
if direction == "l":
return "r"
elif direction == "r":
return "l"
elif direction == "t":
return "b"
elif direction == "b":
return "t"
class Cell(arcade.Sprite):
def __init__(self, cell_type, x, y, left_neighbour, top_neighbour):
super().__init__(TEXTURE_MAP[cell_type, ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross", cell_type == "power_source"], center_x=x, center_y=y)
self.rotation = ROTATIONS[cell_type][0] if cell_type in ROTATIONS else "cross"
self.cell_type = cell_type
self.powered = False
self.left_neighbour, self.top_neighbour = left_neighbour, top_neighbour
self.right_neighbour, self.bottom_neighbour = None, None
def get_neighbour(self, name):
if name == "l":
return self.left_neighbour
elif name == "r":
return self.right_neighbour
elif name == "b":
return self.bottom_neighbour
elif name == "t":
return self.top_neighbour
def get_connected_neighbours(self, include_houses=False):
return [
self.get_neighbour(neighbour_direction) for neighbour_direction in NEIGHBOURS[self.rotation]
if (
self.get_neighbour(neighbour_direction) and
(include_houses or self.get_neighbour(neighbour_direction).cell_type != "house") and
get_opposite(neighbour_direction) in NEIGHBOURS[self.get_neighbour(neighbour_direction).rotation]
)
]
def update_visual(self):
self.texture = TEXTURE_MAP[(self.cell_type, self.rotation, self.powered)]
def next_rotation(self):
current_index = ROTATIONS[self.cell_type].index(self.rotation)
if current_index + 1 == len(ROTATIONS[self.cell_type]):
self.rotation = ROTATIONS[self.cell_type][0]
else:
self.rotation = ROTATIONS[self.cell_type][current_index + 1]
self.update_visual()