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,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
16,28,id=HEBEL,emitter_state=false,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 16,28,id=HEBEL,emitter_state=false,debug=1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
10,28,id=HEBEL,emitter_state=false,debug=2,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
1
26 # #
27 # #
28 # #
29 # P P x x x x x x x x x D #
30 # # # # # # # # # # # # # # # # # # # # # # # r l # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
31 # # # # # # # # # # # # # # # # # # # # # # # r l # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
32 # # # # # # # # # # # # # # # # # # # # # # # r l # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
40 # # # # # # # # # # # # # # # # # # # # # # # r l # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
41 # # # # # # # # # # # # # # # # # # # # # # # r l # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
42
43 16 28 id=HEBEL emitter_state=false debug=1
44 10 28 id=HEBEL emitter_state=false debug=2

View File

@ -28,20 +28,18 @@ class Level:
elif line[0] != '': elif line[0] != '':
x = int(line[0]) x = int(line[0])
y = int(line[1]) y = int(line[1])
tile = tiles[x][y] tile = tiles[y][x]
for i in range(2, len(line)): for i in range(2, len(line)):
if line[i] == '': if line[i] == '':
break continue
split_item = line[i].split('=') 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] tile[split_item[0]] = split_item[1]
elif split_item[0] == 'requires': elif split_item[0] == 'requires':
tile[split_item[0]] = split_item[1].split(';') tile[split_item[0]] = split_item[1].split(';')
elif split_item[0] == 'emitter_state':
tile[split_item[0]] = split_item[1].split(';')
else: else:
raise ValueError('Incorrect attribute name: ' + split_item[0]) raise ValueError('Incorrect attribute name: ' + split_item[0])
tiles[x][y] = tile tiles[y][x] = tile
return tiles return tiles

View File

@ -1,5 +1,6 @@
from level.Level import Level from level.Level import Level
from level.elements.InputLevelElement import InputLevelElement from level.elements.InputLevelElement import InputLevelElement
from physics.CollisionDirection import CollisionDirection
from physics.SpriteManager import SpriteManager from physics.SpriteManager import SpriteManager
from physics.TickData import TickData from physics.TickData import TickData
from sprite.SpritesheetManager import SpritesheetManager from sprite.SpritesheetManager import SpritesheetManager
@ -7,6 +8,10 @@ from sprite.SpritesheetManager import SpritesheetManager
class ButtonInputLevelElement(InputLevelElement): 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): def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level):
self.spritesheet = spritesheet_manager.get_sheet('pressureplate') self.spritesheet = spritesheet_manager.get_sheet('pressureplate')
self.set_animation_state('on' if self.emitter_state else 'off') self.set_animation_state('on' if self.emitter_state else 'off')
@ -15,5 +20,26 @@ class ButtonInputLevelElement(InputLevelElement):
def tick(self, tick_data: TickData): def tick(self, tick_data: TickData):
super().tick(tick_data) super().tick(tick_data)
collisions = self.get_collides_with() collisions = self.get_collides_with_direction(CollisionDirection.TOP)
# print(collisions)
# 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')

View File

@ -15,8 +15,8 @@ TILES = {
'~': SimpleBlockLevelElement, '~': SimpleBlockLevelElement,
'|': SimpleBlockLevelElement, '|': SimpleBlockLevelElement,
'P': ButtonInputLevelElement,
'L': ButtonInputLevelElement, # TODO 'L': ButtonInputLevelElement, # TODO
'P': ButtonInputLevelElement
} }

View File

@ -55,8 +55,6 @@ if what_to_run == 'level':
text_1.position_scale.scale = (0.3, 0.3) text_1.position_scale.scale = (0.3, 0.3)
sprite_manager.add_ui_element(DrawLayers.UI, text_1) sprite_manager.add_ui_element(DrawLayers.UI, text_1)
ghost_character.debug_label = text_1
while True: while True:
clock.tick(frame_rate) clock.tick(frame_rate)

View File

@ -60,7 +60,6 @@ class PhysicsElementsHandler:
collides_with_last = None collides_with_last = None
collided = [False, False] collided = [False, False]
sprite.reset_collides_with()
for i in range(motion_steps): for i in range(motion_steps):
if not collided[0]: if not collided[0]:
@ -77,10 +76,16 @@ class PhysicsElementsHandler:
) )
if sprite.motion[0] > 0: 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: 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]) sprite.motion = (0, sprite.motion[1])
@ -101,10 +106,16 @@ class PhysicsElementsHandler:
) )
if sprite.motion[1] > 0: 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: 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) sprite.motion = (sprite.motion[0], 0)

View File

@ -9,7 +9,7 @@ from ui_elements.TextLabel import TextLabel
class PlayerSprite(DynamicSprite): class PlayerSprite(DynamicSprite):
def __init__(self, spritesheet: Spritesheet): def __init__(self, spritesheet: Spritesheet):
super().__init__(spritesheet) super().__init__(spritesheet)
self.debug_label = TextLabel('', -1, -1) self.cheat_fly = False
self.jump_time = -1 self.jump_time = -1
self.allowed_jump_time = 12 self.allowed_jump_time = 12
@ -33,13 +33,17 @@ class PlayerSprite(DynamicSprite):
if self.motion[0] > -self.max_motion_horizontal_via_input: if self.motion[0] > -self.max_motion_horizontal_via_input:
self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1]) self.motion = (self.motion[0] - self.acceleration_horizontal, self.motion[1])
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP): if not self.cheat_fly:
if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM): if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP):
self.jump_time = self.allowed_jump_time if self.jump_time < 0 and self.get_collides_with_direction(CollisionDirection.BOTTOM):
self.motion = (self.motion[0], self.motion[1] - 7) 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: if self.jump_time >= 0:
self.motion = (self.motion[0], self.motion[1] - 0.5) self.jump_time -= 1
if self.jump_time >= 0: else:
self.jump_time -= 1 if tick_data.key_manager.is_keymap_down(KeyManager.KEY_UP):
self.motion = (self.motion[0], self.motion[1] - 2)
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()))}') if tick_data.key_manager.is_keymap_down(KeyManager.KEY_DOWN):
self.motion = (self.motion[0], self.motion[1] + 2)