2023-12-28 18:34:38 +00:00
|
|
|
"""
|
|
|
|
Module d'exemple de l'utilisation du moteur de jeu.
|
|
|
|
"""
|
|
|
|
|
2024-01-03 02:14:53 +00:00
|
|
|
from engine import Scene, start_game
|
2024-01-03 18:58:49 +00:00
|
|
|
from engine.ecs import Entity, World
|
|
|
|
from engine.math import Vec2
|
|
|
|
from plugins import defaults, physics
|
|
|
|
from plugins import render
|
|
|
|
from plugins.inputs import Held
|
|
|
|
from plugins.render import (
|
|
|
|
Origin,
|
|
|
|
Position,
|
|
|
|
Scale,
|
|
|
|
SpriteBundle,
|
|
|
|
)
|
|
|
|
from plugins.timing import Delta
|
|
|
|
|
|
|
|
|
2024-01-04 00:12:25 +00:00
|
|
|
def lol(a: Entity, b: Entity, simul: bool = False):
|
2024-01-03 18:58:49 +00:00
|
|
|
if Bounce in b:
|
|
|
|
speed = a[physics.Velocity].length
|
|
|
|
a[physics.Velocity] = a[physics.Velocity].normalized
|
|
|
|
a[physics.Velocity].y = (a[Position].y - b[Position].y) * 0.005
|
|
|
|
a[physics.Velocity] = a[physics.Velocity].normalized * min(
|
2024-01-04 00:12:25 +00:00
|
|
|
(speed * 1.1), 1000.0
|
2024-01-03 18:58:49 +00:00
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Bounce:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def lol_simul(a: Entity, b: Entity):
|
2024-01-04 00:12:25 +00:00
|
|
|
lol(a, b, True)
|
2024-01-03 18:58:49 +00:00
|
|
|
return RightWall not in b
|
|
|
|
|
|
|
|
|
|
|
|
class Simulated:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Bar:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class RightWall:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class BallFollow(int):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Mine:
|
|
|
|
pass
|
2023-12-28 18:34:38 +00:00
|
|
|
|
|
|
|
|
2024-01-03 02:14:53 +00:00
|
|
|
def __initialize(world: World):
|
|
|
|
"""
|
|
|
|
Initialise les ressources pour le moteur de jeu.
|
|
|
|
"""
|
|
|
|
world.new_entity().set(
|
2024-01-03 18:58:49 +00:00
|
|
|
SpriteBundle(
|
|
|
|
"background.png",
|
|
|
|
-1,
|
|
|
|
scale=Vec2(render.WIDTH, render.HEIGHT),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
# world.new_entity().set(
|
|
|
|
# SpriteBundle(
|
|
|
|
# "dodo.png",
|
|
|
|
# 0,
|
|
|
|
# position=Vec2(800, 500),
|
|
|
|
# origin=Vec2(0.5, 0.5),
|
|
|
|
# scale=Vec2(400, 300),
|
|
|
|
# ),
|
|
|
|
# physics.Solid(),
|
|
|
|
# )
|
|
|
|
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
0,
|
|
|
|
position=Vec2(100, 100),
|
|
|
|
origin=Vec2(0, 0),
|
|
|
|
scale=Vec2(render.WIDTH - 200, 10),
|
|
|
|
),
|
|
|
|
physics.Solid(),
|
|
|
|
)
|
|
|
|
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
0,
|
|
|
|
position=Vec2(100, render.HEIGHT - 100),
|
|
|
|
origin=Vec2(0, 1),
|
|
|
|
scale=Vec2(render.WIDTH - 200, 10),
|
|
|
|
),
|
|
|
|
physics.Solid(),
|
2024-01-03 02:14:53 +00:00
|
|
|
)
|
|
|
|
|
2024-01-03 18:58:49 +00:00
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
0,
|
|
|
|
position=Vec2(render.WIDTH - 100, 100),
|
|
|
|
origin=Vec2(1, 0),
|
|
|
|
scale=Vec2(10, render.HEIGHT - 200),
|
|
|
|
),
|
|
|
|
physics.Solid(),
|
|
|
|
RightWall(),
|
|
|
|
)
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
0,
|
|
|
|
position=Vec2(100, 100),
|
|
|
|
origin=Vec2(0, 0),
|
|
|
|
scale=Vec2(10, render.HEIGHT - 200),
|
|
|
|
),
|
|
|
|
physics.Solid(),
|
|
|
|
)
|
|
|
|
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
0,
|
|
|
|
position=Vec2(render.WIDTH - 130, render.HEIGHT / 2),
|
|
|
|
origin=Vec2(0.5, 0.5),
|
|
|
|
scale=Vec2(10, 200),
|
|
|
|
),
|
|
|
|
physics.Solid(),
|
|
|
|
Bar(),
|
|
|
|
Bounce(),
|
|
|
|
)
|
|
|
|
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
0,
|
|
|
|
position=Vec2(130, render.HEIGHT / 2),
|
|
|
|
origin=Vec2(0.5, 0.5),
|
|
|
|
scale=Vec2(10, 200),
|
|
|
|
),
|
|
|
|
physics.Solid(),
|
|
|
|
Mine(),
|
|
|
|
Bounce(),
|
|
|
|
)
|
|
|
|
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dada.png",
|
|
|
|
1,
|
|
|
|
scale=Vec2(10),
|
|
|
|
position=Vec2(500, 500),
|
|
|
|
origin=Vec2(0.5, 0.5),
|
|
|
|
),
|
|
|
|
physics.Velocity(Vec2(200, 100)),
|
|
|
|
physics.CollisionHandler(lol),
|
|
|
|
)
|
|
|
|
|
2024-01-04 00:12:25 +00:00
|
|
|
for i in range(20):
|
|
|
|
world.new_entity().set(
|
|
|
|
SpriteBundle(
|
|
|
|
"dodo.png",
|
|
|
|
10,
|
|
|
|
scale=Vec2(10),
|
|
|
|
),
|
|
|
|
physics.CollisionHandler(lol),
|
|
|
|
BallFollow(i + 1),
|
|
|
|
# physics.CollisionHandler(lol_simul),
|
|
|
|
)
|
|
|
|
|
2024-01-03 18:58:49 +00:00
|
|
|
|
|
|
|
def __update(world: World):
|
|
|
|
"""
|
|
|
|
Test.
|
|
|
|
"""
|
|
|
|
for entity in world.query(Mine, Position):
|
|
|
|
if "z" in world[Held]:
|
|
|
|
entity[Position].y -= 300 * world[Delta]
|
|
|
|
if "s" in world[Held]:
|
|
|
|
entity[Position].y += 300 * world[Delta]
|
|
|
|
|
|
|
|
ball = max(
|
|
|
|
world.query(Position, physics.Velocity, physics.CollisionHandler),
|
|
|
|
key=lambda e: e[Position].x,
|
|
|
|
)
|
|
|
|
for bar in world.query(Bar):
|
|
|
|
bar.remove(physics.Solid)
|
|
|
|
entity = world.new_entity()
|
|
|
|
entity.set(
|
|
|
|
Position(ball[Position]),
|
|
|
|
Scale(ball[Scale]),
|
|
|
|
physics.Velocity(ball[physics.Velocity]),
|
|
|
|
Origin(ball[Origin]),
|
|
|
|
physics.CollisionHandler(lol_simul),
|
|
|
|
)
|
|
|
|
physics.move_entity(entity, entity[physics.Velocity] * 500)
|
|
|
|
target = entity[Position].y
|
|
|
|
for bar in world.query(Bar):
|
|
|
|
diff = target - bar[Position].y
|
2024-01-04 00:12:25 +00:00
|
|
|
# bar[Position].y += (diff / abs(diff)) * 300 * world[Delta]
|
|
|
|
bar[Position].y += diff
|
2024-01-03 18:58:49 +00:00
|
|
|
bar.set(physics.Solid())
|
|
|
|
entity.destroy()
|
|
|
|
|
2024-01-04 00:12:25 +00:00
|
|
|
# for bar in world.query(Bar):
|
|
|
|
# bar.remove(physics.Solid)
|
|
|
|
for entity in world.query(BallFollow):
|
|
|
|
entity[Position] = Vec2(ball[Position])
|
|
|
|
entity[physics.Velocity] = Vec2(ball[physics.Velocity])
|
|
|
|
physics.move_entity(entity, entity[physics.Velocity] * entity[BallFollow] * 0.5)
|
|
|
|
del entity[physics.Velocity]
|
|
|
|
# for bar in world.query(Bar):
|
|
|
|
# bar.set(physics.Solid())
|
|
|
|
|
2024-01-03 18:58:49 +00:00
|
|
|
# ball.set(Simulated())
|
|
|
|
# for entity in world.query(Bar):
|
|
|
|
# entity.remove(physics.Solid)
|
|
|
|
# last_position = Vec2(ball[Position])
|
|
|
|
# last_velocity = Vec2(ball[physics.Velocity])
|
|
|
|
# physics.move_entity(ball, ball[physics.Velocity] * 5000)
|
|
|
|
# ball[Position] = last_position
|
|
|
|
# ball[physics.Velocity] = last_velocity
|
|
|
|
# for entity in world.query(Bar):
|
|
|
|
# entity.set(physics.Solid())
|
|
|
|
# ball.remove(Simulated)
|
|
|
|
|
2024-01-03 02:14:53 +00:00
|
|
|
|
|
|
|
MENU = Scene(
|
|
|
|
[__initialize],
|
2024-01-03 18:58:49 +00:00
|
|
|
[__update],
|
2024-01-03 02:14:53 +00:00
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
start_game(defaults.PLUGIN, MENU)
|