def test_none_move(self): entity = self.make_entity( current_position=Position(0, 0), goal_position=Position(0, 0), ) self.system.process() self.assertIsNone(self.registry.get(entity, Action))
def make_move_action(self, entity, pos, goal): delta = goal.position - pos if delta.x: delta = Position(sign(delta.x), 0) elif delta.y: delta = Position(0, sign(delta.y)) else: return self.registry.assign(entity, Action, ActionType.MOVE, delta=delta)
def render_debug(self): top_left = Position(0, 0) top_right = Position(self.config.map.width - 1, 0) bottom_left = Position(0, self.config.map.height - 1) bottom_right = Position(self.config.map.width - 1, self.config.map.height - 1) self.draw(top_left, 'M', libtcod.red) self.draw(top_right, 'M', libtcod.red) self.draw(bottom_left, 'M', libtcod.red) self.draw(bottom_right, 'M', libtcod.red)
def events(): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() # left mouse button pressed if pygame.mouse.get_pressed()[0]: mouse_pos = pygame.mouse.get_pos() pos = Position(mouse_pos[0] // SCALE, mouse_pos[1] // SCALE) empty = True for entity in entity_manager.pairs_for_type(Position): if entity[1].x == pos.x and entity[1].y == pos.y: empty = False break if empty == True: new_entity = entity_manager.create_entity() vel = Velocity(0, 0, 1) pixel_type = PixelType(Color.yellow) entity_manager.add_component(new_entity, pos) entity_manager.add_component(new_entity, vel) entity_manager.add_component(new_entity, pixel_type) if pygame.mouse.get_pressed()[2]: mouse_pos = pygame.mouse.get_pos() pos = Position(mouse_pos[0] // SCALE, mouse_pos[1] // SCALE) empty = True for entity in entity_manager.pairs_for_type(Position): if entity[1].x == pos.x and entity[1].y == pos.y: empty = False break if empty == True: new_entity = entity_manager.create_entity() vel = Velocity(0, 0, 1) pixel_type = PixelType(Color.blue) entity_manager.add_component(new_entity, pos) entity_manager.add_component(new_entity, vel) entity_manager.add_component(new_entity, pixel_type) if pygame.mouse.get_pressed()[1]: mouse_pos = pygame.mouse.get_pos() pos = Position(mouse_pos[0] // SCALE, mouse_pos[1] // SCALE) empty = True for entity in entity_manager.pairs_for_type(Position): if entity[1].x == pos.x and entity[1].y == pos.y: empty = False break if empty == True: new_entity = entity_manager.create_entity() vel = Velocity(0, 0, 1) pixel_type = PixelType(Color.brown) entity_manager.add_component(new_entity, pos) entity_manager.add_component(new_entity, vel) entity_manager.add_component(new_entity, pixel_type)
def initialize_player(self, x, y): """ initializes a player entity at x, y """ position = Position(x, y) player_rect = pg.Rect(position.x, position.y, 32, 32) collider = Collider( layer=0b00000001, mask=0b11111110, rect=player_rect, callbacks={ # floor 0b00000010: self.player_floor, }, debug=DEBUG ) sprite = Sprite( base_image=self.images[0], anims=[], rect=player_rect, ) physics = Physics( gravity=75, air_friction=0, ground_friction=1, ) player_id = self.create_entity(position) self.add_component(player_id, collider) self.add_component(player_id, sprite) self.add_component(player_id, Input()) self.add_component(player_id, physics)
def test_create_wall(self): self.system.create_wall(123, 5345) walls = [ position for _, _, position in self.registry.view(Obstacle, Position) ] self.assertListEqual([Position(123, 5345)], walls)
def test_procces_makes_move_goal(self): pos = Position(2, 1) self.pubsub_view.move_orders.append(pos) player = self.registry.create(Player) self.system.process() self.assertEqual( self.registry.get(player, Goal), Goal(GoalType.MOVE, position=pos) )
def test_something(self): world = self.world player = world.create_entity() world.add_component(player, PlayerData(name="Jon Doe", changed=True)) world.add_component(player, Position()) world.add_component(player, Movement()) world.add_component(player, LocationHistory()) world.add_component(player, Persistent()) world.component_for_entity(player, Movement).x = 3 world.component_for_entity(player, Movement).location_id = 10 world.process() self.assertEqual(3, world.component_for_entity(player, Position).x) cursor = self.db.cursor() # check player saved cursor.execute("SELECT * FROM PlayerData") data = cursor.fetchone() self.assertEqual(player, data[0]) # check current position cursor.execute("SELECT * FROM Position") data = cursor.fetchone() self.assertEqual(player, data[0]) self.assertEqual(3.0, data[1]) # x self.assertEqual(0.0, data[2]) # y self.assertEqual(10, data[3]) # location_id # check for location history for player 1 to visit location 10 cursor.execute("SELECT * FROM LocationHistory") data = cursor.fetchone() self.assertEqual(player, data[0]) self.assertEqual(10, data[1]) self.assertIsNotNone(data[2]) # change location and check for history world.component_for_entity(player, Movement).location_id = 11 world.process() cursor.execute("SELECT * FROM LocationHistory WHERE location_id = 11") data = cursor.fetchone() self.assertEqual(player, data[0]) self.assertEqual(11, data[1]) self.assertIsNotNone(data[2])
def initialize_floor(self, x, y): position = Position(x, y) floor_rect = (x, y, 100, 20) collider = Collider( layer=0b00000010, mask=0b00000000, rect=floor_rect, callbacks={}, debug=DEBUG ) sprite = Sprite( base_image=None, anims=[], rect=floor_rect ) floor_id = self.create_entity(position, collider) self.add_component(floor_id, position) self.add_component(floor_id, collider) self.add_component(floor_id, sprite)
def create_wall(self, x: int, y: int): patterns.load('wall').spawn(self.registry, Position(x, y))
pygame.init() pygame.display.set_caption('~ R I K \' S G A M E ~') pygame.display.set_icon(pygame.image.load('icon.png')) clock = pygame.time.Clock() w = World(size=1024, tilesize=32) s = Screen(800, 800) gs = GraphicsSystem() cs = ControlSystem() ss = SpriteSystem() p = Entity(w) p.camera = Camera(0, 0, 800, 800, track=True, entitytotrack=p) p.position = Position(0, 0, 32, 32, 2) p.control = Control(pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT) p.state = State() p.sprite = Sprite() p.sprite.sprites['idle'] = [ pygame.image.load('images/player/idle.png'), pygame.image.load('images/player/idle2.png'), pygame.image.load('images/player/idle3.png') ] p.sprite.animate = True running = True while running: # clear screen
pos_rend_manager = intersection(position_manager, render_data_manager) blt.open() for pos_rend in pos_rend_manager.all_components(): blt.put(pos_rend[0].x, pos_rend[0].y, pos_rend[1].glyph) blt.refresh() def render(): pos_rend_manager = intersection(position_manager, render_data_manager) blt.clear() for pos_rend in pos_rend_manager.all_components(): blt.put(pos_rend[0].x, pos_rend[0].y, pos_rend[1].glyph) blt.refresh() def terminate_render(): blt.close() if __name__ == "__main__": from ecs import Entity from components import Position, RenderData for i in range(10): e = Entity.create() position_manager.add(Position(i, i), e) render_data_manager.add(RenderData(chr(65 + i), "foo"), e) initialize_render() while blt.read() != blt.TK_CLOSE: render() terminate_render()
def test_vertical_move(self): self.check_delta( current_position=Position(0, 0), goal_position=Position(2, 0), expected_delta=Position(1, 0), )
def test_procces_makes_order(self): self.pubsub.mouse_clicks.append( Position(settings.TILE_SIZE * 2, settings.TILE_SIZE + 1)) self.system.process() self.assertEqual(next(self.pubsub_view.move_orders), Position(2, 1))
def test_horizontal_move(self): self.check_delta( current_position=Position(1, 0), goal_position=Position(-1, 0), expected_delta=Position(-1, 0), )
else: dead_queue.append(cell) else: if num_neighbors == 3: live_queue.append(cell) for cell in live_queue: render_data_manager.replace(RenderData('#', None), cell) for cell in dead_queue: render_data_manager.replace(RenderData(' ', None), cell) if __name__ == "__main__": from ecs import Entity cells = [Entity.create() for _ in range(4)] position_manager.add(Position(10, 10), cells[0]) position_manager.add(Position(11, 10), cells[1]) position_manager.add(Position(10, 11), cells[2]) position_manager.add(Position(11, 11), cells[3]) render_data_manager.add(RenderData('#', None), cells[0]) render_data_manager.add(RenderData('#', None), cells[1]) render_data_manager.add(RenderData('#', None), cells[2]) render_data_manager.add(RenderData(' ', None), cells[3]) field = [[None for y in range(100)] for x in range(100)] for entity in position_manager.all_entities(): pos = position_manager.get(entity) field[pos.x][pos.y] = entity update(field)
def on_mouse_release(x, y, button, modifiers): pubsub.mouse_clicks.append(Position(x, y))