gamengine3d 1.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gamengine3d/__init__.py +15 -0
- gamengine3d/cli.py +34 -0
- gamengine3d/context.py +212 -0
- gamengine3d/engine.py +263 -0
- gamengine3d/examples/__init__.py +0 -0
- gamengine3d/examples/basics.py +11 -0
- gamengine3d/examples/player_movement.py +83 -0
- gamengine3d/examples/scripts/cylinder.py +21 -0
- gamengine3d/examples/scripts/player_movement.py +94 -0
- gamengine3d/examples/scripts/settings.py +6 -0
- gamengine3d/examples/the_everything_demo.py +42 -0
- gamengine3d/game_objects.py +185 -0
- gamengine3d/helper.py +726 -0
- gamengine3d/shaders/__init__.py +0 -0
- gamengine3d/shaders/default.frag +64 -0
- gamengine3d/shaders/default.vert +24 -0
- gamengine3d/shaders/depth.frag +2 -0
- gamengine3d/shaders/depth.vert +9 -0
- gamengine3d-1.6.0.dist-info/METADATA +80 -0
- gamengine3d-1.6.0.dist-info/RECORD +24 -0
- gamengine3d-1.6.0.dist-info/WHEEL +5 -0
- gamengine3d-1.6.0.dist-info/entry_points.txt +2 -0
- gamengine3d-1.6.0.dist-info/licenses/LICENSE.txt +21 -0
- gamengine3d-1.6.0.dist-info/top_level.txt +1 -0
gamengine3d/__init__.py
ADDED
gamengine3d/cli.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
|
|
2
|
+
import argparse
|
|
3
|
+
import runpy
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
def main():
|
|
7
|
+
parser = argparse.ArgumentParser(prog="gamengine3d")
|
|
8
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
9
|
+
|
|
10
|
+
# Get the list of available demos from the examples folder
|
|
11
|
+
examples_dir = Path(__file__).parent / "examples"
|
|
12
|
+
available_demos = [
|
|
13
|
+
f.stem for f in examples_dir.glob("*.py") if f.is_file() and f.name != "__init__.py"
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
demo_cmd = sub.add_parser("demo", help="Run a built-in example")
|
|
17
|
+
demo_cmd.add_argument(
|
|
18
|
+
"demo_name",
|
|
19
|
+
nargs="?",
|
|
20
|
+
default="basics",
|
|
21
|
+
choices=available_demos, # restrict to available demos
|
|
22
|
+
help=f"The name of the example to run. Available: {', '.join(available_demos)}"
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
args = parser.parse_args()
|
|
26
|
+
|
|
27
|
+
if args.command == "demo":
|
|
28
|
+
module = f"gamengine3d.examples.{args.demo_name}"
|
|
29
|
+
print(f"Running example: {args.demo_name}")
|
|
30
|
+
print(f"Path: {module}")
|
|
31
|
+
runpy.run_module(module, run_name="__main__")
|
|
32
|
+
|
|
33
|
+
if __name__ == "__main__":
|
|
34
|
+
main()
|
gamengine3d/context.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
from .game_objects import GameObject
|
|
2
|
+
from .helper import Color, vector3d, Light, Camera, Renderer
|
|
3
|
+
from pygame.time import Clock
|
|
4
|
+
import time
|
|
5
|
+
import importlib
|
|
6
|
+
|
|
7
|
+
class Functions:
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self.draw_cube = lambda pos=vector3d.zero, size=vector3d(3), color=Color.light_blue: None
|
|
10
|
+
self.draw_sphere = lambda pos=vector3d.zero, radius=2, color=Color.light_red, segments=32, rings=16: None
|
|
11
|
+
self.draw_cylinder = lambda pos=vector3d.zero, length=2, radius=.5, color=Color.light_yellow, segments=32: None
|
|
12
|
+
self.add_light = lambda light: None
|
|
13
|
+
self.add_object = lambda obj: None
|
|
14
|
+
self.is_colliding_pos = lambda name, pos: None
|
|
15
|
+
self.is_colliding = lambda name1, name2: None
|
|
16
|
+
self.get_game_object = lambda name: None
|
|
17
|
+
self.remove_object = lambda name: None
|
|
18
|
+
self.remove_light = lambda name: None
|
|
19
|
+
|
|
20
|
+
class Delay:
|
|
21
|
+
def __init__(self, delay_time, callback):
|
|
22
|
+
self.start_time = time.perf_counter()
|
|
23
|
+
self.delay_time = delay_time
|
|
24
|
+
self.callback = callback
|
|
25
|
+
self.finished = False
|
|
26
|
+
|
|
27
|
+
def update(self):
|
|
28
|
+
if time.perf_counter() - self.start_time >= self.delay_time:
|
|
29
|
+
self.callback()
|
|
30
|
+
self.finished = True
|
|
31
|
+
|
|
32
|
+
class DelayVar:
|
|
33
|
+
def __init__(self, delay, obj, attr, value):
|
|
34
|
+
self.start_time = time.perf_counter()
|
|
35
|
+
self.delay = delay
|
|
36
|
+
self.obj = obj
|
|
37
|
+
self.attr = attr
|
|
38
|
+
self.value = value
|
|
39
|
+
self.finished = False
|
|
40
|
+
|
|
41
|
+
def update(self):
|
|
42
|
+
if time.perf_counter() - self.start_time >= self.delay:
|
|
43
|
+
setattr(self.obj, self.attr, self.value)
|
|
44
|
+
self.finished = True
|
|
45
|
+
|
|
46
|
+
class KeyCallback:
|
|
47
|
+
def __init__(self, key, event_type, callback, args=None, dt=False):
|
|
48
|
+
self.callback = callback
|
|
49
|
+
self.key = key
|
|
50
|
+
self.event_type = event_type
|
|
51
|
+
self.dt = dt
|
|
52
|
+
if not args:
|
|
53
|
+
args = []
|
|
54
|
+
self.args = args
|
|
55
|
+
|
|
56
|
+
def update(self, pressed_keys, held_keys, released_keys, dt):
|
|
57
|
+
if self.event_type == "pressed":
|
|
58
|
+
if self.key in pressed_keys:
|
|
59
|
+
if self.dt:
|
|
60
|
+
self.callback(dt, *self.args)
|
|
61
|
+
else:
|
|
62
|
+
self.callback(*self.args)
|
|
63
|
+
|
|
64
|
+
elif self.event_type == "held":
|
|
65
|
+
if self.key in held_keys:
|
|
66
|
+
if self.dt:
|
|
67
|
+
self.callback(dt, *self.args)
|
|
68
|
+
else:
|
|
69
|
+
self.callback(*self.args)
|
|
70
|
+
|
|
71
|
+
elif self.event_type == "released":
|
|
72
|
+
if self.key in released_keys:
|
|
73
|
+
if self.dt:
|
|
74
|
+
self.callback(dt, *self.args)
|
|
75
|
+
else:
|
|
76
|
+
self.callback(*self.args)
|
|
77
|
+
|
|
78
|
+
class KeyCallbackVar:
|
|
79
|
+
def __init__(self, key, event_type, obj, attr, value):
|
|
80
|
+
self.key = key
|
|
81
|
+
self.event_type = event_type
|
|
82
|
+
self.obj = obj
|
|
83
|
+
self.attr = attr
|
|
84
|
+
self.value = value
|
|
85
|
+
|
|
86
|
+
def update(self, pressed_keys, held_keys, releaed_keys):
|
|
87
|
+
if self.event_type == "pressed":
|
|
88
|
+
if self.key in pressed_keys:
|
|
89
|
+
setattr(self.obj, self.attr, self.value)
|
|
90
|
+
|
|
91
|
+
elif self.event_type == "held":
|
|
92
|
+
if self.key in held_keys:
|
|
93
|
+
setattr(self.obj, self.attr, self.value)
|
|
94
|
+
|
|
95
|
+
elif self.event_type == "released":
|
|
96
|
+
if self.key in releaed_keys:
|
|
97
|
+
setattr(self.obj, self.attr, self.value)
|
|
98
|
+
|
|
99
|
+
class ScheduledCaller:
|
|
100
|
+
def __init__(self, n_times, callback, delay, caller_id):
|
|
101
|
+
self.n_times = n_times
|
|
102
|
+
self.callback = callback
|
|
103
|
+
self.delay = delay
|
|
104
|
+
self.caller_id = caller_id
|
|
105
|
+
self.finished = False
|
|
106
|
+
self.delay_obj = Delay(self.delay, self.callback)
|
|
107
|
+
self.forever = False
|
|
108
|
+
if n_times == -1:
|
|
109
|
+
self.forever = True
|
|
110
|
+
|
|
111
|
+
def update(self):
|
|
112
|
+
self.delay_obj.update()
|
|
113
|
+
if self.delay_obj.finished:
|
|
114
|
+
self.n_times -= 1
|
|
115
|
+
self.delay_obj = Delay(self.delay, self.callback)
|
|
116
|
+
|
|
117
|
+
if not self.forever:
|
|
118
|
+
self.finished = self.n_times <= 0
|
|
119
|
+
|
|
120
|
+
class RuntimeVars:
|
|
121
|
+
def from_settings(self, module_name):
|
|
122
|
+
module = importlib.import_module(module_name)
|
|
123
|
+
for attr_name in dir(module):
|
|
124
|
+
if not attr_name.startswith("__"):
|
|
125
|
+
setattr(self, attr_name, getattr(module, attr_name))
|
|
126
|
+
|
|
127
|
+
class Context:
|
|
128
|
+
def __init__(self):
|
|
129
|
+
self.functions: Functions = Functions()
|
|
130
|
+
self.ambient_light: float = .2
|
|
131
|
+
self.lights: list[Light] = []
|
|
132
|
+
self.game_objects: list[GameObject] = []
|
|
133
|
+
self.mouse_sensitivity: float = 0.3
|
|
134
|
+
self.pan_sensitivity: float = 0.005
|
|
135
|
+
self.fps: int | float = 0
|
|
136
|
+
self.engine = None
|
|
137
|
+
self.camera: Camera | None = None
|
|
138
|
+
self.clock: Clock | None = None
|
|
139
|
+
self.renderer: Renderer | None = None
|
|
140
|
+
self.keys_held: list[str] = []
|
|
141
|
+
self.keys_pressed: list[str] = []
|
|
142
|
+
self.keys_released: list[str] = []
|
|
143
|
+
self.key_callbacks: list[KeyCallback] = []
|
|
144
|
+
self.key_callback_vars: list[KeyCallbackVar] = []
|
|
145
|
+
self.delays: list[Delay] = []
|
|
146
|
+
self.delay_vars: list[DelayVar] = []
|
|
147
|
+
self.scheduled_callers: list[ScheduledCaller] = []
|
|
148
|
+
self.runtime_vars = RuntimeVars()
|
|
149
|
+
self.exit = False
|
|
150
|
+
|
|
151
|
+
def on_key_press(self, key, callback, args=None, dt=False):
|
|
152
|
+
if args is None:
|
|
153
|
+
args = []
|
|
154
|
+
self.key_callbacks.append(KeyCallback(key, "pressed", callback, args, dt))
|
|
155
|
+
|
|
156
|
+
def on_key_held(self, key, callback, args=None, dt=False):
|
|
157
|
+
if args is None:
|
|
158
|
+
args = []
|
|
159
|
+
self.key_callbacks.append(KeyCallback(key, "held", callback, args, dt))
|
|
160
|
+
|
|
161
|
+
def on_key_released(self, key, callback, args=None, dt=False):
|
|
162
|
+
if args is None:
|
|
163
|
+
args = []
|
|
164
|
+
self.key_callbacks.append(KeyCallback(key, "released", callback, args, dt))
|
|
165
|
+
|
|
166
|
+
def on_key_press_var(self, key, obj, attr, value):
|
|
167
|
+
self.key_callback_vars.append(KeyCallbackVar(key, "pressed", obj, attr, value))
|
|
168
|
+
|
|
169
|
+
def on_key_held_var(self, key, obj, attr, value):
|
|
170
|
+
self.key_callback_vars.append(KeyCallbackVar(key, "held", obj, attr, value))
|
|
171
|
+
|
|
172
|
+
def on_key_released_var(self, key, obj, attr, value):
|
|
173
|
+
self.key_callback_vars.append(KeyCallbackVar(key, "released", obj, attr, value))
|
|
174
|
+
|
|
175
|
+
def update(self, dt):
|
|
176
|
+
for callback in self.key_callbacks:
|
|
177
|
+
callback.update(self.keys_pressed, self.keys_held, self.keys_released, dt)
|
|
178
|
+
|
|
179
|
+
for callback_var in self.key_callback_vars:
|
|
180
|
+
callback_var.update(self.keys_pressed, self.keys_held, self.keys_released)
|
|
181
|
+
|
|
182
|
+
delays = self.delays[:]
|
|
183
|
+
for delay in delays:
|
|
184
|
+
delay.update()
|
|
185
|
+
if delay.finished:
|
|
186
|
+
self.delays.remove(delay)
|
|
187
|
+
|
|
188
|
+
delay_vars = self.delay_vars[:]
|
|
189
|
+
for delay in delay_vars:
|
|
190
|
+
delay.update()
|
|
191
|
+
if delay.finished:
|
|
192
|
+
self.delay_vars.remove(delay)
|
|
193
|
+
|
|
194
|
+
scheduled_callers = self.scheduled_callers[:]
|
|
195
|
+
for caller in scheduled_callers:
|
|
196
|
+
caller.update()
|
|
197
|
+
if caller.finished:
|
|
198
|
+
self.scheduled_callers.remove(caller)
|
|
199
|
+
|
|
200
|
+
def add_delay(self, delay, callback):
|
|
201
|
+
self.delays.append(Delay(delay, callback))
|
|
202
|
+
|
|
203
|
+
def add_delay_var(self, delay, obj, attr, value):
|
|
204
|
+
self.delay_vars.append(DelayVar(delay, obj, attr, value))
|
|
205
|
+
|
|
206
|
+
def add_scheduled_caller(self, n_times, delay, callback, caller_id=None):
|
|
207
|
+
if caller_id is None:
|
|
208
|
+
max_id = -1
|
|
209
|
+
if self.scheduled_callers:
|
|
210
|
+
max_id = max([caller.caller_id for caller in self.scheduled_callers])
|
|
211
|
+
caller_id = max_id + 1
|
|
212
|
+
self.scheduled_callers.append(ScheduledCaller(n_times, callback, delay, caller_id))
|
gamengine3d/engine.py
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
from itertools import product
|
|
2
|
+
from pyrr import Matrix44
|
|
3
|
+
from .context import Context
|
|
4
|
+
import pygame
|
|
5
|
+
from .game_objects import Cuboid, GameObject, Sphere
|
|
6
|
+
from .helper import Renderer, vector3d, Color, Camera, Light, EngineError, Script
|
|
7
|
+
import math
|
|
8
|
+
|
|
9
|
+
class Engine:
|
|
10
|
+
def __init__(self, width=500, height=500, resizable=True, name="GamEngine3d", background_color=Color.light_grey, ambient_light=.2):
|
|
11
|
+
self.width = width
|
|
12
|
+
self.height = height
|
|
13
|
+
self.resizable = resizable
|
|
14
|
+
self.window_name = name
|
|
15
|
+
self.background_color = background_color
|
|
16
|
+
self.scripts = []
|
|
17
|
+
|
|
18
|
+
flags = pygame.DOUBLEBUF | pygame.OPENGL
|
|
19
|
+
if self.resizable:
|
|
20
|
+
flags |= pygame.RESIZABLE
|
|
21
|
+
|
|
22
|
+
pygame.display.set_caption(self.window_name)
|
|
23
|
+
self.renderer = Renderer(width, height, flags=flags)
|
|
24
|
+
self.renderer.ambient = ambient_light
|
|
25
|
+
self.clock = pygame.time.Clock()
|
|
26
|
+
|
|
27
|
+
self.camera = Camera(position=vector3d(0, 1.5, 3),
|
|
28
|
+
target=vector3d(0, 0, 0),
|
|
29
|
+
aspect_ratio=self.width / self.height)
|
|
30
|
+
|
|
31
|
+
self.last_mouse_pos = None
|
|
32
|
+
self.mouse_sensitivity = 0.3
|
|
33
|
+
self.pan_sensitivity = 0.005
|
|
34
|
+
self.active_button = None
|
|
35
|
+
|
|
36
|
+
self.gameobjects = []
|
|
37
|
+
|
|
38
|
+
self.context = Context()
|
|
39
|
+
|
|
40
|
+
self.context.functions.draw_cube = self.draw_cuboid
|
|
41
|
+
self.context.functions.draw_sphere = self.draw_sphere
|
|
42
|
+
self.context.functions.add_light = self.add_light
|
|
43
|
+
self.context.functions.add_object = self.add_object
|
|
44
|
+
self.context.functions.draw_cylinder = self.draw_cylinder
|
|
45
|
+
self.context.functions.get_game_object = self.get_object
|
|
46
|
+
self.context.functions.is_colliding = self.is_colliding
|
|
47
|
+
self.context.functions.remove_light = self.remove_light
|
|
48
|
+
self.context.functions.remove_object = self.remove_object
|
|
49
|
+
self.context.functions.is_colliding_pos = self.is_colliding_pos
|
|
50
|
+
|
|
51
|
+
self.context.game_objects = self.gameobjects
|
|
52
|
+
self.context.engine = self
|
|
53
|
+
self.context.camera = self.camera
|
|
54
|
+
self.context.renderer = self.renderer
|
|
55
|
+
self.context.clock = self.clock
|
|
56
|
+
|
|
57
|
+
self.context.lights = self.renderer.lights
|
|
58
|
+
|
|
59
|
+
def run(self, fps=60, dynamic_view=True):
|
|
60
|
+
running = True
|
|
61
|
+
self.context.fps = fps
|
|
62
|
+
keys_held = []
|
|
63
|
+
self.context.keys_held = keys_held
|
|
64
|
+
self.init_scripts()
|
|
65
|
+
|
|
66
|
+
for script in self.scripts:
|
|
67
|
+
script.run("on_start")
|
|
68
|
+
|
|
69
|
+
while running:
|
|
70
|
+
keys_pressed = []
|
|
71
|
+
keys_released = []
|
|
72
|
+
dt = self.clock.get_time() / 1000
|
|
73
|
+
self.mouse_sensitivity = self.context.mouse_sensitivity
|
|
74
|
+
self.pan_sensitivity = self.context.pan_sensitivity
|
|
75
|
+
self.context.fps = self.clock.get_fps()
|
|
76
|
+
|
|
77
|
+
for event in pygame.event.get():
|
|
78
|
+
if event.type == pygame.QUIT:
|
|
79
|
+
running = False
|
|
80
|
+
|
|
81
|
+
if self.resizable and event.type == pygame.VIDEORESIZE:
|
|
82
|
+
self.width, self.height = event.w, event.h
|
|
83
|
+
self.renderer.resize(self.width, self.height)
|
|
84
|
+
|
|
85
|
+
self.camera.aspect_ratio = self.width / self.height
|
|
86
|
+
|
|
87
|
+
old_pos = self.camera.position.copy()
|
|
88
|
+
self.camera.move_to(vector3d.one)
|
|
89
|
+
self.camera.move_to(old_pos)
|
|
90
|
+
|
|
91
|
+
if dynamic_view:
|
|
92
|
+
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
93
|
+
if event.button in (1, 3): # Left or right
|
|
94
|
+
self.last_mouse_pos = pygame.mouse.get_pos()
|
|
95
|
+
self.active_button = event.button
|
|
96
|
+
elif event.type == pygame.MOUSEBUTTONUP:
|
|
97
|
+
if event.button in (1, 3):
|
|
98
|
+
self.last_mouse_pos = None
|
|
99
|
+
self.active_button = None
|
|
100
|
+
|
|
101
|
+
elif event.type == pygame.MOUSEMOTION:
|
|
102
|
+
if self.last_mouse_pos is not None:
|
|
103
|
+
x, y = pygame.mouse.get_pos()
|
|
104
|
+
dx = x - self.last_mouse_pos[0]
|
|
105
|
+
dy = y - self.last_mouse_pos[1]
|
|
106
|
+
self.last_mouse_pos = (x, y)
|
|
107
|
+
|
|
108
|
+
if self.active_button == 3:
|
|
109
|
+
self.camera.rotate_around_target(-dx * self.mouse_sensitivity, dy * self.mouse_sensitivity)
|
|
110
|
+
|
|
111
|
+
elif self.active_button == 1:
|
|
112
|
+
self._pan_camera(dx, dy)
|
|
113
|
+
|
|
114
|
+
elif event.type == pygame.MOUSEWHEEL:
|
|
115
|
+
self.camera.zoom(event.y * 0.5)
|
|
116
|
+
|
|
117
|
+
if event.type == pygame.KEYDOWN:
|
|
118
|
+
key_name = pygame.key.name(event.key)
|
|
119
|
+
keys_pressed.append(key_name)
|
|
120
|
+
keys_held.append(key_name)
|
|
121
|
+
|
|
122
|
+
elif event.type == pygame.KEYUP:
|
|
123
|
+
key_name = pygame.key.name(event.key)
|
|
124
|
+
keys_held.remove(key_name)
|
|
125
|
+
keys_released.append(key_name)
|
|
126
|
+
|
|
127
|
+
self.context.keys_pressed = keys_pressed
|
|
128
|
+
self.context.keys_released = keys_released
|
|
129
|
+
self.context.update(dt)
|
|
130
|
+
|
|
131
|
+
self.renderer.render_shadow_pass(self.gameobjects)
|
|
132
|
+
self.renderer.clear(self.background_color)
|
|
133
|
+
self.camera.update_renderer(self.renderer)
|
|
134
|
+
|
|
135
|
+
self.renderer.ambient = self.context.ambient_light
|
|
136
|
+
|
|
137
|
+
for script in self.scripts:
|
|
138
|
+
script.update(dt)
|
|
139
|
+
|
|
140
|
+
for obj in self.gameobjects:
|
|
141
|
+
obj.update(dt)
|
|
142
|
+
obj.draw(self.renderer)
|
|
143
|
+
|
|
144
|
+
if self.context.exit:
|
|
145
|
+
running = False
|
|
146
|
+
|
|
147
|
+
self.renderer.swap()
|
|
148
|
+
self.clock.tick(fps)
|
|
149
|
+
|
|
150
|
+
for script in self.scripts:
|
|
151
|
+
script.run("on_exit")
|
|
152
|
+
self.renderer.quit()
|
|
153
|
+
|
|
154
|
+
def _pan_camera(self, dx, dy):
|
|
155
|
+
forward = (self.camera.target - self.camera.position).normalized
|
|
156
|
+
right = forward.cross(self.camera.up).normalized
|
|
157
|
+
up = self.camera.up.normalized
|
|
158
|
+
|
|
159
|
+
pan = (right * -dx + up * dy) * self.pan_sensitivity * self.camera.radius
|
|
160
|
+
self.camera.target += pan
|
|
161
|
+
self.camera.position += pan
|
|
162
|
+
self.camera._update_position()
|
|
163
|
+
|
|
164
|
+
def add_light(self, light):
|
|
165
|
+
self.renderer.add_light(light)
|
|
166
|
+
if light.show:
|
|
167
|
+
self.add_object(Sphere(pos=light.position, color=light.color, radius=.1))
|
|
168
|
+
|
|
169
|
+
def add_object(self, obj: GameObject):
|
|
170
|
+
self.gameobjects.append(obj)
|
|
171
|
+
|
|
172
|
+
def remove_object(self, name):
|
|
173
|
+
obj = self.get_object(name)
|
|
174
|
+
self.gameobjects.remove(obj)
|
|
175
|
+
|
|
176
|
+
def remove_light(self, name):
|
|
177
|
+
for light in self.renderer.lights:
|
|
178
|
+
if light.name == name:
|
|
179
|
+
self.renderer.lights.remove(light)
|
|
180
|
+
return
|
|
181
|
+
|
|
182
|
+
raise EngineError(f"No Light Object Named {name}")
|
|
183
|
+
|
|
184
|
+
def get_object(self, name):
|
|
185
|
+
for obj in self.gameobjects:
|
|
186
|
+
if obj.name == name:
|
|
187
|
+
return obj
|
|
188
|
+
|
|
189
|
+
raise EngineError(f"Game Object with name {name} not found")
|
|
190
|
+
|
|
191
|
+
def is_colliding(self, name1, name2):
|
|
192
|
+
obj1 = self.get_object(name1)
|
|
193
|
+
obj2 = self.get_object(name2)
|
|
194
|
+
|
|
195
|
+
if not (isinstance(obj1, Cuboid) and isinstance(obj2, Cuboid)):
|
|
196
|
+
raise NotImplementedError(f"Cylinder And Sphere Collision Not Implemented")
|
|
197
|
+
|
|
198
|
+
corners = obj1.get_corners()
|
|
199
|
+
for corner in corners:
|
|
200
|
+
if obj2.is_point_in(corner):
|
|
201
|
+
return True
|
|
202
|
+
|
|
203
|
+
return False
|
|
204
|
+
|
|
205
|
+
def is_colliding_pos(self, name, pos):
|
|
206
|
+
obj = self.get_object(name)
|
|
207
|
+
if not isinstance(obj, Cuboid):
|
|
208
|
+
raise NotImplementedError(f"Cylinder And Sphere Collision Not Implemented")
|
|
209
|
+
|
|
210
|
+
return obj.is_point_in(pos)
|
|
211
|
+
|
|
212
|
+
def draw_cuboid(self, pos: vector3d=vector3d.zero, size: vector3d=vector3d(2), rotation: vector3d=vector3d.zero, color: Color = Color.light_blue):
|
|
213
|
+
faces = [
|
|
214
|
+
[0, 1, 3, 2],
|
|
215
|
+
[4, 6, 7, 5],
|
|
216
|
+
[0, 4, 5, 1],
|
|
217
|
+
[2, 3, 7, 6],
|
|
218
|
+
[0, 2, 6, 4],
|
|
219
|
+
[1, 5, 7, 3],
|
|
220
|
+
]
|
|
221
|
+
|
|
222
|
+
half_size = size / 2
|
|
223
|
+
|
|
224
|
+
rot_matrix = Matrix44.from_eulers((
|
|
225
|
+
math.radians(rotation.x),
|
|
226
|
+
math.radians(rotation.y),
|
|
227
|
+
math.radians(rotation.z)
|
|
228
|
+
))
|
|
229
|
+
|
|
230
|
+
corners = []
|
|
231
|
+
for dx, dy, dz in product([-1, 1], repeat=3):
|
|
232
|
+
local = vector3d(dx * half_size.x, dy * half_size.y, dz * half_size.z)
|
|
233
|
+
|
|
234
|
+
rotated = vector3d(
|
|
235
|
+
*(rot_matrix @ [local.x, local.y, local.z, 1.0])[:3]
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
world = pos + rotated
|
|
239
|
+
corners.append(world)
|
|
240
|
+
|
|
241
|
+
for face_idx in faces:
|
|
242
|
+
self.renderer.render_quad(
|
|
243
|
+
corners[face_idx[0]],
|
|
244
|
+
corners[face_idx[1]],
|
|
245
|
+
corners[face_idx[2]],
|
|
246
|
+
corners[face_idx[3]],
|
|
247
|
+
color=color
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
def draw_sphere(self, pos: vector3d=vector3d.zero, radius: int=2, color: Color=Color.light_red, segments=32, rings=16):
|
|
251
|
+
self.renderer.render_sphere(center=pos, radius=radius, color=color, segments=segments, rings=rings)
|
|
252
|
+
|
|
253
|
+
def draw_cylinder(self, pos: vector3d=vector3d.zero, length: int=2, radius: int=.5, color: Color=Color.light_yellow, segments: int=32, rotation: vector3d=vector3d.zero):
|
|
254
|
+
self.renderer.render_cylinder(center=pos, height=length, radius=radius, color=color, segments=segments, rotation=rotation)
|
|
255
|
+
|
|
256
|
+
def attach(self, path):
|
|
257
|
+
script = Script(self, path, self.context)
|
|
258
|
+
script.init_instance()
|
|
259
|
+
self.scripts.append(script)
|
|
260
|
+
|
|
261
|
+
def init_scripts(self):
|
|
262
|
+
for script in self.scripts:
|
|
263
|
+
script.init_instance()
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from gamengine3d import Engine, Cuboid, vector3d, Light, Color # import all the classes
|
|
2
|
+
|
|
3
|
+
engine = Engine(500, 500, resizable=True, name="Basics Demo", background_color=Color.light_blue, ambient_light=0.4) # define the Engine object.
|
|
4
|
+
|
|
5
|
+
floor = Cuboid(pos=vector3d.zero, size=vector3d(10, .5, 10), color=Color.light_grey) # create the floor
|
|
6
|
+
|
|
7
|
+
engine.add_light(Light(position=vector3d(0, 4, .1), color=Color.white)) # create and add a light (x or z values of 0 don't create the light in this case, fixes are being made
|
|
8
|
+
|
|
9
|
+
engine.add_object(floor) # add the floor object
|
|
10
|
+
|
|
11
|
+
engine.run() # run the game
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from gamengine3d import Engine, vector3d, Color, Cuboid, Light
|
|
3
|
+
|
|
4
|
+
# Initialize engine
|
|
5
|
+
engine = Engine(
|
|
6
|
+
1200, 800,
|
|
7
|
+
background_color=Color.light_blue,
|
|
8
|
+
ambient_light=0.3,
|
|
9
|
+
name="Player Movement Demo"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
# Room setup
|
|
13
|
+
room_size = vector3d(5, 1.5, 2.5)
|
|
14
|
+
half_size = room_size / 2
|
|
15
|
+
room_color = Color.white
|
|
16
|
+
light_intensity = 0.3
|
|
17
|
+
|
|
18
|
+
# Camera positioning
|
|
19
|
+
engine.camera.move_to(vector3d(0.0, 1.4, 2.3))
|
|
20
|
+
|
|
21
|
+
# Floor
|
|
22
|
+
floor = Cuboid(
|
|
23
|
+
size=vector3d(room_size.x, 1, room_size.z),
|
|
24
|
+
color=Color.light_grey,
|
|
25
|
+
name="Floor",
|
|
26
|
+
pos=vector3d(0, 0.05 - 0.5)
|
|
27
|
+
)
|
|
28
|
+
engine.add_object(floor)
|
|
29
|
+
|
|
30
|
+
# Walls
|
|
31
|
+
left_wall = Cuboid(
|
|
32
|
+
pos=vector3d(-half_size.x, half_size.y, 0),
|
|
33
|
+
size=vector3d(0.1, room_size.y, room_size.z),
|
|
34
|
+
color=room_color,
|
|
35
|
+
name="Left"
|
|
36
|
+
)
|
|
37
|
+
engine.add_object(left_wall)
|
|
38
|
+
|
|
39
|
+
right_wall = Cuboid(
|
|
40
|
+
pos=vector3d(half_size.x, half_size.y, 0),
|
|
41
|
+
size=vector3d(0.1, room_size.y, room_size.z),
|
|
42
|
+
color=room_color,
|
|
43
|
+
name="Right"
|
|
44
|
+
)
|
|
45
|
+
engine.add_object(right_wall)
|
|
46
|
+
|
|
47
|
+
back_wall = Cuboid(
|
|
48
|
+
pos=vector3d(0, half_size.y, -half_size.z),
|
|
49
|
+
size=vector3d(room_size.x, room_size.y, 0.1),
|
|
50
|
+
color=room_color,
|
|
51
|
+
name="Back"
|
|
52
|
+
)
|
|
53
|
+
engine.add_object(back_wall)
|
|
54
|
+
|
|
55
|
+
# Player
|
|
56
|
+
player = Cuboid(
|
|
57
|
+
name="Player",
|
|
58
|
+
color=Color.light_red,
|
|
59
|
+
size=vector3d(0.2),
|
|
60
|
+
pos=vector3d(0, 1)
|
|
61
|
+
)
|
|
62
|
+
EXAMPLE_DIR = Path(__file__).parent
|
|
63
|
+
player_script = EXAMPLE_DIR / "scripts" / "player_movement.py" # get the player_movement script path
|
|
64
|
+
|
|
65
|
+
player.attach(player_script, engine.context) # attach movement script
|
|
66
|
+
# you can directly add a relative path like "scripts/player_movement.py"
|
|
67
|
+
|
|
68
|
+
engine.add_object(player)
|
|
69
|
+
|
|
70
|
+
# Lights
|
|
71
|
+
engine.add_light(Light(
|
|
72
|
+
vector3d(1/3 * room_size.x, room_size.y, 1),
|
|
73
|
+
Color.white,
|
|
74
|
+
light_intensity
|
|
75
|
+
))
|
|
76
|
+
engine.add_light(Light(
|
|
77
|
+
vector3d(-1/3 * room_size.x, room_size.y, 1),
|
|
78
|
+
Color.white,
|
|
79
|
+
light_intensity
|
|
80
|
+
))
|
|
81
|
+
|
|
82
|
+
# Run the demo
|
|
83
|
+
engine.run(dynamic_view=True)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from gamengine3d import *
|
|
2
|
+
|
|
3
|
+
class Cylinder:
|
|
4
|
+
def __init__(self, obj: Cylinder, context: Context):
|
|
5
|
+
self.obj = obj
|
|
6
|
+
self.context = context
|
|
7
|
+
self.color_idx = 0
|
|
8
|
+
self.obj.color = self.context.runtime_vars.cylinder_colors[0]
|
|
9
|
+
self.context.add_scheduled_caller(n_times=-1, delay=1, callback=self.switch_color)
|
|
10
|
+
self.context.on_key_held("w", self.draw_cylinder)
|
|
11
|
+
self.obj.visible = False
|
|
12
|
+
|
|
13
|
+
def update(self, dt):
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
def switch_color(self):
|
|
17
|
+
self.color_idx += 1
|
|
18
|
+
self.obj.color = self.context.runtime_vars.cylinder_colors[self.color_idx % self.context.runtime_vars.num_cylinder_colors]
|
|
19
|
+
|
|
20
|
+
def draw_cylinder(self):
|
|
21
|
+
self.context.renderer.render_cylinder(vector3d.zero, 2, .1, Color.blue, 32)
|