Add delay to direction change
This commit is contained in:
parent
4467ca555e
commit
4151f5996c
12
survival/components/direction_component.py
Normal file
12
survival/components/direction_component.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from survival.enums import Direction
|
||||||
|
|
||||||
|
|
||||||
|
class DirectionChangeComponent:
|
||||||
|
def __init__(self, direction):
|
||||||
|
self.direction = direction
|
||||||
|
|
||||||
|
def rotate_left(self):
|
||||||
|
self.direction = Direction.rotate_left(self.direction)
|
||||||
|
|
||||||
|
def rotate_right(self):
|
||||||
|
self.direction = Direction.rotate_right(self.direction)
|
@ -1,4 +1,5 @@
|
|||||||
class PathfindingComponent:
|
class PathfindingComponent:
|
||||||
def __init__(self, target_pos):
|
def __init__(self, target_pos):
|
||||||
self.target_grid_pos = (int(target_pos[0] / 32), int(target_pos[1] / 32))
|
self.target_grid_pos = (int(target_pos[0] / 32), int(target_pos[1] / 32))
|
||||||
|
self.current_target = None
|
||||||
self.path = None
|
self.path = None
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from survival.enums import Direction
|
from survival.enums import Direction
|
||||||
|
from survival.settings import DIRECTION_CHANGE_DELAY
|
||||||
|
|
||||||
|
|
||||||
class PositionComponent:
|
class PositionComponent:
|
||||||
@ -6,9 +7,10 @@ class PositionComponent:
|
|||||||
self.position = pos
|
self.position = pos
|
||||||
self.grid_position = grid_pos
|
self.grid_position = grid_pos
|
||||||
self.direction = Direction.DOWN
|
self.direction = Direction.DOWN
|
||||||
|
self.direction_change_timer = DIRECTION_CHANGE_DELAY
|
||||||
|
|
||||||
def rotate_left(self):
|
def rotate_left(self):
|
||||||
self.direction = Direction.rotate_left(self.direction)
|
return Direction.rotate_left(self.direction)
|
||||||
|
|
||||||
def rotate_right(self):
|
def rotate_right(self):
|
||||||
self.direction = Direction.rotate_right(self.direction)
|
return Direction.rotate_right(self.direction)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
SCREEN_WIDTH = 1920
|
SCREEN_WIDTH = 1920
|
||||||
SCREEN_HEIGHT = 1080
|
SCREEN_HEIGHT = 1080
|
||||||
RESOURCES_AMOUNT = 300
|
RESOURCES_AMOUNT = 300
|
||||||
|
DIRECTION_CHANGE_DELAY = 200
|
||||||
|
27
survival/systems/direction_system.py
Normal file
27
survival/systems/direction_system.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
from survival import esper
|
||||||
|
from survival.components.direction_component import DirectionChangeComponent
|
||||||
|
from survival.components.position_component import PositionComponent
|
||||||
|
from survival.settings import DIRECTION_CHANGE_DELAY
|
||||||
|
|
||||||
|
|
||||||
|
class DirectionSystem(esper.Processor):
|
||||||
|
def process(self, dt):
|
||||||
|
for ent, (pos, direction) in self.world.get_components(PositionComponent, DirectionChangeComponent):
|
||||||
|
if pos.direction_change_timer > 0:
|
||||||
|
pos.direction_change_timer -= dt
|
||||||
|
continue
|
||||||
|
|
||||||
|
dir_left = pos.rotate_left()
|
||||||
|
dir_right = pos.rotate_right()
|
||||||
|
|
||||||
|
pos.direction_change_timer = DIRECTION_CHANGE_DELAY
|
||||||
|
|
||||||
|
if dir_left == direction.direction:
|
||||||
|
pos.direction = dir_left
|
||||||
|
elif dir_right == direction.direction:
|
||||||
|
pos.direction = dir_right
|
||||||
|
else:
|
||||||
|
pos.direction = dir_left
|
||||||
|
continue
|
||||||
|
|
||||||
|
self.world.remove_component(ent, DirectionChangeComponent)
|
@ -1,6 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from survival import esper
|
from survival import esper
|
||||||
|
from survival.components.direction_component import DirectionChangeComponent
|
||||||
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.pathfinding_component import PathfindingComponent
|
from survival.components.pathfinding_component import PathfindingComponent
|
||||||
@ -25,8 +26,10 @@ 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]:
|
||||||
pos.rotate_left()
|
if not self.world.has_component(ent, DirectionChangeComponent):
|
||||||
|
self.world.add_component(ent, DirectionChangeComponent(pos.rotate_left()))
|
||||||
elif keys[pygame.K_RIGHT]:
|
elif keys[pygame.K_RIGHT]:
|
||||||
pos.rotate_right()
|
if not self.world.has_component(ent, DirectionChangeComponent):
|
||||||
|
self.world.add_component(ent, DirectionChangeComponent(pos.rotate_right()))
|
||||||
elif keys[pygame.K_UP]:
|
elif keys[pygame.K_UP]:
|
||||||
self.world.add_component(ent, MovingComponent())
|
self.world.add_component(ent, MovingComponent())
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from survival import esper
|
from survival import esper
|
||||||
|
from survival.components.direction_component import DirectionChangeComponent
|
||||||
from survival.components.movement_component import MovementComponent
|
from survival.components.movement_component import MovementComponent
|
||||||
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
|
||||||
@ -18,17 +19,24 @@ class PathfindingMovementSystem(esper.Processor):
|
|||||||
if pathfinding.path is None:
|
if pathfinding.path is None:
|
||||||
pathfinding.path = breadth_first_search(self.game_map, pos.grid_position, pathfinding.target_grid_pos)
|
pathfinding.path = breadth_first_search(self.game_map, pos.grid_position, pathfinding.target_grid_pos)
|
||||||
|
|
||||||
if len(pathfinding.path) < 1:
|
if len(pathfinding.path) < 1 and pathfinding.current_target is None:
|
||||||
self.world.remove_component(ent, PathfindingComponent)
|
self.world.remove_component(ent, PathfindingComponent)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if self.world.has_component(ent, MovingComponent):
|
if self.world.has_component(ent, MovingComponent):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
target = pathfinding.path.pop(0)
|
if pathfinding.current_target is None:
|
||||||
|
target = pathfinding.path.pop(0)
|
||||||
|
else:
|
||||||
|
target = pathfinding.current_target
|
||||||
|
|
||||||
vector = (target[0] - pos.grid_position[0], target[1] - pos.grid_position[1])
|
vector = (target[0] - pos.grid_position[0], target[1] - pos.grid_position[1])
|
||||||
direction = Direction.from_vector(vector)
|
direction = Direction.from_vector(vector)
|
||||||
if direction != pos.direction:
|
if direction != pos.direction:
|
||||||
pos.direction = direction
|
pathfinding.current_target = target
|
||||||
|
self.world.add_component(ent, DirectionChangeComponent(direction))
|
||||||
|
continue
|
||||||
|
|
||||||
|
pathfinding.current_target = None
|
||||||
self.world.add_component(ent, MovingComponent())
|
self.world.add_component(ent, MovingComponent())
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from survival import esper
|
from survival import esper
|
||||||
from survival.systems.camera_system import CameraSystem
|
from survival.systems.camera_system import CameraSystem
|
||||||
from survival.systems.collision_system import CollisionSystem
|
from survival.systems.collision_system import CollisionSystem
|
||||||
|
from survival.systems.direction_system import DirectionSystem
|
||||||
from survival.systems.draw_system import DrawSystem
|
from survival.systems.draw_system import DrawSystem
|
||||||
from survival.systems.input_system import InputSystem
|
from survival.systems.input_system import InputSystem
|
||||||
from survival.systems.movement_system import MovementSystem
|
from survival.systems.movement_system import MovementSystem
|
||||||
@ -19,5 +20,6 @@ class WorldGenerator:
|
|||||||
world.add_processor(DrawSystem(camera))
|
world.add_processor(DrawSystem(camera))
|
||||||
world.add_processor(TimeSystem())
|
world.add_processor(TimeSystem())
|
||||||
world.add_processor(PathfindingMovementSystem(game_map), priority=3)
|
world.add_processor(PathfindingMovementSystem(game_map), priority=3)
|
||||||
|
world.add_processor(DirectionSystem())
|
||||||
|
|
||||||
return world
|
return world
|
||||||
|
Loading…
Reference in New Issue
Block a user