dev-jakklu #5

Merged
s452701 merged 14 commits from dev-jakklu into master 2021-05-08 22:22:47 +02:00
8 changed files with 67 additions and 32 deletions
Showing only changes of commit 5b469611e6 - Show all commits

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -1,5 +1,4 @@
class MovingComponent:
def __init__(self, direction, target):
self.direction = direction
self.movement_target = target
self.checked_collision = False
def __init__(self):
self.target = None
self.direction_vector = None

View File

@ -1,4 +1,8 @@
from survival.enums import Direction
class PositionComponent:
def __init__(self, pos, grid_pos):
self.position = pos
self.grid_position = grid_pos
self.direction = Direction.DOWN

27
survival/enums.py Normal file
View File

@ -0,0 +1,27 @@
from enum import IntEnum
class Direction(IntEnum):
DOWN = 0
LEFT = 1
UP = 2
RIGHT = 3
@staticmethod
def rotate_left(direction):
return Direction((direction - 1) % 4)
@staticmethod
def rotate_right(direction):
return Direction((direction + 1) % 4)
@staticmethod
def get_vector(direction):
if direction == Direction.UP:
return 0, -1
elif direction == Direction.DOWN:
return 0, 1
elif direction == Direction.LEFT:
return -1, 0
elif direction == Direction.RIGHT:
return 1, 0

View File

@ -1,6 +1,9 @@
import operator
from survival import esper
from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent
from survival.enums import Direction
class CollisionSystem(esper.Processor):
@ -9,17 +12,20 @@ class CollisionSystem(esper.Processor):
def process(self, dt):
for ent, (pos, moving) in self.world.get_components(PositionComponent, MovingComponent):
if moving.checked_collision:
if moving.target is not None:
continue
moving.checked_collision = True
if self.check_collision(moving.movement_target):
self.world.remove_component(ent, MovingComponent)
vector = Direction.get_vector(pos.direction)
moving.target = tuple(map(operator.add, vector, pos.grid_position))
moving.direction_vector = vector
if self.check_collision(moving.target):
self.world.remove_component(ent, MovingComponent)
else:
self.map.move_entity(pos.grid_position, moving.movement_target)
pos.grid_position = moving.movement_target
self.map.move_entity(pos.grid_position, moving.target)
pos.grid_position = moving.target
def check_collision(self, pos):
return self.map.is_colliding(pos)

View File

@ -10,4 +10,5 @@ class DrawSystem(esper.Processor):
def process(self, dt):
for ent, (sprite, pos) in self.world.get_components(SpriteComponent, PositionComponent):
sprite.image.pos = pos.position
sprite.image.origin = (32 * pos.direction.value, 0)
self.camera.draw(sprite.image)

View File

@ -4,6 +4,7 @@ from survival import esper
from survival.components.input_component import InputComponent
from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent
from survival.enums import Direction
class InputSystem(esper.Processor):
@ -17,10 +18,8 @@ class InputSystem(esper.Processor):
if self.world.has_component(ent, MovingComponent):
continue
if keys[pygame.K_LEFT]:
self.world.add_component(ent, MovingComponent([-1, 0], [pos.grid_position[0] - 1, pos.grid_position[1]]))
pos.direction = Direction.rotate_left(pos.direction)
elif keys[pygame.K_RIGHT]:
self.world.add_component(ent, MovingComponent([1, 0], [pos.grid_position[0] + 1, pos.grid_position[1]]))
elif keys[pygame.K_DOWN]:
self.world.add_component(ent, MovingComponent([0, 1], [pos.grid_position[0], pos.grid_position[1] + 1]))
pos.direction = Direction.rotate_right(pos.direction)
elif keys[pygame.K_UP]:
self.world.add_component(ent, MovingComponent([0, -1], [pos.grid_position[0], pos.grid_position[1] - 1]))
self.world.add_component(ent, MovingComponent())

View File

@ -13,23 +13,22 @@ class MovementSystem(esper.Processor):
for ent, (mov, pos, moving, sprite) in self.world.get_components(MovementComponent, PositionComponent,
MovingComponent,
SpriteComponent):
if moving.direction[0] != 0:
pos.position[0] += moving.direction[0] * mov.speed * dt / 100
if abs(moving.movement_target[0] * 32 - pos.position[0]) < 0.1 * mov.speed:
pos.position = [moving.movement_target[0] * 32, moving.movement_target[1] * 32]
self.world.remove_component(ent, MovingComponent)
else:
pos.position[1] += moving.direction[1] * mov.speed * dt / 100
if abs(pos.position[1] - moving.movement_target[1] * 32) < 0.1 * mov.speed:
pos.position = [moving.movement_target[0] * 32, moving.movement_target[1] * 32]
pos.position[0] += moving.direction_vector[0] * mov.speed * dt / 100
pos.position[1] += moving.direction_vector[1] * mov.speed * dt / 100
if abs(moving.target[0] * 32 - pos.position[0]) < 0.1 * mov.speed and abs(
pos.position[1] - moving.target[1] * 32) < 0.1 * mov.speed:
pos.position = [moving.target[0] * 32, moving.target[1] * 32]
self.world.remove_component(ent, MovingComponent)
if moving.direction[0] == 1:
sprite.image.origin = (96, 0)
elif moving.direction[0] == -1:
sprite.image.origin = (64, 0)
elif moving.direction[1] == 1:
sprite.image.origin = (0, 0)
else:
sprite.image.origin = (32, 0)
# if moving.direction[0] != 0:
# pos.position[0] += moving.direction[0] * mov.speed * dt / 100
# if abs(moving.movement_target[0] * 32 - pos.position[0]) < 0.1 * mov.speed:
# pos.position = [moving.movement_target[0] * 32, moving.movement_target[1] * 32]
# self.world.remove_component(ent, MovingComponent)
# else:
# pos.position[1] += moving.direction[1] * mov.speed * dt / 100
# if abs(pos.position[1] - moving.movement_target[1] * 32) < 0.1 * mov.speed:
# pos.position = [moving.movement_target[0] * 32, moving.movement_target[1] * 32]
# self.world.remove_component(ent, MovingComponent)