development #8
@ -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)
|
||||||
|
@ -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:
|
||||||
|
Loading…
Reference in New Issue
Block a user