Compare commits
2
Commits
0e3f07938c
...
8c414fe294
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c414fe294 | ||
|
|
00fb80382b |
No files matched your search
+6
-1
@@ -38,7 +38,12 @@ class Position(Vector2D):
|
||||
|
||||
|
||||
@dataclass
|
||||
class Target(Vector2D):
|
||||
class Source(Vector2D):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Destination(Vector2D):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
+26
-12
@@ -1,3 +1,6 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import cast
|
||||
|
||||
from pyglet.shapes import Circle, Line
|
||||
from libs.ecs.world import World
|
||||
from components import (
|
||||
@@ -6,7 +9,7 @@ from components import (
|
||||
Position,
|
||||
Radius,
|
||||
SpaceShipMarker,
|
||||
Target,
|
||||
Destination,
|
||||
)
|
||||
from random import choice, randint, sample
|
||||
from settings import (
|
||||
@@ -19,12 +22,19 @@ from settings import (
|
||||
SPACESHIP_PATH_COLOR,
|
||||
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(
|
||||
world: World,
|
||||
x: int,
|
||||
y: int,
|
||||
x: float,
|
||||
y: float,
|
||||
radius: int,
|
||||
color: tuple[int, int, int, int],
|
||||
) -> int:
|
||||
@@ -39,26 +49,30 @@ def create_planet(
|
||||
|
||||
def create_spaceship(
|
||||
world: World,
|
||||
src_x: int,
|
||||
src_y: int,
|
||||
dest_x: int,
|
||||
dest_y: int,
|
||||
src_x: float,
|
||||
src_y: float,
|
||||
dest_x: float,
|
||||
dest_y: float,
|
||||
approach_radius: int,
|
||||
):
|
||||
return world.spawn(
|
||||
SpaceShipMarker(),
|
||||
Position(x=src_x, y=src_y),
|
||||
Target(x=dest_x, y=dest_y),
|
||||
Destination(x=dest_x, y=dest_y),
|
||||
Radius(approach_radius),
|
||||
Circle(x=src_x, y=src_y, radius=SPACESHIP_RADIUS, color=SPACESHIP_COLOR),
|
||||
Line(x=src_x, y=src_y, x2=dest_x, y2=dest_y, color=SPACESHIP_PATH_COLOR),
|
||||
Circle(
|
||||
x=src_x,
|
||||
y=src_y,
|
||||
radius=SPACESHIP_RADIUS,
|
||||
color=SPACESHIP_COLOR,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def generate_random_planet(world: World) -> int:
|
||||
radius: int = randint(MIN_PLANET_RADIUS, MAX_PLANET_RADIUS)
|
||||
x: int = randint(radius, SCREEN_WIDTH - radius)
|
||||
y: int = randint(radius, SCREEN_HEIGHT - radius)
|
||||
x: float = float(randint(radius, SCREEN_WIDTH - radius))
|
||||
y: float = float(randint(radius, SCREEN_HEIGHT - radius))
|
||||
# color = (randint(0, 255), randint(0, 255), randint(0, 255), 255)
|
||||
color = choice(PLANET_COLORS) + (255,)
|
||||
return create_planet(world, x, y, radius, color)
|
||||
|
||||
+1
-1
Submodule libs/ecs updated: 4769b8d707...8ad4106b36.
+3
-3
@@ -7,7 +7,7 @@ from components import (
|
||||
Color,
|
||||
Radius,
|
||||
SpaceShipMarker,
|
||||
Target,
|
||||
Destination,
|
||||
Vector2D,
|
||||
)
|
||||
from settings import SPACESHIP_COLOR, SPACESHIP_RADIUS, SPACESHIP_SPEED
|
||||
@@ -43,7 +43,7 @@ class SpaceShipKillSystem:
|
||||
marked_for_death = []
|
||||
for entity, component_storage in world.query(SpaceShipMarker):
|
||||
pos: Position = component_storage[Position]
|
||||
target: Target = component_storage[Target]
|
||||
target: Destination = component_storage[Destination]
|
||||
approach_radius = component_storage[Radius]
|
||||
if abs(pos - target) < approach_radius.value:
|
||||
marked_for_death.append(entity)
|
||||
@@ -56,7 +56,7 @@ class SpaceShipTravelSystem:
|
||||
def process(cls, world: World, **args):
|
||||
for _, component_storage in world.query(SpaceShipMarker):
|
||||
pos: Position = component_storage[Position]
|
||||
target: Target = component_storage[Target]
|
||||
target: Destination = component_storage[Destination]
|
||||
|
||||
diff: Vector2D = target - pos
|
||||
diff_mag = abs(diff)
|
||||
|
||||
@@ -1,12 +1,22 @@
|
||||
from dataclasses import dataclass, field
|
||||
from random import randint
|
||||
from typing import cast
|
||||
|
||||
import pyglet
|
||||
from pyglet.window.mouse import LEFT
|
||||
from pyglet.shapes import Circle, Line
|
||||
from components import PlanetMarker, Position, Radius, SpaceShipMarker, Vector2D
|
||||
from helpers import generate_random_planet, generate_random_spaceship
|
||||
from components import (
|
||||
PlanetMarker,
|
||||
Position,
|
||||
Radius,
|
||||
SpaceShipMarker,
|
||||
Vector2D,
|
||||
)
|
||||
from helpers import (
|
||||
GameState,
|
||||
generate_random_planet,
|
||||
)
|
||||
from libs.ecs.world import World
|
||||
from libs.ecs.event import EventBus
|
||||
from systems import (
|
||||
PlanetRenderSyncSystem,
|
||||
SpaceShipKillSystem,
|
||||
@@ -15,6 +25,7 @@ from systems import (
|
||||
)
|
||||
from settings import FPS, FULLSCREEN, SCREEN_WIDTH, SCREEN_HEIGHT
|
||||
|
||||
game_state = GameState()
|
||||
world = World()
|
||||
world.systems.extend(
|
||||
(
|
||||
@@ -40,8 +51,8 @@ for _ in range(12):
|
||||
@window.event
|
||||
def on_draw():
|
||||
window.clear()
|
||||
for _, component_storage in world.query(SpaceShipMarker):
|
||||
component_storage[Line].draw()
|
||||
for _, line in game_state.routes.items():
|
||||
line.draw()
|
||||
|
||||
for _, component_storage in world.query(SpaceShipMarker):
|
||||
component_storage[Circle].draw()
|
||||
@@ -52,29 +63,44 @@ def on_draw():
|
||||
|
||||
@window.event
|
||||
def on_mouse_release(x, y, button, modifiers):
|
||||
if button == LEFT:
|
||||
closest_entity, component_storage = min(
|
||||
world.query(PlanetMarker),
|
||||
key=lambda p: abs(p[1][Position] - Vector2D(x, y)),
|
||||
if button != LEFT:
|
||||
return
|
||||
closest_entity, component_storage = min(
|
||||
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]
|
||||
if component_storage[Radius].value < abs(pos - Vector2D(x, y)):
|
||||
print("NO PLANET")
|
||||
fpos: Position = cast(dict, world.get_components(first_planet))[Position]
|
||||
spos: Position = cast(dict, world.get_components(second_planet))[Position]
|
||||
route_key = (first_planet, second_planet)
|
||||
if route_key in game_state.routes:
|
||||
print("ROUTE DELETE")
|
||||
game_state.routes.pop(route_key)
|
||||
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):
|
||||
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(periodic_random_spaceship_generation, 0.5)
|
||||
|
||||
pyglet.app.run()
|
||||
Reference in new issue
Block a user