Make a modular BaseGame class that all Games are based on to decrease bloat, add Diaunay simulation and fix some stuff in README

This commit is contained in:
csd4ni3l
2025-09-28 21:20:15 +02:00
parent 195a429ff5
commit 311d3607f2
14 changed files with 242 additions and 407 deletions

View File

@@ -1,49 +1,24 @@
import pyglet, arcade, arcade.gui, os, json, numpy as np
from game.chladni_plate_simulator.shader import create_shader
from game.base import BaseGame
from utils.preload import button_texture, button_hovered_texture
from utils.constants import button_style
class Game(arcade.gui.UIView):
class Game(BaseGame):
def __init__(self, pypresence_client):
super().__init__()
self.pypresence_client = pypresence_client
self.pypresence_client.update(state="Playing a simulator", details="Chladni Plate Simulator")
self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.settings_box = self.anchor.add(arcade.gui.UIBoxLayout(align="center", size_hint=(0.2, 1)).with_background(color=arcade.color.GRAY), anchor_x="right", anchor_y="bottom")
self.settings_label = self.settings_box.add(arcade.gui.UILabel(text="Settings", font_size=24))
if os.path.exists("data.json"):
with open("data.json", "r") as file:
self.settings = json.load(file)
else:
self.settings = {}
if not "chladni_plate_simulator" in self.settings:
self.settings["chladni_plate_simulator"] = {
super().__init__(pypresence_client, "Chladni Plate Simulator", "chladni_plate_simulator", {
"k": 0.1
}
})
self.sources = np.empty((0, 2), dtype=np.float32)
self.dragged_source = None
self.needs_redraw = False
def add_setting(self, text, min_value, max_value, step, settings_key):
label = self.settings_box.add(arcade.gui.UILabel(text.format(value=self.settings["chladni_plate_simulator"][settings_key])))
slider = self.settings_box.add(arcade.gui.UISlider(value=self.settings["chladni_plate_simulator"][settings_key], min_value=min_value, max_value=max_value, step=step))
slider._render_steps = lambda surface: None
slider.on_change = lambda event, label=label: self.change_value(label, text, settings_key, event.new_value)
def change_value(self, label, text, settings_key, value):
label.text = text.format(value=value)
self.settings["chladni_plate_simulator"][settings_key] = value
super().change_value(label, text, settings_key, value)
self.needs_redraw = True
@@ -86,8 +61,6 @@ class Game(arcade.gui.UIView):
def on_show_view(self):
super().on_show_view()
self.settings_box.add(arcade.gui.UISpace(height=self.window.height / 75))
self.add_setting("k: {value}", 0.02, 0.5, 0.01, "k")
self.add_source_button = self.settings_box.add(arcade.gui.UITextureButton(text="Add source", texture=button_texture, texture_hovered=button_hovered_texture, style=button_style, width=self.window.width * 0.2))
@@ -96,14 +69,9 @@ class Game(arcade.gui.UIView):
self.setup()
def on_key_press(self, symbol, modifiers):
if symbol == arcade.key.ESCAPE:
self.shader_program.delete()
self.sources_ssbo.delete()
from menus.main import Main
self.window.show_view(Main(self.pypresence_client))
elif symbol == arcade.key.C:
super().on_key_press(symbol, modifiers)
if symbol == arcade.key.C:
del self.sources
self.sources = np.empty((0, 2), dtype=np.float32)