Change movement to directional based one

This commit is contained in:
Jakub Klupieć 2021-04-19 00:07:40 +02:00
parent a1f5b605f8
commit 5b469611e6
8 changed files with 67 additions and 32 deletions

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: class MovingComponent:
def __init__(self, direction, target): def __init__(self):
self.direction = direction self.target = None
self.movement_target = target self.direction_vector = None
self.checked_collision = False

View File

@ -1,4 +1,8 @@
from survival.enums import Direction
class PositionComponent: class PositionComponent:
def __init__(self, pos, grid_pos): def __init__(self, pos, grid_pos):
self.position = pos self.position = pos
self.grid_position = grid_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 import esper
from survival.components.moving_component import MovingComponent from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
from survival.enums import Direction
class CollisionSystem(esper.Processor): class CollisionSystem(esper.Processor):
@ -9,17 +12,20 @@ class CollisionSystem(esper.Processor):
def process(self, dt): def process(self, dt):
for ent, (pos, moving) in self.world.get_components(PositionComponent, MovingComponent): for ent, (pos, moving) in self.world.get_components(PositionComponent, MovingComponent):
if moving.checked_collision: if moving.target is not None:
continue continue
moving.checked_collision = True moving.checked_collision = True
if self.check_collision(moving.movement_target): vector = Direction.get_vector(pos.direction)
self.world.remove_component(ent, MovingComponent) 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: else:
self.map.move_entity(pos.grid_position, moving.movement_target) self.map.move_entity(pos.grid_position, moving.target)
pos.grid_position = moving.movement_target pos.grid_position = moving.target
def check_collision(self, pos): def check_collision(self, pos):
return self.map.is_colliding(pos) return self.map.is_colliding(pos)

View File

@ -10,4 +10,5 @@ class DrawSystem(esper.Processor):
def process(self, dt): def process(self, dt):
for ent, (sprite, pos) in self.world.get_components(SpriteComponent, PositionComponent): for ent, (sprite, pos) in self.world.get_components(SpriteComponent, PositionComponent):
sprite.image.pos = pos.position sprite.image.pos = pos.position
sprite.image.origin = (32 * pos.direction.value, 0)
self.camera.draw(sprite.image) 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.input_component import InputComponent
from survival.components.moving_component import MovingComponent from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
from survival.enums import Direction
class InputSystem(esper.Processor): class InputSystem(esper.Processor):
@ -17,10 +18,8 @@ class InputSystem(esper.Processor):
if self.world.has_component(ent, MovingComponent): if self.world.has_component(ent, MovingComponent):
continue continue
if keys[pygame.K_LEFT]: 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]: elif keys[pygame.K_RIGHT]:
self.world.add_component(ent, MovingComponent([1, 0], [pos.grid_position[0] + 1, pos.grid_position[1]])) pos.direction = Direction.rotate_right(pos.direction)
elif keys[pygame.K_DOWN]:
self.world.add_component(ent, MovingComponent([0, 1], [pos.grid_position[0], pos.grid_position[1] + 1]))
elif keys[pygame.K_UP]: 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, for ent, (mov, pos, moving, sprite) in self.world.get_components(MovementComponent, PositionComponent,
MovingComponent, MovingComponent,
SpriteComponent): SpriteComponent):
if moving.direction[0] != 0:
pos.position[0] += moving.direction[0] * mov.speed * dt / 100 pos.position[0] += moving.direction_vector[0] * mov.speed * dt / 100
if abs(moving.movement_target[0] * 32 - pos.position[0]) < 0.1 * mov.speed: pos.position[1] += moving.direction_vector[1] * mov.speed * dt / 100
pos.position = [moving.movement_target[0] * 32, moving.movement_target[1] * 32]
self.world.remove_component(ent, MovingComponent) if abs(moving.target[0] * 32 - pos.position[0]) < 0.1 * mov.speed and abs(
else: pos.position[1] - moving.target[1] * 32) < 0.1 * mov.speed:
pos.position[1] += moving.direction[1] * mov.speed * dt / 100 pos.position = [moving.target[0] * 32, moving.target[1] * 32]
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) self.world.remove_component(ent, MovingComponent)
if moving.direction[0] == 1: # if moving.direction[0] != 0:
sprite.image.origin = (96, 0) # pos.position[0] += moving.direction[0] * mov.speed * dt / 100
elif moving.direction[0] == -1: # if abs(moving.movement_target[0] * 32 - pos.position[0]) < 0.1 * mov.speed:
sprite.image.origin = (64, 0) # pos.position = [moving.movement_target[0] * 32, moving.movement_target[1] * 32]
elif moving.direction[1] == 1: # self.world.remove_component(ent, MovingComponent)
sprite.image.origin = (0, 0) # else:
else: # pos.position[1] += moving.direction[1] * mov.speed * dt / 100
sprite.image.origin = (32, 0) # 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)