Compare commits

...
2 Commits
Author SHA1 Message Date
nejdetckenobi 8c414fe294 Modifies path creation and deletion 2026-10-17 17:01:58 +03:00
nejdetckenobi 00fb80382b Bumps ECS framework 2026-10-17 17:01:46 +03:00
5 changed files with 82 additions and 37 deletions

No files matched your search

+6 -1
View File
@@ -38,7 +38,12 @@ class Position(Vector2D):
@dataclass @dataclass
class Target(Vector2D): class Source(Vector2D):
pass
@dataclass
class Destination(Vector2D):
pass pass
+26 -12
View File
@@ -1,3 +1,6 @@
from dataclasses import dataclass, field
from typing import cast
from pyglet.shapes import Circle, Line from pyglet.shapes import Circle, Line
from libs.ecs.world import World from libs.ecs.world import World
from components import ( from components import (
@@ -6,7 +9,7 @@ from components import (
Position, Position,
Radius, Radius,
SpaceShipMarker, SpaceShipMarker,
Target, Destination,
) )
from random import choice, randint, sample from random import choice, randint, sample
from settings import ( from settings import (
@@ -19,12 +22,19 @@ from settings import (
SPACESHIP_PATH_COLOR, SPACESHIP_PATH_COLOR,
SPACESHIP_RADIUS, SPACESHIP_RADIUS,
) )
from libs.ecs.state import BaseGameState
@dataclass
class GameState(BaseGameState):
selected_planet: int | None = None
routes: dict[tuple[int, int], "Line"] = field(default_factory=dict)
def create_planet( def create_planet(
world: World, world: World,
x: int, x: float,
y: int, y: float,
radius: int, radius: int,
color: tuple[int, int, int, int], color: tuple[int, int, int, int],
) -> int: ) -> int:
@@ -39,26 +49,30 @@ def create_planet(
def create_spaceship( def create_spaceship(
world: World, world: World,
src_x: int, src_x: float,
src_y: int, src_y: float,
dest_x: int, dest_x: float,
dest_y: int, dest_y: float,
approach_radius: int, approach_radius: int,
): ):
return world.spawn( return world.spawn(
SpaceShipMarker(), SpaceShipMarker(),
Position(x=src_x, y=src_y), Position(x=src_x, y=src_y),
Target(x=dest_x, y=dest_y), Destination(x=dest_x, y=dest_y),
Radius(approach_radius), Radius(approach_radius),
Circle(x=src_x, y=src_y, radius=SPACESHIP_RADIUS, color=SPACESHIP_COLOR), Circle(
Line(x=src_x, y=src_y, x2=dest_x, y2=dest_y, color=SPACESHIP_PATH_COLOR), x=src_x,
y=src_y,
radius=SPACESHIP_RADIUS,
color=SPACESHIP_COLOR,
),
) )
def generate_random_planet(world: World) -> int: def generate_random_planet(world: World) -> int:
radius: int = randint(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS) radius: int = randint(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS)
x: int = randint(radius, SCREEN_WIDTH - radius) x: float = float(randint(radius, SCREEN_WIDTH - radius))
y: int = randint(radius, SCREEN_HEIGHT - radius) y: float = float(randint(radius, SCREEN_HEIGHT - radius))
# color = (randint(0, 255), randint(0, 255), randint(0, 255), 255) # color = (randint(0, 255), randint(0, 255), randint(0, 255), 255)
color = choice(PLANET_COLORS) + (255,) color = choice(PLANET_COLORS) + (255,)
return create_planet(world, x, y, radius, color) return create_planet(world, x, y, radius, color)
Submodule libs/ecs updated: 4769b8d707...8ad4106b36.
+3 -3
View File
@@ -7,7 +7,7 @@ from components import (
Color, Color,
Radius, Radius,
SpaceShipMarker, SpaceShipMarker,
Target, Destination,
Vector2D, Vector2D,
) )
from settings import SPACESHIP_COLOR, SPACESHIP_RADIUS, SPACESHIP_SPEED from settings import SPACESHIP_COLOR, SPACESHIP_RADIUS, SPACESHIP_SPEED
@@ -43,7 +43,7 @@ class SpaceShipKillSystem:
marked_for_death = [] marked_for_death = []
for entity, component_storage in world.query(SpaceShipMarker): for entity, component_storage in world.query(SpaceShipMarker):
pos: Position = component_storage[Position] pos: Position = component_storage[Position]
target: Target = component_storage[Target] target: Destination = component_storage[Destination]
approach_radius = component_storage[Radius] approach_radius = component_storage[Radius]
if abs(pos - target) < approach_radius.value: if abs(pos - target) < approach_radius.value:
marked_for_death.append(entity) marked_for_death.append(entity)
@@ -56,7 +56,7 @@ class SpaceShipTravelSystem:
def process(cls, world: World, **args): def process(cls, world: World, **args):
for _, component_storage in world.query(SpaceShipMarker): for _, component_storage in world.query(SpaceShipMarker):
pos: Position = component_storage[Position] pos: Position = component_storage[Position]
target: Target = component_storage[Target] target: Destination = component_storage[Destination]
diff: Vector2D = target - pos diff: Vector2D = target - pos
diff_mag = abs(diff) diff_mag = abs(diff)
+46 -20
View File
@@ -1,12 +1,22 @@
from dataclasses import dataclass, field
from random import randint from random import randint
from typing import cast
import pyglet import pyglet
from pyglet.window.mouse import LEFT from pyglet.window.mouse import LEFT
from pyglet.shapes import Circle, Line from pyglet.shapes import Circle, Line
from components import PlanetMarker, Position, Radius, SpaceShipMarker, Vector2D from components import (
from helpers import generate_random_planet, generate_random_spaceship PlanetMarker,
Position,
Radius,
SpaceShipMarker,
Vector2D,
)
from helpers import (
GameState,
generate_random_planet,
)
from libs.ecs.world import World from libs.ecs.world import World
from libs.ecs.event import EventBus
from systems import ( from systems import (
PlanetRenderSyncSystem, PlanetRenderSyncSystem,
SpaceShipKillSystem, SpaceShipKillSystem,
@@ -15,6 +25,7 @@ from systems import (
) )
from settings import FPS, FULLSCREEN, SCREEN_WIDTH, SCREEN_HEIGHT from settings import FPS, FULLSCREEN, SCREEN_WIDTH, SCREEN_HEIGHT
game_state = GameState()
world = World() world = World()
world.systems.extend( world.systems.extend(
( (
@@ -40,8 +51,8 @@ for _ in range(12):
@window.event @window.event
def on_draw(): def on_draw():
window.clear() window.clear()
for _, component_storage in world.query(SpaceShipMarker): for _, line in game_state.routes.items():
component_storage[Line].draw() line.draw()
for _, component_storage in world.query(SpaceShipMarker): for _, component_storage in world.query(SpaceShipMarker):
component_storage[Circle].draw() component_storage[Circle].draw()
@@ -52,29 +63,44 @@ def on_draw():
@window.event @window.event
def on_mouse_release(x, y, button, modifiers): def on_mouse_release(x, y, button, modifiers):
if button == LEFT: if button != LEFT:
closest_entity, component_storage = min( return
world.query(PlanetMarker), closest_entity, component_storage = min(
key=lambda p: abs(p[1][Position] - Vector2D(x, y)), world.query(PlanetMarker),
key=lambda p: abs(p[1][Position] - Vector2D(x, y)),
)
pos = component_storage[Position]
if component_storage[Radius].value < abs(pos - Vector2D(x, y)):
print("DESELECT")
game_state.selected_planet = None
elif game_state.selected_planet is None:
print("SELECT")
game_state.selected_planet = closest_entity
elif game_state.selected_planet != closest_entity:
first_planet, second_planet = sorted(
(game_state.selected_planet, closest_entity)
) )
pos = component_storage[Position] fpos: Position = cast(dict, world.get_components(first_planet))[Position]
if component_storage[Radius].value < abs(pos - Vector2D(x, y)): spos: Position = cast(dict, world.get_components(second_planet))[Position]
print("NO PLANET") route_key = (first_planet, second_planet)
if route_key in game_state.routes:
print("ROUTE DELETE")
game_state.routes.pop(route_key)
else: else:
print("PLANET") print("ROUTE CREATE")
game_state.routes[route_key] = Line(
x=fpos.x,
y=fpos.y,
x2=spos.x,
y2=spos.y,
)
game_state.selected_planet = None
def on_update(dt): def on_update(dt):
world.tick(dt=dt) world.tick(dt=dt)
def periodic_random_spaceship_generation(*args, **kwargs):
for i in range(randint(0, 5)):
generate_random_spaceship(world=world)
pyglet.clock.schedule_interval(on_update, 1 / FPS) pyglet.clock.schedule_interval(on_update, 1 / FPS)
pyglet.clock.schedule_interval(periodic_random_spaceship_generation, 0.5)
pyglet.app.run() pyglet.app.run()