From 43813c824ebc43e2e509c4b54bf46785de5e8b4c Mon Sep 17 00:00:00 2001 From: Tipragot Date: Sun, 29 Oct 2023 19:49:06 +0100 Subject: [PATCH] =?UTF-8?q?Syst=C3=A8me=20de=20d=C3=A9placement=20des=20do?= =?UTF-8?q?ssiers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../animations/search_directory/info.json | 4 +- src/engine.py | 3 + src/main.py | 6 +- src/plugins/smooth.py | 2 +- src/scenes/directory_search.py | 174 ++++++++++++++++++ src/scenes/folder_hide.py | 53 ------ 6 files changed, 183 insertions(+), 59 deletions(-) create mode 100644 src/scenes/directory_search.py delete mode 100644 src/scenes/folder_hide.py diff --git a/assets/textures/animations/search_directory/info.json b/assets/textures/animations/search_directory/info.json index 10627ba..5aee946 100644 --- a/assets/textures/animations/search_directory/info.json +++ b/assets/textures/animations/search_directory/info.json @@ -1,8 +1,8 @@ { "end_image": "directory.png", "offset": { - "x": -48, - "y": -176 + "x": 8, + "y": -83 }, "frame_count": 268, "fps": 60 diff --git a/src/engine.py b/src/engine.py index 0c61496..5365097 100644 --- a/src/engine.py +++ b/src/engine.py @@ -433,6 +433,7 @@ class Mouse: self.pressed: set[int] = set() self.released: set[int] = set() self.position: Vec2 = Vec2(0.0, 0.0) + self.delta: Vec2 = Vec2(0.0, 0.0) def is_button_pressed(self, button: int) -> bool: """ @@ -670,6 +671,7 @@ def start_game( keyboard.released.clear() mouse.pressed.clear() mouse.released.clear() + last_position = Vec2(mouse.position) for event in pygame.event.get(): if event.type == pygame.QUIT: world[Game].stop() @@ -697,6 +699,7 @@ def start_game( ((event.pos[0] - rect[0]) / rect[2]) * Display.WIDTH, ((event.pos[1] - rect[1]) / rect[3]) * Display.HEIGHT, ) + mouse.delta = mouse.position - last_position # On vérifie le survol des textures et textes for entity in world.query(Position, Texture): diff --git a/src/main.py b/src/main.py index 546e02a..13a8888 100644 --- a/src/main.py +++ b/src/main.py @@ -4,14 +4,14 @@ Example de l'utilisation du moteur de jeu. from engine import start_game -from scenes import folder_hide, menu +from scenes import directory_search, menu start_game( { "menu": menu.SCENE, - "folder_hide": folder_hide.SCENE, + "directory_search": directory_search.SCENE, }, - "folder_hide", + "directory_search", title="Guess The Number", ) diff --git a/src/plugins/smooth.py b/src/plugins/smooth.py index 6858b2d..0f9681e 100644 --- a/src/plugins/smooth.py +++ b/src/plugins/smooth.py @@ -24,7 +24,7 @@ def __update_positions(world: World): for entity in world.query(Position, Target): position = entity[Position] target = entity[Target] - speed = entity[Speed] if Speed in entity else Speed(5) + speed = entity[Speed] if Speed in entity else Speed(10) entity[Position] = Position( position + (target - position) * world[Delta] * speed ) diff --git a/src/scenes/directory_search.py b/src/scenes/directory_search.py new file mode 100644 index 0000000..89d0a29 --- /dev/null +++ b/src/scenes/directory_search.py @@ -0,0 +1,174 @@ +""" +Scène du jeu dans lequel on se cache de Edmond dans les dossiers. +""" + +from enum import Enum +from engine import ( + Centered, + Display, + Entity, + Hovered, + Mouse, + Order, + Position, + Scene, + Texture, + Vec2, + World, +) +from plugins import smooth + + +__LINES = 3 +__COLUMNS = 5 + + +class State(Enum): + """ + Etat de la scène. + """ + + WAITING = 0 + SEARCHING = 1 + + +class SelectedDirectory: + """ + Une ressource qui stoque le dossier selectionné pour le déplacement. + """ + + def __init__(self, entity: Entity, start_position: Vec2): + self.entity = entity + self.position = start_position + + +class DirectoryPosition: + """ + La position d'un dossier dans la grille. + """ + + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + def __eq__(self, value: object) -> bool: + if isinstance(value, DirectoryPosition): + return self.x == value.x and self.y == value.y + return False + + +def __initialize_world(world: World): + """ + Initialise le monde de la scène. + """ + world[State] = State.WAITING + for y in range(__LINES): + for x in range(__COLUMNS): + world.create_entity( + Position(0, 0), + Order(1), + Centered(), + Texture("directory.png"), + # HoveredTexture("directory_hover.png"), + DirectoryPosition(x, y), + ) + + +def __move_directories(world: World): + """ + Permet de déplacer les dossiers avec la souris. + """ + mouse = world[Mouse] + for entity in world.query(Hovered, DirectoryPosition): + if mouse.is_button_pressed(1): + world[SelectedDirectory] = SelectedDirectory(entity, Vec2(mouse.position)) + break + + # Si un dossier est séléctionné + if SelectedDirectory in world: + selected_directory = world[SelectedDirectory] + selected_entity = selected_directory.entity + directory_position = selected_entity[DirectoryPosition] + + # Vérification du relachement de la souris + if not mouse.is_button(1): + del world[SelectedDirectory] + return + + # On calcule le déplacement de la souris + mouse_delta = mouse.position - selected_directory.position + + # On annule si il y a pas eu de déplacement significatif de la souris + if mouse_delta.length < 40: + return + + # Récupération du mouvement voulu + if abs(mouse_delta.x) >= abs(mouse_delta.y): + movement = (int(mouse_delta.x / abs(mouse_delta.x)), 0) + else: + movement = (0, int(mouse_delta.y / abs(mouse_delta.y))) + + # Récupération des mouvements possible du dossier + movements: list[tuple[int, int]] = [] + if directory_position.x != 0: + movements.append((-1, 0)) + if directory_position.x != __COLUMNS - 1: + movements.append((1, 0)) + if directory_position.y != 0: + movements.append((0, -1)) + if directory_position.y != __LINES - 1: + movements.append((0, 1)) + if len(movements) == 0: + return + + # Si le mouvement n'est pas possible, on annule + if movement not in movements: + return + + # On trouve l'autre dossier + for entity in world.query(DirectoryPosition): + if entity != selected_entity and entity[ + DirectoryPosition + ] == DirectoryPosition( + directory_position.x + movement[0], + directory_position.y + movement[1], + ): + other_directory = entity + break + else: + return + + # On actualise la position de l'autre dossier + other_directory[DirectoryPosition].x -= movement[0] + other_directory[DirectoryPosition].y -= movement[1] + + # On actualise la position du dossier + selected_entity[DirectoryPosition].x += movement[0] + selected_entity[DirectoryPosition].y += movement[1] + + # On retire le dossier selectionné + del world[SelectedDirectory] + + +def __update_positions(world: World): + """ + Met à jour la position cible des dossiers. + """ + for entity in world.query(DirectoryPosition): + position = entity[DirectoryPosition] + offset = Vec2(-300, -50) if world[State] == State.WAITING else Vec2(-300, -75) + size = Vec2(150, 100) if world[State] == State.WAITING else Vec2(150, 150) + first_position = Vec2(Display.WIDTH / 2, Display.HEIGHT / 2) + offset + entity[smooth.Target] = smooth.Target( + first_position + Vec2(position.x, position.y) * size + ) + + +SCENE = ( + Scene( + [__initialize_world], + [__move_directories, __update_positions], + [], + ) + + smooth.PLUGIN +) diff --git a/src/scenes/folder_hide.py b/src/scenes/folder_hide.py deleted file mode 100644 index 611e084..0000000 --- a/src/scenes/folder_hide.py +++ /dev/null @@ -1,53 +0,0 @@ -""" -Scène du jeu dans lequel on se cache de Edmond dans les dossiers. -""" - -from engine import Centered, Display, Order, Position, Scene, Texture, Vec2, World -from plugins import smooth - - -class FolderPosition: - """ - La position d'un dossier dans la grille. - """ - - def __init__(self, x: int, y: int): - self.x = x - self.y = y - - -def __initialize_world(world: World): - """ - Initialise le monde de la scène. - """ - for x in range(5): - for y in range(2): - world.create_entity( - Position(0, 0), - Order(1), - Centered(), - Texture("directory.png"), - FolderPosition(x, y), - ) - - -def __update_positions(world: World): - """ - Met à jour la position cible des dossiers. - """ - for entity in world.query(FolderPosition): - position = entity[FolderPosition] - first_position = Vec2(Display.WIDTH / 2 - 300, Display.HEIGHT / 2 - 50) - entity[smooth.Target] = smooth.Target( - first_position + Vec2(position.x * 150, position.y * 100) - ) - - -SCENE = ( - Scene( - [__initialize_world], - [__update_positions], - [], - ) - + smooth.PLUGIN -)