Refactor code structure for improved readability and maintainability

This commit is contained in:
Matteo Benedetto
2025-05-14 20:41:34 +02:00
parent 6334445675
commit 882a4aac62
27 changed files with 426 additions and 192 deletions
View File
+43
View File
@@ -0,0 +1,43 @@
import random
from Entities.entity import Entity
class Marine(Entity):
next_cell = (1,1)
movement = 0
def update(self):
self.move()
super().update()
def select_unit(self):
self.selected = True
# Play a random voice response when selected
sound_file = f"marine/tmawht0{random.randint(0, 4)}.wav"
print(f"Playing sound: {sound_file}")
self.graphics.play_sound(sound_file)
def move(self):
if (self.x, self.y) != self.next_cell:
# Set walking animation and direction
self.action = "walk"
self.direction = self.graphics.get_direction((self.x, self.y), self.next_cell)
self.moving = True
# Calculate target coordinates
target_x, target_y = self.graphics.iso_transform(self.next_cell[0], self.next_cell[1])
# Increment movement counter
self.movement += 0.01
# Calculate how far we've moved (0.0 to 1.0)
move_progress = min(self.movement, 1.0)
# Calculate new position based on progress between cells
self.iso_x = self.iso_x + (target_x - self.iso_x) * move_progress
self.iso_y = self.iso_y + (target_y - self.iso_y) * move_progress
print(f"Moving to {self.iso_x}, {self.iso_y} with progress {move_progress}")
if self.movement >= 1.0:
# Reset movement and set to idle
self.movement = 0
self.iso_x, self.iso_y = self.graphics.iso_transform(self.x, self.y)
View File
+29
View File
@@ -0,0 +1,29 @@
class Entity:
def __init__(self, asset, x, y, action, direction, speed, engine):
self.asset = asset
self.graphics = engine.graphics
self.x = x
self.y = y
self.iso_x, self.iso_y = self.graphics.iso_transform(self.x, self.y)
self.action = action
self.direction = direction
self.speed = speed
self.frame = 0
self.engine = engine
self.selected = True
self.movement = 0
def update(self):
x, y = self.graphics.iso_transform(self.x, self.y)
occlusion = self.graphics.get_distance((self.x, self.y), self.engine.cursor_pos) / 4
# Set color based on selection status
color = (255, 255, 0, 255) if self.selected else (0, 255, 0, 255)
self.graphics.draw_square(self.x, self.y, color=color, margin=4)
if occlusion >= 0.8:
return
self.frame = self.graphics.render_sprite(f"{self.asset}_{self.action}_dir{self.direction}", self.iso_x, self.iso_y, self.frame, occlusion)