development #8

Merged
s452701 merged 6 commits from development into master 2021-05-24 13:20:01 +02:00
12 changed files with 41 additions and 9 deletions
Showing only changes of commit ee25509975 - Show all commits

View File

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

View File

@ -1,10 +1,11 @@
import random
from typing import List
from survival.tile import Tile
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.min_height = min_height
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:
def __init__(self, maxitems):
def __init__(self, maxitems = 10):
self.maxitems = maxitems
self.items = {}

View File

@ -18,5 +18,8 @@ class EntityLayer:
def remove_entity(self, pos):
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):
return self.tiles[pos[1]][pos[0]] is not None

View File

@ -22,6 +22,9 @@ class GameMap:
def remove_entity(self, 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):
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())
game_map.add_entity(home, pos)
sprite = SpriteComponent('stone.png')
sprite = SpriteComponent('tree.png')
sprite.set_scale(2)
world.add_component(home, sprite)
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.input_component import InputComponent
from survival.components.movement_component import MovementComponent
@ -14,6 +15,7 @@ class PlayerGenerator:
world.add_component(player, pos)
world.add_component(player, MovementComponent())
world.add_component(player, InputComponent())
world.add_component(player, OnCollisionComponent())
camera_target = CameraTargetComponent(pos)
world.add_component(player, camera_target)
game_map.add_entity(player, pos)

View File

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

View File

@ -1,4 +1,5 @@
import random
from typing import List
from survival.biomes.biome_data import BiomeData
from survival.biomes.biome_preset import BiomePreset
@ -34,7 +35,7 @@ class TileGenerator:
return Tile(origin=tile.origin, cost=tile.cost)
@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)]
@staticmethod

View File

@ -1,5 +1,6 @@
from enum import Enum
from queue import PriorityQueue
from typing import Tuple, List
from survival import GameMap
from survival.components.position_component import PositionComponent
@ -13,7 +14,7 @@ class Action(Enum):
class State:
def __init__(self, position: tuple[int, int], direction: Direction):
def __init__(self, position: Tuple[int, int], direction: Direction):
self.position = position
self.direction = direction
@ -32,12 +33,12 @@ class Node:
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)
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.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
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])

View File

@ -1,6 +1,7 @@
import operator
from survival import esper
from survival.components.OnCollisionComponent import OnCollisionComponent
from survival.components.moving_component import MovingComponent
from survival.components.position_component import PositionComponent
from survival.enums import Direction
@ -11,7 +12,7 @@ class CollisionSystem(esper.Processor):
self.map = game_map
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:
continue
@ -22,6 +23,11 @@ class CollisionSystem(esper.Processor):
moving.direction_vector = vector
if self.check_collision(moving.target):
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:
self.map.move_entity(pos.grid_position, moving.target)
pos.grid_position = moving.target