From acc2e4cd28721735a49a57734ae2f716ade847c4 Mon Sep 17 00:00:00 2001 From: Roz K Date: Sat, 31 Dec 2022 21:02:46 +0100 Subject: [PATCH] Add entity class. --- game/batch.py | 6 +++--- game/entity.py | 20 ++++++++++++++++++++ game/game.py | 40 ++++++++++++++++++++++------------------ game/resources.py | 3 --- 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 game/entity.py diff --git a/game/batch.py b/game/batch.py index b06b0ab..da8ef06 100644 --- a/game/batch.py +++ b/game/batch.py @@ -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 diff --git a/game/entity.py b/game/entity.py new file mode 100644 index 0000000..dcf49c4 --- /dev/null +++ b/game/entity.py @@ -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 . + +class Entity: + __slots__ = 'index' + + def __init__(self, batch, model, *params): + self.index = batch.spawn(model, *params) diff --git a/game/game.py b/game/game.py index 1f3c1fd..7f466d6 100644 --- a/game/game.py +++ b/game/game.py @@ -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), diff --git a/game/resources.py b/game/resources.py index 8d92bf6..28c9dea 100644 --- a/game/resources.py +++ b/game/resources.py @@ -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'