|
| 1 | +# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +from point import Point |
| 5 | +from definitions import NONE, TYPE_BLOCK, TYPE_CHIP, NORTH, SOUTH, WEST, EAST |
| 6 | + |
| 7 | +DIR_UP = 0 |
| 8 | +DIR_LEFT = 1 |
| 9 | +DIR_DOWN = 2 |
| 10 | +DIR_RIGHT = 3 |
| 11 | + |
| 12 | +# creatures should move based on chip, tiles near them, and their own AI |
| 13 | +# creatures should be able to move in any direction assuming they are not blocked |
| 14 | + |
| 15 | +# Abstract class |
| 16 | +class Creature: |
| 17 | + def __init__(self, *, position=None, direction=NONE, creature_type=NONE): |
| 18 | + self.cur_pos = position or Point(0, 0) |
| 19 | + self.type = creature_type or TYPE_BLOCK |
| 20 | + self.direction = direction |
| 21 | + self.state = 0x00 |
| 22 | + self.hidden = False |
| 23 | + self.on_slip_list = False |
| 24 | + self.to_direction = NONE |
| 25 | + |
| 26 | + def move(self, destination): |
| 27 | + if destination.y < self.cur_pos.y: |
| 28 | + self.direction = NORTH |
| 29 | + elif destination.x < self.cur_pos.x: |
| 30 | + self.direction = WEST |
| 31 | + elif destination.y > self.cur_pos.y: |
| 32 | + self.direction = SOUTH |
| 33 | + elif destination.x > self.cur_pos.x: |
| 34 | + self.direction = EAST |
| 35 | + else: |
| 36 | + self.direction = NONE |
| 37 | + self.cur_pos = destination |
| 38 | + |
| 39 | + def image_number(self): |
| 40 | + tile_index = 0 |
| 41 | + if self.type == TYPE_CHIP: |
| 42 | + tile_index = 0x6C |
| 43 | + elif self.type == TYPE_BLOCK: |
| 44 | + tile_index = 0x0A |
| 45 | + else: |
| 46 | + tile_index = 0x40 + ((self.type - 1) * 4) |
| 47 | + |
| 48 | + if self.direction == WEST: |
| 49 | + tile_index += DIR_LEFT |
| 50 | + elif self.direction == EAST: |
| 51 | + tile_index += DIR_RIGHT |
| 52 | + elif self.direction == NORTH: |
| 53 | + tile_index += DIR_UP |
| 54 | + elif self.direction in (SOUTH, NONE): |
| 55 | + tile_index += DIR_DOWN |
| 56 | + return tile_index |
| 57 | + |
| 58 | + def get_tile_in_dir(self, direction): |
| 59 | + pt_dir = Point(self.cur_pos.x, self.cur_pos.y) |
| 60 | + if direction == WEST: |
| 61 | + pt_dir.x -= 1 |
| 62 | + elif direction == EAST: |
| 63 | + pt_dir.x += 1 |
| 64 | + elif direction == NORTH: |
| 65 | + pt_dir.y -= 1 |
| 66 | + elif direction == SOUTH: |
| 67 | + pt_dir.y += 1 |
| 68 | + return pt_dir |
| 69 | + |
| 70 | + def left(self): |
| 71 | + # return the point to the left of the creature |
| 72 | + pt_dest = Point(self.cur_pos.x, self.cur_pos.y) |
| 73 | + if self.direction == NORTH: |
| 74 | + pt_dest.x -= 1 |
| 75 | + elif self.direction == WEST: |
| 76 | + pt_dest.y += 1 |
| 77 | + elif self.direction == SOUTH: |
| 78 | + pt_dest.x += 1 |
| 79 | + elif self.direction == EAST: |
| 80 | + pt_dest.y -= 1 |
| 81 | + return pt_dest |
| 82 | + |
| 83 | + def right(self): |
| 84 | + # Return point to the right of the creature |
| 85 | + pt_dest = Point(self.cur_pos.x, self.cur_pos.y) |
| 86 | + if self.direction == NORTH: |
| 87 | + pt_dest.x += 1 |
| 88 | + elif self.direction == WEST: |
| 89 | + pt_dest.y -= 1 |
| 90 | + elif self.direction == SOUTH: |
| 91 | + pt_dest.x -= 1 |
| 92 | + elif self.direction == EAST: |
| 93 | + pt_dest.y += 1 |
| 94 | + return pt_dest |
| 95 | + |
| 96 | + def back(self): |
| 97 | + # Return point behind the creature |
| 98 | + pt_dest = Point(self.cur_pos.x, self.cur_pos.y) |
| 99 | + if self.direction == NORTH: |
| 100 | + pt_dest.y += 1 |
| 101 | + elif self.direction == WEST: |
| 102 | + pt_dest.x += 1 |
| 103 | + elif self.direction == SOUTH: |
| 104 | + pt_dest.y -= 1 |
| 105 | + elif self.direction == EAST: |
| 106 | + pt_dest.x -= 1 |
| 107 | + return pt_dest |
| 108 | + |
| 109 | + def front(self): |
| 110 | + # Return point in front of the creature |
| 111 | + pt_dest = Point(self.cur_pos.x, self.cur_pos.y) |
| 112 | + if self.direction == NORTH: |
| 113 | + pt_dest.y -= 1 |
| 114 | + elif self.direction == WEST: |
| 115 | + pt_dest.x -= 1 |
| 116 | + elif self.direction == SOUTH: |
| 117 | + pt_dest.y += 1 |
| 118 | + elif self.direction == EAST: |
| 119 | + pt_dest.x += 1 |
| 120 | + return pt_dest |
| 121 | + |
| 122 | + def reverse(self): |
| 123 | + if self.direction == NORTH: |
| 124 | + return SOUTH |
| 125 | + elif self.direction == SOUTH: |
| 126 | + return NORTH |
| 127 | + elif self.direction == WEST: |
| 128 | + return EAST |
| 129 | + elif self.direction == EAST: |
| 130 | + return WEST |
| 131 | + else: |
| 132 | + return self.direction |
0 commit comments