Implement marine unit visibility and movement logic; add map loading from JSON

This commit is contained in:
Matteo Benedetto
2025-05-15 22:14:47 +02:00
parent 9a1cdb9ced
commit 20bb62ee97
11 changed files with 302 additions and 71 deletions
+35 -7
View File
@@ -3,23 +3,45 @@ from Entities.entity import Entity
class Marine(Entity):
next_cell = (1,1)
target_cell = (8,7)
movement = 0
view = 3
def update(self):
self.move()
super().update()
def set_visibility(self):
# Set visibility based on the distance to the cursor
distance = self.graphics.get_distance((self.x, self.y), self.engine.cursor_pos)
if distance < self.view:
self.graphics.set_opacity(self.frame, 1.0)
else:
self.graphics.set_opacity(self.frame, 0.5)
def select_unit(self):
self.selected = True
# Play a random voice response when selected
sound_file = f"marine/tmawht0{random.randint(0, 4)}.wav"
sound_file = f"marine/tmawht0{random.randint(0, 3)}.wav"
print(f"Playing sound: {sound_file}")
self.graphics.play_sound(sound_file)
def move(self):
if (self.x, self.y) != self.next_cell:
self.iso_x, self.iso_y = self.graphics.iso_transform(self.x, self.y)
self.position = (self.x, self.y)
if self.target_cell != (self.x, self.y) and self.selected:
target_iso_x, target_iso_y = self.graphics.iso_transform(self.target_cell[0], self.target_cell[1])
self.graphics.draw_square(target_iso_x, target_iso_y, color=(255, 0, 0, 255), margin=6)
if self.position != self.target_cell:
if self.position == self.next_cell:
self.next_cell = self.graphics.get_next_cell(self.next_cell, self.target_cell)
self.engine.entities_positions[self.next_cell] = self
if self.engine.entities_positions.get(self.target_cell) is not None:
if self.engine.entities_positions.get(self.target_cell) != self:
self.target_cell = self.graphics.find_neighbors(self.target_cell)[0] or self.position
return
# Set walking animation and direction
self.action = "walk"
self.direction = self.graphics.get_direction((self.x, self.y), self.next_cell)
@@ -29,15 +51,21 @@ class Marine(Entity):
target_x, target_y = self.graphics.iso_transform(self.next_cell[0], self.next_cell[1])
# Increment movement counter
self.movement += 0.01
self.movement += 0.1
# 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)
self.x, self.y = self.next_cell
self.iso_x, self.iso_y = self.graphics.iso_transform(self.x, self.y)
else:
self.action = "idle"
def set_target_cell(self, target_cell):
self.target_cell = target_cell
self.graphics.play_sound(f"marine/tmayes0{random.randint(0, 3)}.wav")
+10 -5
View File
@@ -5,6 +5,7 @@ class Entity:
self.graphics = engine.graphics
self.x = x
self.y = y
self.next_cell = (x, y)
self.iso_x, self.iso_y = self.graphics.iso_transform(self.x, self.y)
self.action = action
self.direction = direction
@@ -15,15 +16,19 @@ class Entity:
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)
color = (0, 255, 0, 255)
if self.selected:
self.graphics.draw_square(self.iso_x, self.iso_y, color=color, margin=4)
# Draw target indicator if target is set
occlusion = self.graphics.map_shadow[self.y][self.x]
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)