diff --git a/Entities/Units/marine.py b/Entities/Units/marine.py index 1beb523..5d57fa5 100644 --- a/Entities/Units/marine.py +++ b/Entities/Units/marine.py @@ -24,24 +24,43 @@ class Marine(Entity): self.selected = True # Play a random voice response when selected sound_file = f"marine/tmawht0{random.randint(0, 3)}.wav" - print(f"Playing sound: {sound_file}") - self.graphics.play_sound(sound_file) + 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) - if self.target_cell != (self.x, self.y) and self.selected: + # 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) - 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) + + # 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 - 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 + # 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) @@ -67,5 +86,27 @@ class Marine(Entity): 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 - self.graphics.play_sound(f"marine/tmayes0{random.randint(0, 3)}.wav") \ No newline at end of file + 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 \ No newline at end of file diff --git a/assets/Sounds/sounds/burrowdn.wav b/assets/Sounds/sounds/burrowdn.wav new file mode 100644 index 0000000..fad6085 Binary files /dev/null and b/assets/Sounds/sounds/burrowdn.wav differ diff --git a/assets/Sounds/sounds/burrowup.wav b/assets/Sounds/sounds/burrowup.wav new file mode 100644 index 0000000..6d27f50 Binary files /dev/null and b/assets/Sounds/sounds/burrowup.wav differ diff --git a/assets/Sounds/sounds/button.wav b/assets/Sounds/sounds/button.wav new file mode 100644 index 0000000..7bbc2c0 Binary files /dev/null and b/assets/Sounds/sounds/button.wav differ diff --git a/assets/Sounds/sounds/buzz.wav b/assets/Sounds/sounds/buzz.wav new file mode 100644 index 0000000..bdc0018 Binary files /dev/null and b/assets/Sounds/sounds/buzz.wav differ diff --git a/assets/Sounds/sounds/explo1.wav b/assets/Sounds/sounds/explo1.wav new file mode 100644 index 0000000..1adec9c Binary files /dev/null and b/assets/Sounds/sounds/explo1.wav differ diff --git a/assets/Sounds/sounds/explo2.wav b/assets/Sounds/sounds/explo2.wav new file mode 100644 index 0000000..1e6945c Binary files /dev/null and b/assets/Sounds/sounds/explo2.wav differ diff --git a/assets/Sounds/sounds/explo3.wav b/assets/Sounds/sounds/explo3.wav new file mode 100644 index 0000000..d1cc920 Binary files /dev/null and b/assets/Sounds/sounds/explo3.wav differ diff --git a/assets/Sounds/sounds/explo4.wav b/assets/Sounds/sounds/explo4.wav new file mode 100644 index 0000000..f7c66ad Binary files /dev/null and b/assets/Sounds/sounds/explo4.wav differ diff --git a/assets/Sounds/sounds/explo5.wav b/assets/Sounds/sounds/explo5.wav new file mode 100644 index 0000000..77b4466 Binary files /dev/null and b/assets/Sounds/sounds/explo5.wav differ diff --git a/assets/Sounds/sounds/explolrg.wav b/assets/Sounds/sounds/explolrg.wav new file mode 100644 index 0000000..70986e4 Binary files /dev/null and b/assets/Sounds/sounds/explolrg.wav differ diff --git a/assets/Sounds/sounds/explomed.wav b/assets/Sounds/sounds/explomed.wav new file mode 100644 index 0000000..0a0b35f Binary files /dev/null and b/assets/Sounds/sounds/explomed.wav differ diff --git a/assets/Sounds/sounds/explosm.wav b/assets/Sounds/sounds/explosm.wav new file mode 100644 index 0000000..324001e Binary files /dev/null and b/assets/Sounds/sounds/explosm.wav differ diff --git a/assets/Sounds/sounds/intonydus.wav b/assets/Sounds/sounds/intonydus.wav new file mode 100644 index 0000000..54329cb Binary files /dev/null and b/assets/Sounds/sounds/intonydus.wav differ diff --git a/assets/Sounds/sounds/land.wav b/assets/Sounds/sounds/land.wav new file mode 100644 index 0000000..5dd7732 Binary files /dev/null and b/assets/Sounds/sounds/land.wav differ diff --git a/assets/Sounds/sounds/liftoff.wav b/assets/Sounds/sounds/liftoff.wav new file mode 100644 index 0000000..a6b4509 Binary files /dev/null and b/assets/Sounds/sounds/liftoff.wav differ diff --git a/assets/Sounds/sounds/onfirsml.wav b/assets/Sounds/sounds/onfirsml.wav new file mode 100644 index 0000000..a895f1c Binary files /dev/null and b/assets/Sounds/sounds/onfirsml.wav differ diff --git a/assets/Sounds/sounds/outofgas.wav b/assets/Sounds/sounds/outofgas.wav new file mode 100644 index 0000000..8f4a453 Binary files /dev/null and b/assets/Sounds/sounds/outofgas.wav differ diff --git a/assets/Sounds/sounds/pbldgplc.wav b/assets/Sounds/sounds/pbldgplc.wav new file mode 100644 index 0000000..cd947b5 Binary files /dev/null and b/assets/Sounds/sounds/pbldgplc.wav differ diff --git a/assets/Sounds/sounds/perror.wav b/assets/Sounds/sounds/perror.wav new file mode 100644 index 0000000..ca86fb4 Binary files /dev/null and b/assets/Sounds/sounds/perror.wav differ diff --git a/assets/Sounds/sounds/prescue.wav b/assets/Sounds/sounds/prescue.wav new file mode 100644 index 0000000..13fb9bf Binary files /dev/null and b/assets/Sounds/sounds/prescue.wav differ diff --git a/assets/Sounds/sounds/pshtra00.wav b/assets/Sounds/sounds/pshtra00.wav new file mode 100644 index 0000000..b04134c Binary files /dev/null and b/assets/Sounds/sounds/pshtra00.wav differ diff --git a/assets/Sounds/sounds/pshtra01.wav b/assets/Sounds/sounds/pshtra01.wav new file mode 100644 index 0000000..8201887 Binary files /dev/null and b/assets/Sounds/sounds/pshtra01.wav differ diff --git a/assets/Sounds/sounds/ptesum00.wav b/assets/Sounds/sounds/ptesum00.wav new file mode 100644 index 0000000..9e2ade8 Binary files /dev/null and b/assets/Sounds/sounds/ptesum00.wav differ diff --git a/assets/Sounds/sounds/tbldgplc.wav b/assets/Sounds/sounds/tbldgplc.wav new file mode 100644 index 0000000..ae3db0b Binary files /dev/null and b/assets/Sounds/sounds/tbldgplc.wav differ diff --git a/assets/Sounds/sounds/tdrtra00.wav b/assets/Sounds/sounds/tdrtra00.wav new file mode 100644 index 0000000..a7f7a0a Binary files /dev/null and b/assets/Sounds/sounds/tdrtra00.wav differ diff --git a/assets/Sounds/sounds/tpwrdown.wav b/assets/Sounds/sounds/tpwrdown.wav new file mode 100644 index 0000000..ac0a022 Binary files /dev/null and b/assets/Sounds/sounds/tpwrdown.wav differ diff --git a/assets/Sounds/sounds/transmission.wav b/assets/Sounds/sounds/transmission.wav new file mode 100644 index 0000000..2d2610d Binary files /dev/null and b/assets/Sounds/sounds/transmission.wav differ diff --git a/assets/Sounds/sounds/trescue.wav b/assets/Sounds/sounds/trescue.wav new file mode 100644 index 0000000..f5bc389 Binary files /dev/null and b/assets/Sounds/sounds/trescue.wav differ diff --git a/assets/Sounds/sounds/uicwht00.wav b/assets/Sounds/sounds/uicwht00.wav new file mode 100644 index 0000000..a877418 Binary files /dev/null and b/assets/Sounds/sounds/uicwht00.wav differ diff --git a/assets/Sounds/sounds/unrwht00.wav b/assets/Sounds/sounds/unrwht00.wav new file mode 100644 index 0000000..9cc67b1 Binary files /dev/null and b/assets/Sounds/sounds/unrwht00.wav differ diff --git a/assets/Sounds/sounds/upiwht00.wav b/assets/Sounds/sounds/upiwht00.wav new file mode 100644 index 0000000..2ed1837 Binary files /dev/null and b/assets/Sounds/sounds/upiwht00.wav differ diff --git a/assets/Sounds/sounds/utmwht00.wav b/assets/Sounds/sounds/utmwht00.wav new file mode 100644 index 0000000..735fc44 Binary files /dev/null and b/assets/Sounds/sounds/utmwht00.wav differ diff --git a/assets/Sounds/sounds/youlose.wav b/assets/Sounds/sounds/youlose.wav new file mode 100644 index 0000000..ed5ac12 Binary files /dev/null and b/assets/Sounds/sounds/youlose.wav differ diff --git a/assets/Sounds/sounds/youwin.wav b/assets/Sounds/sounds/youwin.wav new file mode 100644 index 0000000..766d33d Binary files /dev/null and b/assets/Sounds/sounds/youwin.wav differ diff --git a/assets/Sounds/sounds/zbldgplc.wav b/assets/Sounds/sounds/zbldgplc.wav new file mode 100644 index 0000000..c33d42d Binary files /dev/null and b/assets/Sounds/sounds/zbldgplc.wav differ diff --git a/assets/Sounds/sounds/zovtra00.wav b/assets/Sounds/sounds/zovtra00.wav new file mode 100644 index 0000000..6694c20 Binary files /dev/null and b/assets/Sounds/sounds/zovtra00.wav differ diff --git a/assets/Sounds/sounds/zovtra01.wav b/assets/Sounds/sounds/zovtra01.wav new file mode 100644 index 0000000..765e832 Binary files /dev/null and b/assets/Sounds/sounds/zovtra01.wav differ diff --git a/assets/Sounds/sounds/zpwrdown.wav b/assets/Sounds/sounds/zpwrdown.wav new file mode 100644 index 0000000..1cf7b04 Binary files /dev/null and b/assets/Sounds/sounds/zpwrdown.wav differ diff --git a/assets/Sounds/sounds/zrescue.wav b/assets/Sounds/sounds/zrescue.wav new file mode 100644 index 0000000..a77dc8b Binary files /dev/null and b/assets/Sounds/sounds/zrescue.wav differ diff --git a/conf/keymap_game.json b/conf/keymap_game.json index ec12464..e18abf8 100644 --- a/conf/keymap_game.json +++ b/conf/keymap_game.json @@ -3,5 +3,7 @@ "keydown:up": "scroll_up", "keydown:down": "scroll_down", "keydown:left": "scroll_left", - "keydown:right": "scroll_right" + "keydown:right": "scroll_right", + "keydown:pageup": "zoom_in", + "keydown:pagedown": "zoom_out" } \ No newline at end of file diff --git a/engine_demo.py b/engine_demo.py index 0bcbd81..c2569a9 100644 --- a/engine_demo.py +++ b/engine_demo.py @@ -31,11 +31,18 @@ class GameEngine(UserControls): def run(self): running = True # Set a custom scale if needed - self.graphics.set_scaling_factor(0.50) # 50% scale + self.graphics.set_scaling_factor(1.50) # 50% scale self.entities.append(Marine("knight", 0, 0, "idle", 1, 1, self)) self.entities.append(Marine("knight", 5, 0, "idle", 1, 1, self)) + #5 more marines + self.entities.append(Marine("knight", 0, 5, "idle", 1, 1, self)) + self.entities.append(Marine("knight", 5, 5, "idle", 1, 1, self)) + self.entities.append(Marine("knight", 1,1, "idle", 1, 1, self)) + self.entities.append(Marine("knight", 2,2, "idle", 1, 1, self)) + self.entities.append(Marine("knight", 3,3, "idle", 1, 1, self)) while running: + self.cmd_sound_effects = False # Start the frame timer perf_counter = self.graphics.get_perf_counter() # Initialize the map shadow and entities positions diff --git a/enne2engine/controls.py b/enne2engine/controls.py index a51522c..5f43372 100644 --- a/enne2engine/controls.py +++ b/enne2engine/controls.py @@ -31,4 +31,10 @@ class UserControls: self.graphics.view_offset_x += 10 def scroll_right(self): - self.graphics.view_offset_x -= 10 \ No newline at end of file + self.graphics.view_offset_x -= 10 + + def zoom_in(self): + self.graphics.set_scaling_factor(self.graphics.scaling_factor + 0.1) + + def zoom_out(self): + self.graphics.set_scaling_factor(self.graphics.scaling_factor - 0.1) \ No newline at end of file diff --git a/enne2engine/isogeometry.py b/enne2engine/isogeometry.py index 7c345b2..ac83470 100644 --- a/enne2engine/isogeometry.py +++ b/enne2engine/isogeometry.py @@ -12,6 +12,8 @@ class IsometricGeometry: def get_direction(self, initial_coord, surrounding_coord): + if not surrounding_coord: + return 1 # Calcola la differenza tra le coordinate delta_x = surrounding_coord[0] - initial_coord[0] delta_y = surrounding_coord[1] - initial_coord[1] @@ -36,7 +38,7 @@ class IsometricGeometry: else: return 1 - def find_neighbors(self, coordinates): + def find_neighbors(self, coordinates, ignore_units=False): """ Find and return the cells adjacent to the cell at position (coordinates). @@ -58,8 +60,10 @@ class IsometricGeometry: # Skip walls if self.engine.map[new_y][new_x]["wall"]: continue - if self.engine.entities_positions.get((new_x, new_y)) is not None: - continue + if not ignore_units: + # Skip occupied cells + if self.engine.entities_positions.get((new_x, new_y)) is not None: + continue neighbors.append((new_x, new_y)) @@ -164,9 +168,9 @@ class IsometricGeometry: return -1 - def get_next_cell(self, start, target): + def get_next_cell(self, start, target, ignore_units=False): # Get the neighbors of the start cell - neighbors = self.find_neighbors(start) + neighbors = self.find_neighbors(start, ignore_units=ignore_units) # Get the closest neighbor to the target cell closest_neighbor = self.get_closest_neighbor(neighbors, target) return closest_neighbor \ No newline at end of file diff --git a/enne2engine/sdl2_wrapper.py b/enne2engine/sdl2_wrapper.py index 4645c06..a4afc80 100644 --- a/enne2engine/sdl2_wrapper.py +++ b/enne2engine/sdl2_wrapper.py @@ -25,6 +25,7 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui): self.base_cell_size = 132 # Original/base cell size self.scaling_factor = 0.5 # Default scaling factor (can be changed) self.cell_size = int(self.base_cell_size * self.scaling_factor) # Effective cell size + self.ground_level_offset = self.base_cell_size // 4 self.view_offset_x = 400 self.view_offset_y = 0 self.surface_width = 0 @@ -169,19 +170,20 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui): self.apply_texture_color_mdod(tilesheet_texture, shadow) # Adjusted vertical offset calculation for half-sized cells - vertical_offset = self.cell_size - tile_rect[3] // 2 - self.cell_size//4 - if vertical_offset < 0: - vertical_offset = 0 + vertical_offset = (self.cell_size - tile_rect[3] * self.scaling_factor - self.ground_level_offset * self.scaling_factor) + + print(f"Vertical offset: {vertical_offset}") + # Adjusted horizontal offset calculation for half-sized cells - horizontal_offset = (self.cell_size - tile_rect[2] // 2) + horizontal_offset = (self.cell_size - tile_rect[2] * self.scaling_factor ) iso_x, iso_y = self.iso_transform(x, y) - iso_y += vertical_offset - iso_x += horizontal_offset - tile_rect[2] // 4 # Divided by 4 instead of 2 + iso_y += vertical_offset + iso_x += (horizontal_offset - tile_rect[2] * self.scaling_factor // 2) # Scale the actual tile rectangle based on the current scaling factor - dst_rect = (iso_x, iso_y, + dst_rect = (int(iso_x), int(iso_y), int(tile_rect[2] * self.scaling_factor), int(tile_rect[3] * self.scaling_factor)) @@ -291,8 +293,10 @@ class SDL2Wrapper(IsometricGeometry, SDL2Gui): Args: factor (float): The scaling factor (0.5 = half size, 1.0 = original size, etc.) """ - self.scaling_factor = factor - self.cell_size = int(self.base_cell_size * self.scaling_factor) + if factor > 0: + print(f"Scaling factor set to {factor}") + self.scaling_factor = factor + self.cell_size = int(self.base_cell_size * self.scaling_factor) def play_sound(self, sound_file): """ diff --git a/sdl2_wrapper.py b/sdl2_wrapper.py deleted file mode 100644 index 1e2bb40..0000000 --- a/sdl2_wrapper.py +++ /dev/null @@ -1,132 +0,0 @@ -from enne2engine.sdl2_wrapper import SDL2Wrapper -from enne2engine.pyglet_wrapper import PygletWrapper -from enne2engine.controls import UserControls -import sys -import os -import json -from Entities.Units.marine import Marine - - -class GameEngine(UserControls): - def __init__(self): - super().__init__() - self.graphics = SDL2Wrapper(self) - - # Load map from JSON file - try: - with open("assets/maps/map.json", "r") as map_file: - self.map = json.load(map_file) - except (FileNotFoundError, json.JSONDecodeError) as e: - print(f"Error loading map file: {e}") - print("Exiting program.") - sys.exit(0) - - self.frame_time = 0 - self.cursor_pos = (0, 0) - - self.load_assets() - self.entities = [] - self.entities_positions = {} - - def run(self): - running = True - # Set a custom scale if needed - self.graphics.set_scaling_factor(0.75) # 50% scale - - self.entities.append(Marine("knight", 0, 0, "idle", 1, 1, self)) - while running: - # Start the frame timer - perf_counter = self.graphics.get_perf_counter() - # Initialize the map shadow and entities positions - self.map_shadow = [ [0 for _ in range(len(self.map[0]))] for _ in range(len(self.map)) ] - self.entities_positions.clear() - for entity in self.entities: - self.entities_positions[(entity.x, entity.y)] = entity - # Create the map background texture with tiles - self.graphics.create_background(self.map, "tiles", self.map_shadow) - - # Handle events - event = self.graphics.handle_events() - if event: - #print(f"Event detected: {event}") - if event.startswith("MOUSEDOWN"): - pass - elif event.startswith("SELECTION"): - # Handle multiple unit selection - self.select_units_in_area(event) - elif event.startswith("MOUSEUP"): - self.select_entity_at_cursor() - self.handle_events("keymap_game", event) - - running = False if event == "QUIT" else True - - self.graphics.clear_screen() - self.graphics.render_background() - self.cursor_pos = self.graphics.draw_cursor() - - # Draw the selection rectangle if selecting - if self.graphics.is_mouse_button_pressed(1): - self.graphics.draw_selection_rectangle() - for entity in self.entities: - entity.update() - self.graphics.render_sprites() - self.graphics.update_status(f"Frame time: {round(self.frame_time)}ms - FPS: {round(1000/self.frame_time if self.frame_time != 0 else 1)}") - self.graphics.present_renderer() - self.frame_time = self.graphics.get_frame_time(perf_counter) - self.graphics.delay_frame(self.frame_time,50) - self.graphics.quit() - - def set_cursor(self, x, y): - self.graphics.cursor = (x, y) - - def load_assets(self): - self.graphics.load_tilesheet("tiles", "assets/tiles/landscapeTiles_sheet.png") - for dir in os.listdir("assets/KnightBasic"): - for file in os.listdir(f"assets/KnightBasic/{dir}"): - if file.endswith(".json"): - self.graphics.load_spritesheet(file[:-5].lower(), f"assets/KnightBasic/{dir}/{file}") - - def select_entity_at_cursor(self): - cursor_x, cursor_y = self.cursor_pos - print(f"Cursor position: {cursor_x}, {cursor_y}") - # First deselect all entities - for entity in self.entities: - entity.selected = False - - # Then select the entity at cursor position, if any - entity = self.entities_positions.get((cursor_x, cursor_y)) - if entity: - entity.select_unit() - print(f"Selected entity at cursor: {entity.asset} at position {entity.x}, {entity.y}") - else: - print("No entity selected at cursor position.") - - def select_units_in_area(self, selection_event): - """Select all units within the specified selection area.""" - # Parse selection coordinates - _, start_x, start_y, end_x, end_y = selection_event.split(":") - start_x, start_y, end_x, end_y = int(start_x), int(start_y), int(end_x), int(end_y) - - # Calculate selection rectangle in screen coordinates - min_x = min(start_x, end_x) - max_x = max(start_x, end_x) - min_y = min(start_y, end_y) - max_y = max(start_y, end_y) - - # First deselect all entities - for entity in self.entities: - entity.selected = False - - # Select entities within the rectangle - for entity in self.entities: - # Convert entity position to screen coordinates - screen_x, screen_y = self.graphics.iso_transform(entity.x, entity.y) - - # Check if entity is within selection rectangle - if min_x <= screen_x <= max_x and min_y <= screen_y <= max_y: - entity.select_unit() - print(f"Selected entity in area: {entity.asset} at position {entity.x}, {entity.y}") - -if __name__ == "__main__": - engine = GameEngine() - engine.run() \ No newline at end of file