Add entity class.

This commit is contained in:
Roz K 2022-12-31 21:02:46 +01:00
parent af9365c6e0
commit acc2e4cd28
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
4 changed files with 45 additions and 24 deletions

View File

@ -40,12 +40,12 @@ class Batch:
def __del__(self):
destroy_batch(self._batch)
def append(self, flags, mesh, params):
def spawn(self, model, *params):
assert len(params) == len(self.params)
index = self.size
assert index < self.max_size
self.flags[index] = flags | INSTANCE_FLAG_SPAWNED
self.meshes[index] = mesh
self.flags[index] = model.flags | INSTANCE_FLAG_SPAWNED
self.meshes[index] = model.mesh
for self_params, param in zip(self.params, params):
self_params[index] = param
self.size += 1

20
game/entity.py Normal file
View File

@ -0,0 +1,20 @@
# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class Entity:
__slots__ = 'index'
def __init__(self, batch, model, *params):
self.index = batch.spawn(model, *params)

View File

@ -31,6 +31,7 @@ from game.camera import Camera
from game.environment import Environment
from game.inputs import InputFloat
from game.batch import Batch
from game.entity import Entity
from game.scene import SceneNode, TextureNode, ShaderNode, InputNode, DrawNode, FuncNode
from game import sea
@ -42,6 +43,16 @@ proj_far_z = 3000.0
sun_direction = math.vec3_normalize((1.0, 0.0, 0.5))
sun_power = 1.0
class TestEntity(Entity):
__slots__ = 'translation', 'orientation', 'spawn_translation', 'spawn_orientation'
def __init__(self, batch, model, translation, orientation):
Entity.__init__(self, batch, model, translation, orientation)
self.translation = batch.translation[self.index]
self.orientation = batch.orientation[self.index]
self.spawn_translation = translation
self.spawn_orientation = orientation
def update_camera(time, mouse, camera, environment):
camera_yaw = mouse.drag[0] * 0.001
camera_pitch = mouse.drag[1] * 0.001 + pi * 0.25
@ -49,13 +60,13 @@ def update_camera(time, mouse, camera, environment):
camera.set_view(camera_yaw, camera_pitch, camera_distance)
environment.from_sun(camera.view, sun_direction, sun_power)
def update_tests(time, blob_translation, blob_spawn_translation, cube_translation, cube_spawn_translation, cube_orientation, clouds_orientation):
def update_tests(time, blob, cube, clouds):
rotation = mat3()
mat3_rotation(rotation, vec3_up, (time.current * 0.21) % tau)
mat3_mul_vec3(blob_translation, rotation, blob_spawn_translation)
mat3_mul_vec3(cube_translation, rotation, cube_spawn_translation)
mat3_rotation(cube_orientation, vec3_up, (time.current * 0.43) % tau)
mat3_rotation(clouds_orientation, vec3_up, (time.current * -0.037) % tau)
mat3_mul_vec3(blob.translation, rotation, blob.spawn_translation)
mat3_mul_vec3(cube.translation, rotation, cube.spawn_translation)
mat3_rotation(cube.orientation, vec3_up, (time.current * 0.43) % tau)
mat3_rotation(clouds.orientation, vec3_up, (time.current * -0.037) % tau)
def update_sea(time, camera, sea_phase):
camera.to_km()
@ -125,7 +136,7 @@ def create_scene(keyboard, mouse):
model = mud_model
else:
model = rock_model
model.spawn(tiles_batch, vec3(float(((mx - 128) * 8) + 4), float(((127 - my) * 8) + 4), 0.0))
tiles_batch.spawn(model, vec3(float(((mx - 128) * 8) + 4), float(((127 - my) * 8) + 4), 0.0))
tests_texture = Texture(*archive.get_texture('tests'))
tests_vertices = Vertices(*archive.get_vertices('tests'))
@ -135,19 +146,12 @@ def create_scene(keyboard, mouse):
tests_batch = Batch(tests_vertices, 3, 3,
translation = PARAM_FORMAT_VEC3_FLOAT,
orientation = PARAM_FORMAT_MAT3_INT10 | PARAM_FORMAT_NORMALIZE)
blob_spawn_translation = vec3(-100.0, -500.0, 0.0)
blob_forward = math.vec3_normalize((sun_direction[0], sun_direction[1], 0.0))
blob_right = math.vec3_cross(blob_forward, math.vec3_up)
blob_spawn_orientation = mat3(vec3(*blob_right), vec3(*blob_forward), vec3_up)
blob_id = blob_model.spawn(tests_batch, blob_spawn_translation, blob_spawn_orientation)
blob_translation = tests_batch.translation[blob_id]
cube_spawn_translation = vec3(100.0, -500.0, 0.0)
cube_id = cube_model.spawn(tests_batch, cube_spawn_translation, mat3_identity)
cube_translation = tests_batch.translation[cube_id]
cube_orientation = tests_batch.orientation[cube_id]
clouds_id = clouds_model.spawn(tests_batch, vec3(0.0, 0.0, 32.0), mat3_identity)
clouds_orientation = tests_batch.orientation[clouds_id]
blob_orientation = mat3(vec3(*blob_right), vec3(*blob_forward), vec3_up)
blob = TestEntity(tests_batch, blob_model, vec3(-100.0, -500.0, 0.0), blob_orientation)
cube = TestEntity(tests_batch, cube_model, vec3(100.0, -500.0, 0.0), mat3_identity)
clouds = TestEntity(tests_batch, clouds_model, vec3(0.0, 0.0, 32.0), mat3_identity)
sea_polar_textures = sea.load_polar_textures(('data/sea_bump1.png', 'data/sea_bump2.png'))
sea_detail_texture = sea.load_detail_texture('data/sea_bump.png')
@ -162,7 +166,7 @@ def create_scene(keyboard, mouse):
DrawNode(tiles_batch)
)
),
FuncNode(update_tests, (blob_translation, blob_spawn_translation, cube_translation, cube_spawn_translation, cube_orientation, clouds_orientation)),
FuncNode(update_tests, (blob, cube, clouds)),
TextureNode((tests_texture, heightmap, normalmap),
ShaderNode(tests_shader,
InputNode(tests_shader, camera),

View File

@ -190,9 +190,6 @@ class ModelData:
_write_string(file, self.name)
_write_struct(file, 'II', (self.flags, self.mesh))
def spawn(self, batch, *params):
return batch.append(self.flags, self.mesh, params)
class Archive:
__slots__ = 'textures_db', 'vertices_db', 'models_db'