added OnCollisionComponent class and changed all tuples and lists to Upper case

This commit is contained in:
LukeSkiiWalker 2021-05-21 20:14:00 +02:00
parent 474a6dd525
commit ee25509975
12 changed files with 41 additions and 9 deletions

View File

@ -3,6 +3,7 @@ import pygame
from settings import SCREEN_WIDTH, SCREEN_HEIGHT from settings import SCREEN_WIDTH, SCREEN_HEIGHT
from survival.camera import Camera from survival.camera import Camera
from survival.game_map import GameMap from survival.game_map import GameMap
from survival.generators.building_generator import BuildingGenerator
from survival.generators.player_generator import PlayerGenerator from survival.generators.player_generator import PlayerGenerator
from survival.generators.resource_generator import ResourceGenerator from survival.generators.resource_generator import ResourceGenerator
from survival.generators.world_generator import WorldGenerator from survival.generators.world_generator import WorldGenerator
@ -20,6 +21,7 @@ if __name__ == '__main__':
world = WorldGenerator().create_world(camera, game_map) world = WorldGenerator().create_world(camera, game_map)
player = PlayerGenerator().create_player(world, game_map) player = PlayerGenerator().create_player(world, game_map)
building = BuildingGenerator().create_home(world, game_map)
ResourceGenerator(world, game_map).generate_resources() ResourceGenerator(world, game_map).generate_resources()

View File

@ -1,10 +1,11 @@
import random import random
from typing import List
from survival.tile import Tile from survival.tile import Tile
class BiomePreset: class BiomePreset:
def __init__(self, name, min_height: float, min_moisture: float, min_heat: float, tiles: list[Tile]): def __init__(self, name, min_height: float, min_moisture: float, min_heat: float, tiles: List[Tile]):
self.name = name self.name = name
self.min_height = min_height self.min_height = min_height
self.min_moisture = min_moisture self.min_moisture = min_moisture

View File

@ -0,0 +1,10 @@
class OnCollisionComponent:
def __init__(self, callbacks: [] = []):
self.callbacks = callbacks
def callAll(self):
for func in self.callbacks:
func()
def addCallback(self, fn):
self.callbacks.append(fn)

View File

@ -1,5 +1,5 @@
class InventoryComponent: class InventoryComponent:
def __init__(self, maxitems): def __init__(self, maxitems = 10):
self.maxitems = maxitems self.maxitems = maxitems
self.items = {} self.items = {}

View File

@ -18,5 +18,8 @@ class EntityLayer:
def remove_entity(self, pos): def remove_entity(self, pos):
self.tiles[pos[1]][pos[0]] = None self.tiles[pos[1]][pos[0]] = None
def get_entity(self, pos) -> int:
return self.tiles[pos[1]][pos[0]]
def is_colliding(self, pos): def is_colliding(self, pos):
return self.tiles[pos[1]][pos[0]] is not None return self.tiles[pos[1]][pos[0]] is not None

View File

@ -22,6 +22,9 @@ class GameMap:
def remove_entity(self, pos): def remove_entity(self, pos):
self.entity_layer.remove_entity(pos) self.entity_layer.remove_entity(pos)
def get_entity(self, pos) -> int:
return self.entity_layer.get_entity(pos)
def is_colliding(self, pos): def is_colliding(self, pos):
return pos[0] < 0 or pos[0] >= self.width or pos[1] < 0 or pos[1] >= self.height or self.entity_layer.is_colliding(pos) return pos[0] < 0 or pos[0] >= self.width or pos[1] < 0 or pos[1] >= self.height or self.entity_layer.is_colliding(pos)

View File

@ -12,7 +12,7 @@ class BuildingGenerator:
world.add_component(home, InventoryComponent()) world.add_component(home, InventoryComponent())
game_map.add_entity(home, pos) game_map.add_entity(home, pos)
sprite = SpriteComponent('stone.png') sprite = SpriteComponent('tree.png')
sprite.set_scale(2) sprite.set_scale(2)
world.add_component(home, sprite) world.add_component(home, sprite)
world.add_component(home, CollisionComponent()) world.add_component(home, CollisionComponent())

View File

@ -1,3 +1,4 @@
from survival.components.OnCollisionComponent import OnCollisionComponent
from survival.components.camera_target_component import CameraTargetComponent from survival.components.camera_target_component import CameraTargetComponent
from survival.components.input_component import InputComponent from survival.components.input_component import InputComponent
from survival.components.movement_component import MovementComponent from survival.components.movement_component import MovementComponent
@ -14,6 +15,7 @@ class PlayerGenerator:
world.add_component(player, pos) world.add_component(player, pos)
world.add_component(player, MovementComponent()) world.add_component(player, MovementComponent())
world.add_component(player, InputComponent()) world.add_component(player, InputComponent())
world.add_component(player, OnCollisionComponent())
camera_target = CameraTargetComponent(pos) camera_target = CameraTargetComponent(pos)
world.add_component(player, camera_target) world.add_component(player, camera_target)
game_map.add_entity(player, pos) game_map.add_entity(player, pos)

View File

@ -1,5 +1,6 @@
import random import random
from survival.components.OnCollisionComponent import OnCollisionComponent
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
from survival.components.sprite_component import SpriteComponent from survival.components.sprite_component import SpriteComponent
from survival.settings import RESOURCES_AMOUNT from survival.settings import RESOURCES_AMOUNT
@ -20,8 +21,10 @@ class ResourceGenerator:
pos = PositionComponent(empty_pos, empty_grid_pos) pos = PositionComponent(empty_pos, empty_grid_pos)
sprite = SpriteComponent(random.choice(sprites)) sprite = SpriteComponent(random.choice(sprites))
col = OnCollisionComponent()
self.world.add_component(obj, pos) self.world.add_component(obj, pos)
self.world.add_component(obj, sprite) self.world.add_component(obj, sprite)
self.world.add_component(obj, col)
self.map.add_entity(obj, pos) self.map.add_entity(obj, pos)
def get_empty_grid_position(self): def get_empty_grid_position(self):

View File

@ -1,4 +1,5 @@
import random import random
from typing import List
from survival.biomes.biome_data import BiomeData from survival.biomes.biome_data import BiomeData
from survival.biomes.biome_preset import BiomePreset from survival.biomes.biome_preset import BiomePreset
@ -34,7 +35,7 @@ class TileGenerator:
return Tile(origin=tile.origin, cost=tile.cost) return Tile(origin=tile.origin, cost=tile.cost)
@staticmethod @staticmethod
def generate_random_tiles(width: int, height: int) -> list[list[Tile]]: def generate_random_tiles(width: int, height: int) -> List[List[Tile]]:
return [[TileGenerator.get_random_tile() for _ in range(width)] for _ in range(height)] return [[TileGenerator.get_random_tile() for _ in range(width)] for _ in range(height)]
@staticmethod @staticmethod

View File

@ -1,5 +1,6 @@
from enum import Enum from enum import Enum
from queue import PriorityQueue from queue import PriorityQueue
from typing import Tuple, List
from survival import GameMap from survival import GameMap
from survival.components.position_component import PositionComponent from survival.components.position_component import PositionComponent
@ -13,7 +14,7 @@ class Action(Enum):
class State: class State:
def __init__(self, position: tuple[int, int], direction: Direction): def __init__(self, position: Tuple[int, int], direction: Direction):
self.position = position self.position = position
self.direction = direction self.direction = direction
@ -32,12 +33,12 @@ class Node:
return self.cost == other.cost return self.cost == other.cost
def get_moved_position(position: tuple[int, int], direction: Direction): def get_moved_position(position: Tuple[int, int], direction: Direction):
vector = Direction.get_vector(direction) vector = Direction.get_vector(direction)
return position[0] + vector[0], position[1] + vector[1] return position[0] + vector[0], position[1] + vector[1]
def get_states(state: State, game_map: GameMap) -> list[tuple[Action, State, int]]: def get_states(state: State, game_map: GameMap) -> List[Tuple[Action, State, int]]:
states = list() states = list()
states.append((Action.ROTATE_LEFT, State(state.position, state.direction.rotate_left(state.direction)), 1)) states.append((Action.ROTATE_LEFT, State(state.position, state.direction.rotate_left(state.direction)), 1))
@ -63,7 +64,7 @@ def build_path(node: Node):
return actions return actions
def heuristic(new_node: Node, goal: tuple[int, int]): def heuristic(new_node: Node, goal: Tuple[int, int]):
return abs(new_node.state.position[0] - goal[0]) + abs(new_node.state.position[1] - goal[1]) return abs(new_node.state.position[0] - goal[0]) + abs(new_node.state.position[1] - goal[1])

View File

@ -1,6 +1,7 @@
import operator import operator
from survival import esper from survival import esper
from survival.components.OnCollisionComponent import OnCollisionComponent
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 from survival.enums import Direction
@ -11,7 +12,7 @@ class CollisionSystem(esper.Processor):
self.map = game_map self.map = game_map
def process(self, dt): def process(self, dt):
for ent, (pos, moving) in self.world.get_components(PositionComponent, MovingComponent): for ent, (pos, moving, onCol) in self.world.get_components(PositionComponent, MovingComponent, OnCollisionComponent):
if moving.target is not None: if moving.target is not None:
continue continue
@ -22,6 +23,11 @@ class CollisionSystem(esper.Processor):
moving.direction_vector = vector moving.direction_vector = vector
if self.check_collision(moving.target): if self.check_collision(moving.target):
self.world.remove_component(ent, MovingComponent) self.world.remove_component(ent, MovingComponent)
onCol.callAll()
colliding_object : int = self.map.get_entity(moving.target)
if self.world.has_component(colliding_object, OnCollisionComponent):
self.world.component_for_entity(colliding_object, OnCollisionComponent).callAll()
else: else:
self.map.move_entity(pos.grid_position, moving.target) self.map.move_entity(pos.grid_position, moving.target)
pos.grid_position = moving.target pos.grid_position = moving.target