gamengine3d 1.0.0__tar.gz
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-1.0.0/LICENSE.txt +21 -0
- gamengine3d-1.0.0/PKG-INFO +78 -0
- gamengine3d-1.0.0/README.md +60 -0
- gamengine3d-1.0.0/gamengine3d/__init__.py +14 -0
- gamengine3d-1.0.0/gamengine3d/context.py +119 -0
- gamengine3d-1.0.0/gamengine3d/engine.py +249 -0
- gamengine3d-1.0.0/gamengine3d/game_objects.py +185 -0
- gamengine3d-1.0.0/gamengine3d/helper.py +649 -0
- gamengine3d-1.0.0/gamengine3d.egg-info/PKG-INFO +78 -0
- gamengine3d-1.0.0/gamengine3d.egg-info/SOURCES.txt +13 -0
- gamengine3d-1.0.0/gamengine3d.egg-info/dependency_links.txt +1 -0
- gamengine3d-1.0.0/gamengine3d.egg-info/requires.txt +4 -0
- gamengine3d-1.0.0/gamengine3d.egg-info/top_level.txt +1 -0
- gamengine3d-1.0.0/pyproject.toml +29 -0
- gamengine3d-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Samarth Javagal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: gamengine3d
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A Unity-like 3D engine in Python using pygame
|
|
5
|
+
Author-email: Samarth Javagal <samarthjavagal@gmail.com>
|
|
6
|
+
Classifier: Programming Language :: Python :: 3
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Topic :: Games/Entertainment
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.8
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE.txt
|
|
13
|
+
Requires-Dist: moderngl==5.12.0
|
|
14
|
+
Requires-Dist: numpy==2.3.3
|
|
15
|
+
Requires-Dist: pygame==2.6.1
|
|
16
|
+
Requires-Dist: pyrr==0.10.3
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
GamEngine3D is a Python 3D engine for creating interactive 3D scenes. This README focuses on basic usage.
|
|
20
|
+
|
|
21
|
+
Quick Start
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from gamengine3d import *
|
|
25
|
+
|
|
26
|
+
# 1. Initialize the engine
|
|
27
|
+
engine = Engine(1200, 800, background_color=Color.light_blue)
|
|
28
|
+
|
|
29
|
+
# 2. Create objects
|
|
30
|
+
floor = Cuboid(size=vector3d(5, 0.1, 5), color=Color.light_grey, name="Floor")
|
|
31
|
+
player = Cuboid(name="Player", color=Color.light_red, size=vector3d(0.2), pos=vector3d(0, 1))
|
|
32
|
+
|
|
33
|
+
# 3. Add objects to the engine
|
|
34
|
+
engine.add_object(floor)
|
|
35
|
+
engine.add_object(player)
|
|
36
|
+
|
|
37
|
+
# 4. Attach behavior to objects
|
|
38
|
+
player.attach("player_controller.py", engine.context)
|
|
39
|
+
|
|
40
|
+
# 5. Run the engine
|
|
41
|
+
engine.run(dynamic_view=True)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Notes
|
|
45
|
+
|
|
46
|
+
Engine: Manages rendering, lights, and the main loop.
|
|
47
|
+
|
|
48
|
+
Objects: Add cubes, spheres, or custom shapes with Cuboid(), Sphere(), etc.
|
|
49
|
+
|
|
50
|
+
Attach Scripts: Use .attach("script.py", context) to add movement, input, or custom logic.
|
|
51
|
+
|
|
52
|
+
Run: engine.run() starts the simulation; dynamic_view=True enables interactive camera movement.
|
|
53
|
+
|
|
54
|
+
Example: player_controller.py
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from gamengine3d import *
|
|
58
|
+
import math
|
|
59
|
+
|
|
60
|
+
class PlayerController:
|
|
61
|
+
def __init__(self, obj, context):
|
|
62
|
+
self.obj = obj
|
|
63
|
+
self.context = context
|
|
64
|
+
self.speed = 1
|
|
65
|
+
self.context.on_key_held("up", callback=self.move_forward, dt=True)
|
|
66
|
+
|
|
67
|
+
def update(self, dt):
|
|
68
|
+
pass # called every frame
|
|
69
|
+
|
|
70
|
+
def move_forward(self, dt):
|
|
71
|
+
# simple forward movement
|
|
72
|
+
yaw = math.radians(self.obj.rotation.z)
|
|
73
|
+
forward = vector3d(-math.sin(yaw), 0, math.cos(yaw))
|
|
74
|
+
self.obj.pos += forward * self.speed * dt
|
|
75
|
+
```
|
|
76
|
+
This minimal PlayerController shows how to attach a script to an object and respond to input.
|
|
77
|
+
|
|
78
|
+
The 2d version of ```gamengine3d``` is out under ``gamengine2d``
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
GamEngine3D is a Python 3D engine for creating interactive 3D scenes. This README focuses on basic usage.
|
|
2
|
+
|
|
3
|
+
Quick Start
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
from gamengine3d import *
|
|
7
|
+
|
|
8
|
+
# 1. Initialize the engine
|
|
9
|
+
engine = Engine(1200, 800, background_color=Color.light_blue)
|
|
10
|
+
|
|
11
|
+
# 2. Create objects
|
|
12
|
+
floor = Cuboid(size=vector3d(5, 0.1, 5), color=Color.light_grey, name="Floor")
|
|
13
|
+
player = Cuboid(name="Player", color=Color.light_red, size=vector3d(0.2), pos=vector3d(0, 1))
|
|
14
|
+
|
|
15
|
+
# 3. Add objects to the engine
|
|
16
|
+
engine.add_object(floor)
|
|
17
|
+
engine.add_object(player)
|
|
18
|
+
|
|
19
|
+
# 4. Attach behavior to objects
|
|
20
|
+
player.attach("player_controller.py", engine.context)
|
|
21
|
+
|
|
22
|
+
# 5. Run the engine
|
|
23
|
+
engine.run(dynamic_view=True)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Notes
|
|
27
|
+
|
|
28
|
+
Engine: Manages rendering, lights, and the main loop.
|
|
29
|
+
|
|
30
|
+
Objects: Add cubes, spheres, or custom shapes with Cuboid(), Sphere(), etc.
|
|
31
|
+
|
|
32
|
+
Attach Scripts: Use .attach("script.py", context) to add movement, input, or custom logic.
|
|
33
|
+
|
|
34
|
+
Run: engine.run() starts the simulation; dynamic_view=True enables interactive camera movement.
|
|
35
|
+
|
|
36
|
+
Example: player_controller.py
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from gamengine3d import *
|
|
40
|
+
import math
|
|
41
|
+
|
|
42
|
+
class PlayerController:
|
|
43
|
+
def __init__(self, obj, context):
|
|
44
|
+
self.obj = obj
|
|
45
|
+
self.context = context
|
|
46
|
+
self.speed = 1
|
|
47
|
+
self.context.on_key_held("up", callback=self.move_forward, dt=True)
|
|
48
|
+
|
|
49
|
+
def update(self, dt):
|
|
50
|
+
pass # called every frame
|
|
51
|
+
|
|
52
|
+
def move_forward(self, dt):
|
|
53
|
+
# simple forward movement
|
|
54
|
+
yaw = math.radians(self.obj.rotation.z)
|
|
55
|
+
forward = vector3d(-math.sin(yaw), 0, math.cos(yaw))
|
|
56
|
+
self.obj.pos += forward * self.speed * dt
|
|
57
|
+
```
|
|
58
|
+
This minimal PlayerController shows how to attach a script to an object and respond to input.
|
|
59
|
+
|
|
60
|
+
The 2d version of ```gamengine3d``` is out under ``gamengine2d``
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from .game_objects import GameObject
|
|
2
|
+
from .helper import Color, vector3d, Light, Camera, Renderer
|
|
3
|
+
from pygame.time import Clock
|
|
4
|
+
|
|
5
|
+
class Functions:
|
|
6
|
+
def __init__(self):
|
|
7
|
+
self.draw_cube = lambda pos=vector3d.zero, size=vector3d(3), color=Color.light_blue: None
|
|
8
|
+
self.draw_sphere = lambda pos=vector3d.zero, radius=2, color=Color.light_red, segments=32, rings=16: None
|
|
9
|
+
self.draw_cylinder = lambda pos=vector3d.zero, length=2, radius=.5, color=Color.light_yellow, segments=32: None
|
|
10
|
+
self.add_light = lambda light: None
|
|
11
|
+
self.add_object = lambda obj: None
|
|
12
|
+
self.is_colliding = lambda name1, name2: None
|
|
13
|
+
self.get_game_object = lambda name: None
|
|
14
|
+
self.remove_object = lambda name: None
|
|
15
|
+
self.remove_light = lambda name: None
|
|
16
|
+
|
|
17
|
+
class KeyCallback:
|
|
18
|
+
def __init__(self, key, event_type, callback, args=None, dt=False):
|
|
19
|
+
self.callback = callback
|
|
20
|
+
self.key = key
|
|
21
|
+
self.event_type = event_type
|
|
22
|
+
self.dt = dt
|
|
23
|
+
if not args:
|
|
24
|
+
args = []
|
|
25
|
+
self.args = args
|
|
26
|
+
|
|
27
|
+
def update(self, pressed_keys, held_keys, released_keys, dt):
|
|
28
|
+
if self.event_type == "pressed":
|
|
29
|
+
if self.key in pressed_keys:
|
|
30
|
+
if self.dt:
|
|
31
|
+
self.callback(dt, *self.args)
|
|
32
|
+
else:
|
|
33
|
+
self.callback(*self.args)
|
|
34
|
+
|
|
35
|
+
elif self.event_type == "held":
|
|
36
|
+
if self.key in held_keys:
|
|
37
|
+
if self.dt:
|
|
38
|
+
self.callback(dt, *self.args)
|
|
39
|
+
else:
|
|
40
|
+
self.callback(*self.args)
|
|
41
|
+
|
|
42
|
+
elif self.event_type == "released":
|
|
43
|
+
if self.key in released_keys:
|
|
44
|
+
if self.dt:
|
|
45
|
+
self.callback(dt, *self.args)
|
|
46
|
+
else:
|
|
47
|
+
self.callback(*self.args)
|
|
48
|
+
|
|
49
|
+
class KeyCallbackVar:
|
|
50
|
+
def __init__(self, key, event_type, obj, attr, value):
|
|
51
|
+
self.key = key
|
|
52
|
+
self.event_type = event_type
|
|
53
|
+
self.obj = obj
|
|
54
|
+
self.attr = attr
|
|
55
|
+
self.value = value
|
|
56
|
+
|
|
57
|
+
def update(self, pressed_keys, held_keys, releaed_keys):
|
|
58
|
+
if self.event_type == "pressed":
|
|
59
|
+
if self.key in pressed_keys:
|
|
60
|
+
setattr(self.obj, self.attr, self.value)
|
|
61
|
+
|
|
62
|
+
elif self.event_type == "held":
|
|
63
|
+
if self.key in held_keys:
|
|
64
|
+
setattr(self.obj, self.attr, self.value)
|
|
65
|
+
|
|
66
|
+
elif self.event_type == "released":
|
|
67
|
+
if self.key in releaed_keys:
|
|
68
|
+
setattr(self.obj, self.attr, self.value)
|
|
69
|
+
|
|
70
|
+
class Context:
|
|
71
|
+
def __init__(self):
|
|
72
|
+
self.functions: Functions = Functions()
|
|
73
|
+
self.ambient_light: float = .2
|
|
74
|
+
self.lights: list[Light] = []
|
|
75
|
+
self.game_objects: list[GameObject] = []
|
|
76
|
+
self.mouse_sensitivity: float = 0.3
|
|
77
|
+
self.pan_sensitivity: float = 0.005
|
|
78
|
+
self.fps: int | float = 0
|
|
79
|
+
self.engine = None
|
|
80
|
+
self.camera: Camera | None = None
|
|
81
|
+
self.clock: Clock | None = None
|
|
82
|
+
self.renderer: Renderer | None = None
|
|
83
|
+
self.keys_held: list[str] = []
|
|
84
|
+
self.keys_pressed: list[str] = []
|
|
85
|
+
self.keys_released: list[str] = []
|
|
86
|
+
self.key_callbacks: list[KeyCallback] = []
|
|
87
|
+
self.key_callback_vars: list[KeyCallbackVar] = []
|
|
88
|
+
self.exit = False
|
|
89
|
+
|
|
90
|
+
def on_key_press(self, key, callback, args=None, dt=False):
|
|
91
|
+
if args is None:
|
|
92
|
+
args = []
|
|
93
|
+
self.key_callbacks.append(KeyCallback(key, "pressed", callback, args, dt))
|
|
94
|
+
|
|
95
|
+
def on_key_held(self, key, callback, args=None, dt=False):
|
|
96
|
+
if args is None:
|
|
97
|
+
args = []
|
|
98
|
+
self.key_callbacks.append(KeyCallback(key, "held", callback, args, dt))
|
|
99
|
+
|
|
100
|
+
def on_key_released(self, key, callback, args=None, dt=False):
|
|
101
|
+
if args is None:
|
|
102
|
+
args = []
|
|
103
|
+
self.key_callbacks.append(KeyCallback(key, "released", callback, args, dt))
|
|
104
|
+
|
|
105
|
+
def on_key_press_var(self, key, obj, attr, value):
|
|
106
|
+
self.key_callback_vars.append(KeyCallbackVar(key, "pressed", obj, attr, value))
|
|
107
|
+
|
|
108
|
+
def on_key_held_var(self, key, obj, attr, value):
|
|
109
|
+
self.key_callback_vars.append(KeyCallbackVar(key, "held", obj, attr, value))
|
|
110
|
+
|
|
111
|
+
def on_key_released_var(self, key, obj, attr, value):
|
|
112
|
+
self.key_callback_vars.append(KeyCallbackVar(key, "released", obj, attr, value))
|
|
113
|
+
|
|
114
|
+
def update(self, dt):
|
|
115
|
+
for callback in self.key_callbacks:
|
|
116
|
+
callback.update(self.keys_pressed, self.keys_held, self.keys_released, dt)
|
|
117
|
+
|
|
118
|
+
for callback_var in self.key_callback_vars:
|
|
119
|
+
callback_var.update(self.keys_pressed, self.keys_held, self.keys_released, dt)
|
|
@@ -0,0 +1,249 @@
|
|
|
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
|
+
|
|
50
|
+
self.context.game_objects = self.gameobjects
|
|
51
|
+
self.context.engine = self
|
|
52
|
+
self.context.camera = self.camera
|
|
53
|
+
self.context.renderer = self.renderer
|
|
54
|
+
self.context.clock = self.clock
|
|
55
|
+
|
|
56
|
+
self.context.lights = self.renderer.lights
|
|
57
|
+
|
|
58
|
+
def run(self, fps=60, dynamic_view=True):
|
|
59
|
+
running = True
|
|
60
|
+
self.context.fps = fps
|
|
61
|
+
keys_held = []
|
|
62
|
+
self.context.keys_held = keys_held
|
|
63
|
+
self.init_scripts()
|
|
64
|
+
|
|
65
|
+
for script in self.scripts:
|
|
66
|
+
script.run("on_start")
|
|
67
|
+
|
|
68
|
+
while running:
|
|
69
|
+
keys_pressed = []
|
|
70
|
+
keys_released = []
|
|
71
|
+
dt = self.clock.get_time() / 1000
|
|
72
|
+
self.mouse_sensitivity = self.context.mouse_sensitivity
|
|
73
|
+
self.pan_sensitivity = self.context.pan_sensitivity
|
|
74
|
+
self.context.fps = self.clock.get_fps()
|
|
75
|
+
|
|
76
|
+
for event in pygame.event.get():
|
|
77
|
+
if event.type == pygame.QUIT:
|
|
78
|
+
running = False
|
|
79
|
+
|
|
80
|
+
if self.resizable and event.type == pygame.VIDEORESIZE:
|
|
81
|
+
self.width, self.height = event.w, event.h
|
|
82
|
+
self.renderer.resize(self.width, self.height)
|
|
83
|
+
self.camera.aspect_ratio = self.width / self.height
|
|
84
|
+
|
|
85
|
+
if dynamic_view:
|
|
86
|
+
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
87
|
+
if event.button in (1, 3): # Left or right
|
|
88
|
+
self.last_mouse_pos = pygame.mouse.get_pos()
|
|
89
|
+
self.active_button = event.button
|
|
90
|
+
elif event.type == pygame.MOUSEBUTTONUP:
|
|
91
|
+
if event.button in (1, 3):
|
|
92
|
+
self.last_mouse_pos = None
|
|
93
|
+
self.active_button = None
|
|
94
|
+
|
|
95
|
+
elif event.type == pygame.MOUSEMOTION:
|
|
96
|
+
if self.last_mouse_pos is not None:
|
|
97
|
+
x, y = pygame.mouse.get_pos()
|
|
98
|
+
dx = x - self.last_mouse_pos[0]
|
|
99
|
+
dy = y - self.last_mouse_pos[1]
|
|
100
|
+
self.last_mouse_pos = (x, y)
|
|
101
|
+
|
|
102
|
+
if self.active_button == 3:
|
|
103
|
+
self.camera.rotate_around_target(-dx * self.mouse_sensitivity, dy * self.mouse_sensitivity)
|
|
104
|
+
|
|
105
|
+
elif self.active_button == 1:
|
|
106
|
+
self._pan_camera(dx, dy)
|
|
107
|
+
|
|
108
|
+
elif event.type == pygame.MOUSEWHEEL:
|
|
109
|
+
self.camera.zoom(event.y * 0.5)
|
|
110
|
+
|
|
111
|
+
if event.type == pygame.KEYDOWN:
|
|
112
|
+
key_name = pygame.key.name(event.key)
|
|
113
|
+
keys_pressed.append(key_name)
|
|
114
|
+
keys_held.append(key_name)
|
|
115
|
+
|
|
116
|
+
elif event.type == pygame.KEYUP:
|
|
117
|
+
key_name = pygame.key.name(event.key)
|
|
118
|
+
keys_held.remove(key_name)
|
|
119
|
+
keys_released.append(key_name)
|
|
120
|
+
|
|
121
|
+
self.context.keys_pressed = keys_pressed
|
|
122
|
+
self.context.keys_released = keys_released
|
|
123
|
+
self.context.update(dt)
|
|
124
|
+
|
|
125
|
+
self.renderer.clear(self.background_color)
|
|
126
|
+
self.camera.update_renderer(self.renderer)
|
|
127
|
+
|
|
128
|
+
self.renderer.ambient = self.context.ambient_light
|
|
129
|
+
|
|
130
|
+
for script in self.scripts:
|
|
131
|
+
script.update(dt)
|
|
132
|
+
|
|
133
|
+
for obj in self.gameobjects:
|
|
134
|
+
obj.update(dt)
|
|
135
|
+
obj.draw(self.renderer)
|
|
136
|
+
|
|
137
|
+
if self.context.exit:
|
|
138
|
+
running = False
|
|
139
|
+
|
|
140
|
+
self.renderer.swap()
|
|
141
|
+
self.clock.tick(fps)
|
|
142
|
+
|
|
143
|
+
for script in self.scripts:
|
|
144
|
+
script.run("on_exit")
|
|
145
|
+
self.renderer.quit()
|
|
146
|
+
|
|
147
|
+
def _pan_camera(self, dx, dy):
|
|
148
|
+
forward = (self.camera.target - self.camera.position).normalized
|
|
149
|
+
right = forward.cross(self.camera.up).normalized
|
|
150
|
+
up = self.camera.up.normalized
|
|
151
|
+
|
|
152
|
+
pan = (right * -dx + up * dy) * self.pan_sensitivity * self.camera.radius
|
|
153
|
+
self.camera.target += pan
|
|
154
|
+
self.camera.position += pan
|
|
155
|
+
self.camera._update_position()
|
|
156
|
+
|
|
157
|
+
def add_light(self, light):
|
|
158
|
+
self.renderer.add_light(light)
|
|
159
|
+
if light.show:
|
|
160
|
+
self.add_object(Sphere(pos=light.position, color=light.color, radius=.1))
|
|
161
|
+
|
|
162
|
+
def add_object(self, obj: GameObject):
|
|
163
|
+
self.gameobjects.append(obj)
|
|
164
|
+
|
|
165
|
+
def remove_object(self, name):
|
|
166
|
+
obj = self.get_object(name)
|
|
167
|
+
self.gameobjects.remove(obj)
|
|
168
|
+
|
|
169
|
+
def remove_light(self, name):
|
|
170
|
+
for light in self.renderer.lights:
|
|
171
|
+
if light.name == name:
|
|
172
|
+
self.renderer.lights.remove(light)
|
|
173
|
+
return
|
|
174
|
+
|
|
175
|
+
raise EngineError(f"No Light Object Named {name}")
|
|
176
|
+
|
|
177
|
+
def get_object(self, name):
|
|
178
|
+
for obj in self.gameobjects:
|
|
179
|
+
if obj.name == name:
|
|
180
|
+
return obj
|
|
181
|
+
|
|
182
|
+
raise EngineError(f"Game Object with name {name} not found")
|
|
183
|
+
|
|
184
|
+
def is_colliding(self, name1, name2):
|
|
185
|
+
obj1 = self.get_object(name1)
|
|
186
|
+
obj2 = self.get_object(name2)
|
|
187
|
+
|
|
188
|
+
if not (isinstance(obj1, Cuboid) and isinstance(obj2, Cuboid)):
|
|
189
|
+
raise NotImplementedError(f"Cylinder And Sphere Collision Not Implemented")
|
|
190
|
+
|
|
191
|
+
corners = obj1.get_corners()
|
|
192
|
+
for corner in corners:
|
|
193
|
+
if obj2.is_point_in(corner):
|
|
194
|
+
return True
|
|
195
|
+
|
|
196
|
+
return False
|
|
197
|
+
|
|
198
|
+
def draw_cuboid(self, pos: vector3d=vector3d.zero, size: vector3d=vector3d(2), rotation: vector3d=vector3d.zero, color: Color = Color.light_blue):
|
|
199
|
+
faces = [
|
|
200
|
+
[0, 1, 3, 2],
|
|
201
|
+
[4, 6, 7, 5],
|
|
202
|
+
[0, 4, 5, 1],
|
|
203
|
+
[2, 3, 7, 6],
|
|
204
|
+
[0, 2, 6, 4],
|
|
205
|
+
[1, 5, 7, 3],
|
|
206
|
+
]
|
|
207
|
+
|
|
208
|
+
half_size = size / 2
|
|
209
|
+
|
|
210
|
+
rot_matrix = Matrix44.from_eulers((
|
|
211
|
+
math.radians(rotation.x),
|
|
212
|
+
math.radians(rotation.y),
|
|
213
|
+
math.radians(rotation.z)
|
|
214
|
+
))
|
|
215
|
+
|
|
216
|
+
corners = []
|
|
217
|
+
for dx, dy, dz in product([-1, 1], repeat=3):
|
|
218
|
+
local = vector3d(dx * half_size.x, dy * half_size.y, dz * half_size.z)
|
|
219
|
+
|
|
220
|
+
rotated = vector3d(
|
|
221
|
+
*(rot_matrix @ [local.x, local.y, local.z, 1.0])[:3]
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
world = pos + rotated
|
|
225
|
+
corners.append(world)
|
|
226
|
+
|
|
227
|
+
for face_idx in faces:
|
|
228
|
+
self.renderer.render_quad(
|
|
229
|
+
corners[face_idx[0]],
|
|
230
|
+
corners[face_idx[1]],
|
|
231
|
+
corners[face_idx[2]],
|
|
232
|
+
corners[face_idx[3]],
|
|
233
|
+
color=color
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
def draw_sphere(self, pos: vector3d=vector3d.zero, radius: int=2, color: Color=Color.light_red, segments=32, rings=16):
|
|
237
|
+
self.renderer.render_sphere(center=pos, radius=radius, color=color, segments=segments, rings=rings)
|
|
238
|
+
|
|
239
|
+
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):
|
|
240
|
+
self.renderer.render_cylinder(center=pos, height=length, radius=radius, color=color, segments=segments, rotation=rotation)
|
|
241
|
+
|
|
242
|
+
def attach(self, path):
|
|
243
|
+
script = Script(self, path, self.context)
|
|
244
|
+
script.init_instance()
|
|
245
|
+
self.scripts.append(script)
|
|
246
|
+
|
|
247
|
+
def init_scripts(self):
|
|
248
|
+
for script in self.scripts:
|
|
249
|
+
script.init_instance()
|