import random import math from Entities.entity import Entity class Marine(Entity): movement = 0 view = 3 def update(self): self.centered_position = (self.iso_x, self.iso_y) self.cast_rays_to_distance(6) 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, 3)}.wav" if not self.engine.cmd_sound_effects: self.graphics.play_sound(sound_file) self.engine.cmd_sound_effects = True def move(self): # Aquisisci la posizione dell'unità self.iso_x, self.iso_y = self.graphics.iso_transform(self.x, self.y) self.position = (self.x, self.y) # Controlla se l'unità èha raggiunto la cella di destinazione if self.target_cell != (self.x, self.y): # Contolla che la cella target non sia occupata da una unità che abbia quella cella come target if self.engine.entities_positions.get(self.target_cell): if self.engine.entities_positions.get(self.target_cell).target_cell == self.target_cell and self.engine.entities_positions.get(self.target_cell) != self: #calcola un nuovo target self.target_cell = self.graphics.get_next_cell(self.target_cell, self.position, ignore_units=True) if not self.target_cell: self.target_cell = self.position # Se non ci sono celle disponibili, ferma l'unità return # Aquisisci la posizione a schermo della cella di destinazione e disegna lì un quadrato rosso 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) # Se la cella successiva è la stessa posizione dell'unità, calcola la prossima cella # (in caso contrario sarebbe ancora in movimento parziale) if self.position == self.next_cell: self.next_cell = self.graphics.get_next_cell(self.next_cell, self.target_cell) # Se l'algoritmo ha efettivamente trovato una cella, occupa la posizione if self.next_cell: self.engine.entities_positions[self.next_cell] = self # Se la cella di destinazione è occupata da un'altra entità, fermati else: self.action = "idle" self.next_cell = self.position return # 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.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 if self.movement >= 1.0: # Reset movement and set to idle self.movement = 0 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): # Prima verifica che le coordinate siano all'interno dei limiti della mappa map_height = len(self.engine.map) map_width = len(self.engine.map[0]) x, y = target_cell # Verifica che le coordinate siano valide if not (0 <= y < map_height and 0 <= x < map_width): if not self.engine.cmd_sound_effects: self.engine.graphics.play_sound("sounds/perror.wav") self.engine.cmd_sound_effects = True return # Ora puoi controllare il contenuto della cella in sicurezza if self.engine.map[y][x]["wall"]: if not self.engine.cmd_sound_effects: self.engine.graphics.play_sound("sounds/perror.wav") self.engine.cmd_sound_effects = True return self.target_cell = target_cell if not self.engine.cmd_sound_effects: self.graphics.play_sound(f"marine/tmayes0{random.randint(0, 3)}.wav") self.engine.cmd_sound_effects = True def cast_rays_to_distance(self, distance): # Cast rays to check for visibility done_cells = {} for angle in range(0, 360, 20): for i in range(distance, 0, -1): ray_x = int(self.centered_position[0] + (distance -i) * int(self.engine.graphics.cell_size/3) * math.sin(math.radians(angle))) ray_y = int(self.centered_position[1] - (distance - i)* int(self.engine.graphics.cell_size/6) * math.cos(math.radians(angle))) v_x, v_y = self.graphics.inv_iso_transform(ray_x, ray_y) if v_x>= 0 and v_x < len(self.engine.map[0]) and v_y >= 0 and v_y < len(self.engine.map): if (v_x, v_y) not in done_cells.keys(): done_cells.update({(v_x, v_y): self.engine.map_shadow[v_y][v_x]}) self.engine.map[v_y][v_x]["visited"] = True self.engine.map_shadow[v_y][v_x] = 0 if i == 1: self.engine.map_shadow[v_y][v_x] = max(done_cells.get((v_x, v_y), 0)-0.5, 0) print(done_cells.get((v_x, v_y), 0)) #self.graphics.draw_line((self.centered_position[0], self.centered_position[1], ray_x, ray_y), color=(0, 255, 0, 255)) continue