From ac0b8dc2f51e0b10551f08a5c984924b34576c76 Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Mon, 27 Mar 2023 12:51:44 +0200 Subject: [PATCH] Initial button logic --- project/data/levels/1-1.csv | 5 ++-- project/level/Level.py | 10 +++---- .../level/elements/ButtonInputLevelElement.py | 30 +++++++++++++++++-- project/level/elements/LoadedLevel.py | 2 +- project/main.py | 2 -- project/physics/PhysicsElementsHandler.py | 21 +++++++++---- project/physics/sprites/PlayerSprite.py | 24 ++++++++------- 7 files changed, 66 insertions(+), 28 deletions(-) diff --git a/project/data/levels/1-1.csv b/project/data/levels/1-1.csv index 48a8ae0..8915d8a 100644 --- a/project/data/levels/1-1.csv +++ b/project/data/levels/1-1.csv @@ -26,7 +26,7 @@ #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# #,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,# -#,,,,,,,,,,,,,,,,P,,,,,,,,,,,,,,,,,,,,,,,,,,x,x,x,x,x,x,x,x,x,,,,,,,,,,,,,,,,,D,,,# +#,,,,,,,,,,P,,,,,,P,,,,,,,,,,,,,,,,,,,,,,,,,,x,x,x,x,x,x,x,x,x,,,,,,,,,,,,,,,,,D,,,# #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,r,,,,,,,,,,,l,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,r,,,,,,,,,,,,,,l,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,r,,,,,,,,,,,,,,l,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# @@ -40,4 +40,5 @@ #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,r,,,,,,,,,,,,,,l,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# #,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,r,,,,,,,,,,,,,,l,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,#,# ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, -16,28,id=HEBEL,emitter_state=false,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, \ No newline at end of file +16,28,id=HEBEL,emitter_state=false,debug=1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, +10,28,id=HEBEL,emitter_state=false,debug=2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, \ No newline at end of file diff --git a/project/level/Level.py b/project/level/Level.py index bce45c8..8dbb759 100644 --- a/project/level/Level.py +++ b/project/level/Level.py @@ -28,20 +28,18 @@ class Level: elif line[0] != '': x = int(line[0]) y = int(line[1]) - tile = tiles[x][y] + tile = tiles[y][x] for i in range(2, len(line)): if line[i] == '': - break + continue split_item = line[i].split('=') - if split_item[0] == 'id': + if split_item[0] in ['id', 'emitter_state', 'debug']: tile[split_item[0]] = split_item[1] elif split_item[0] == 'requires': tile[split_item[0]] = split_item[1].split(';') - elif split_item[0] == 'emitter_state': - tile[split_item[0]] = split_item[1].split(';') else: raise ValueError('Incorrect attribute name: ' + split_item[0]) - tiles[x][y] = tile + tiles[y][x] = tile return tiles diff --git a/project/level/elements/ButtonInputLevelElement.py b/project/level/elements/ButtonInputLevelElement.py index 09ce073..a71ef67 100644 --- a/project/level/elements/ButtonInputLevelElement.py +++ b/project/level/elements/ButtonInputLevelElement.py @@ -1,5 +1,6 @@ from level.Level import Level from level.elements.InputLevelElement import InputLevelElement +from physics.CollisionDirection import CollisionDirection from physics.SpriteManager import SpriteManager from physics.TickData import TickData from sprite.SpritesheetManager import SpritesheetManager @@ -7,6 +8,10 @@ from sprite.SpritesheetManager import SpritesheetManager class ButtonInputLevelElement(InputLevelElement): + def __init__(self, tile: dict, level: Level): + super().__init__(tile, level) + self.is_currently_stood_on = False + def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): self.spritesheet = spritesheet_manager.get_sheet('pressureplate') self.set_animation_state('on' if self.emitter_state else 'off') @@ -15,5 +20,26 @@ class ButtonInputLevelElement(InputLevelElement): def tick(self, tick_data: TickData): super().tick(tick_data) - collisions = self.get_collides_with() - # print(collisions) + collisions = self.get_collides_with_direction(CollisionDirection.TOP) + + # debug 1: only activate when player is standing on it + if self.tile['debug'] == '1': + if collisions and not self.is_currently_stood_on: + self.is_currently_stood_on = True + self.set_active(True) + elif not collisions and self.is_currently_stood_on: + self.is_currently_stood_on = False + self.set_active(False) + + # debug 2: toggle active state when player is standing on it + elif self.tile['debug'] == '2': + if collisions and not self.is_currently_stood_on: + self.is_currently_stood_on = True + self.set_active(not self.emitter_state) + elif not collisions and self.is_currently_stood_on: + self.is_currently_stood_on = False + + def set_active(self, active: bool): + self.emitter_state = active + self.set_animation_state('on' if self.emitter_state else 'off') + diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index 947f1b0..c51acf6 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -15,8 +15,8 @@ TILES = { '~': SimpleBlockLevelElement, '|': SimpleBlockLevelElement, + 'P': ButtonInputLevelElement, 'L': ButtonInputLevelElement, # TODO - 'P': ButtonInputLevelElement } diff --git a/project/main.py b/project/main.py index 0c11c11..2c0dfbd 100644 --- a/project/main.py +++ b/project/main.py @@ -55,8 +55,6 @@ if what_to_run == 'level': text_1.position_scale.scale = (0.3, 0.3) sprite_manager.add_ui_element(DrawLayers.UI, text_1) - ghost_character.debug_label = text_1 - while True: clock.tick(frame_rate) diff --git a/project/physics/PhysicsElementsHandler.py b/project/physics/PhysicsElementsHandler.py index 26da8b3..5ed1399 100644 --- a/project/physics/PhysicsElementsHandler.py +++ b/project/physics/PhysicsElementsHandler.py @@ -60,7 +60,6 @@ class PhysicsElementsHandler: collides_with_last = None collided = [False, False] - sprite.reset_collides_with() for i in range(motion_steps): if not collided[0]: @@ -77,10 +76,16 @@ class PhysicsElementsHandler: ) if sprite.motion[0] > 0: - sprite.add_collides_with(CollisionDirection(CollisionDirection.RIGHT, sprite, collides_with)) + sprite.add_collides_with( + CollisionDirection(CollisionDirection.RIGHT, sprite, collides_with)) + collides_with.add_collides_with( + CollisionDirection(CollisionDirection.LEFT, collides_with, sprite)) if sprite.motion[0] < 0: - sprite.add_collides_with(CollisionDirection(CollisionDirection.LEFT, sprite, collides_with)) + sprite.add_collides_with( + CollisionDirection(CollisionDirection.LEFT, sprite, collides_with)) + collides_with.add_collides_with( + CollisionDirection(CollisionDirection.RIGHT, collides_with, sprite)) sprite.motion = (0, sprite.motion[1]) @@ -101,10 +106,16 @@ class PhysicsElementsHandler: ) if sprite.motion[1] > 0: - sprite.add_collides_with(CollisionDirection(CollisionDirection.BOTTOM, sprite, collides_with)) + sprite.add_collides_with( + CollisionDirection(CollisionDirection.BOTTOM, sprite, collides_with)) + collides_with.add_collides_with( + CollisionDirection(CollisionDirection.TOP, collides_with, sprite)) if sprite.motion[1] < 0: - sprite.add_collides_with(CollisionDirection(CollisionDirection.TOP, sprite, collides_with)) + sprite.add_collides_with( + CollisionDirection(CollisionDirection.TOP, sprite, collides_with)) + collides_with.add_collides_with( + CollisionDirection(CollisionDirection.BOTTOM, collides_with, sprite)) sprite.motion = (sprite.motion[0], 0) diff --git a/project/physics/sprites/PlayerSprite.py b/project/physics/sprites/PlayerSprite.py index 08666b0..4bbc6a4 100644 --- a/project/physics/sprites/PlayerSprite.py +++ b/project/physics/sprites/PlayerSprite.py @@ -9,7 +9,7 @@ from ui_elements.TextLabel import TextLabel class PlayerSprite(DynamicSprite): def __init__(self, spritesheet: Spritesheet): super().__init__(spritesheet) - self.debug_label = TextLabel('', -1, -1) + self.cheat_fly = False self.jump_time = -1 self.allowed_jump_time = 12 @@ -33,13 +33,17 @@ class PlayerSprite(DynamicSprite): if self.motion[0] > -self.max_motion_horizontal_via_input: self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1]) - if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP): - if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM): - self.jump_time = self.allowed_jump_time - self.motion = (self.motion[0], self.motion[1] - 7) + if not self.cheat_fly: + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP): + if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM): + self.jump_time = self.allowed_jump_time + self.motion = (self.motion[0], self.motion[1] - 7) + if self.jump_time >= 0: + self.motion = (self.motion[0], self.motion[1] - 0.5) if self.jump_time >= 0: - self.motion = (self.motion[0], self.motion[1] - 0.5) - if self.jump_time >= 0: - self.jump_time -= 1 - - self.debug_label.set_text(f'jump: {self.jump_time}, x: {round(self.motion[0], 2)}, y: {round(self.motion[1], 2)}, touches: {list(map(lambda x: x.to_string(), self.get_collides_with()))}') + self.jump_time -= 1 + else: + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP): + self.motion = (self.motion[0], self.motion[1] - 2) + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_DOWN): + self.motion = (self.motion[0], self.motion[1] + 2)