development #8

Merged
s452701 merged 6 commits from development into master 2021-05-24 13:20:01 +02:00
2 changed files with 14 additions and 1 deletions
Showing only changes of commit 5ea20bd1c0 - Show all commits

View File

@ -1,8 +1,10 @@
import random import random
from survival import GameMap
from survival.components.OnCollisionComponent import OnCollisionComponent 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.esper import World
from survival.settings import RESOURCES_AMOUNT from survival.settings import RESOURCES_AMOUNT
@ -22,6 +24,7 @@ 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() col = OnCollisionComponent()
col.addCallback(self.remove_resource, world=self.world, game_map=self.map, entity=obj)
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.world.add_component(obj, col)
@ -32,3 +35,9 @@ class ResourceGenerator:
while self.map.is_colliding(free_pos): while self.map.is_colliding(free_pos):
free_pos = [random.randrange(self.map.width), random.randrange(self.map.height)] free_pos = [random.randrange(self.map.width), random.randrange(self.map.height)]
return free_pos return free_pos
@staticmethod
def remove_resource(world: World, game_map: GameMap, entity: int):
pos = world.component_for_entity(entity, PositionComponent)
game_map.remove_entity(pos.grid_position)
world.delete_entity(entity, immediate=True)

View File

@ -26,7 +26,11 @@ class CollisionSystem(esper.Processor):
self.world.remove_component(ent, MovingComponent) self.world.remove_component(ent, MovingComponent)
onCol.callAll() onCol.callAll()
colliding_object: int = self.map.get_entity(moving.target) colliding_object: int = self.map.get_entity(moving.target)
if colliding_object is not None and self.world.has_component(colliding_object, OnCollisionComponent):
if colliding_object is None or not self.world.entity_exists(colliding_object):
continue
if self.world.has_component(colliding_object, OnCollisionComponent):
self.world.component_for_entity(colliding_object, OnCollisionComponent).callAll() self.world.component_for_entity(colliding_object, OnCollisionComponent).callAll()
else: else: