load_particle_properties(): updated
it now includes sub folders so you may have //src/parts/mods/modname1/partname/ //src/parts/mods/modname1/customthing //src/parts/mods/modname/partname //src/parts/coreparts/partname/ removed some old sim logic that was accidentally being reused.
This commit is contained in:
parent
64e45910a2
commit
9c5a93ac3a
@ -52,7 +52,6 @@ def load_particle_properties():
|
|||||||
with open(file_path, 'r') as f:
|
with open(file_path, 'r') as f:
|
||||||
mod_data = json.load(f)
|
mod_data = json.load(f)
|
||||||
relative_path = os.path.relpath(file_path, parts_path)
|
relative_path = os.path.relpath(file_path, parts_path)
|
||||||
print(f"Loading particles from: {relative_path}")
|
|
||||||
particle_data.update(mod_data)
|
particle_data.update(mod_data)
|
||||||
print(f"Loaded {len(mod_data)} particles from {relative_path}")
|
print(f"Loaded {len(mod_data)} particles from {relative_path}")
|
||||||
except (FileNotFoundError, json.JSONDecodeError) as e:
|
except (FileNotFoundError, json.JSONDecodeError) as e:
|
||||||
|
|||||||
@ -331,55 +331,6 @@ class Simulation:
|
|||||||
return fx, fy
|
return fx, fy
|
||||||
|
|
||||||
|
|
||||||
def _process_particle_batch(self, batch, dt):
|
|
||||||
updates = []
|
|
||||||
new_active = set()
|
|
||||||
|
|
||||||
# Filter out dormant particles from the batch
|
|
||||||
active_batch = [pos for pos in batch if pos not in self.dormant_particles]
|
|
||||||
|
|
||||||
for x, y in active_batch:
|
|
||||||
particle = self.particles[x][y]
|
|
||||||
if not particle:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if particle.particle_type == 'wall':
|
|
||||||
new_active.add((x, y))
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Check if particle should become dormant
|
|
||||||
if self._check_dormant_state(x, y, particle):
|
|
||||||
new_active.add((x, y))
|
|
||||||
continue
|
|
||||||
|
|
||||||
# physics calculations
|
|
||||||
fx, fy = self.calculate_forces(particle, x, y)
|
|
||||||
# Use max() to ensure mass is never zero
|
|
||||||
mass = max(particle.mass, 0.001)
|
|
||||||
particle.velocity[0] += (fx / mass) * dt
|
|
||||||
particle.velocity[1] += (fy / mass) * dt
|
|
||||||
|
|
||||||
new_x = int(x + particle.velocity[0] * dt)
|
|
||||||
new_y = int(y + particle.velocity[1] * dt)
|
|
||||||
|
|
||||||
if 0 <= new_x < self.width and 0 <= new_y < self.height:
|
|
||||||
if self.particles[new_x][new_y] is None:
|
|
||||||
updates.append((x, y, new_x, new_y, particle))
|
|
||||||
new_active.add((new_x, new_y))
|
|
||||||
# Wake up neighboring dormant particles
|
|
||||||
self._wake_neighbors(new_x, new_y)
|
|
||||||
else:
|
|
||||||
new_active.add((x, y))
|
|
||||||
|
|
||||||
# Apply updates and return new active set
|
|
||||||
for old_x, old_y, new_x, new_y, particle in updates:
|
|
||||||
self.particles[old_x][old_y] = None
|
|
||||||
self.particles[new_x][new_y] = particle
|
|
||||||
particle.position = (new_x, new_y)
|
|
||||||
|
|
||||||
return new_active
|
|
||||||
|
|
||||||
|
|
||||||
def _get_quick_neighbors(self, x, y):
|
def _get_quick_neighbors(self, x, y):
|
||||||
"""Quick neighbor lookup without full spatial grid"""
|
"""Quick neighbor lookup without full spatial grid"""
|
||||||
return [(x+dx, y+dy) for dx, dy in [(-1,0), (1,0), (0,-1), (0,1)]]
|
return [(x+dx, y+dy) for dx, dy in [(-1,0), (1,0), (0,-1), (0,1)]]
|
||||||
@ -795,16 +746,8 @@ class Simulation:
|
|||||||
|
|
||||||
def simulate_step(self, dt, engine_settings):
|
def simulate_step(self, dt, engine_settings):
|
||||||
"""Run simulation step with spatial grid updates"""
|
"""Run simulation step with spatial grid updates"""
|
||||||
|
|
||||||
self.update_spatial_grid()
|
self.update_spatial_grid()
|
||||||
|
|
||||||
active_list = list(self.active_particles)
|
|
||||||
batch_size = 1024
|
|
||||||
|
|
||||||
for i in range(0, len(active_list), batch_size):
|
|
||||||
batch = active_list[i:i + batch_size]
|
|
||||||
self._process_particle_batch(batch, dt)
|
|
||||||
|
|
||||||
# Update particle positions and physics
|
# Update particle positions and physics
|
||||||
self.apply_gravity(dt)
|
self.apply_gravity(dt)
|
||||||
self.apply_physics(dt, engine_settings)
|
self.apply_physics(dt, engine_settings)
|
||||||
|
|||||||
@ -28,23 +28,6 @@ This is for the future physics engine until i figure out a better method used fo
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def update_simulation(sim, dt, engine_settings):
|
|
||||||
"""Update simulation state"""
|
|
||||||
sim.simulate_step(dt, engine_settings)
|
|
||||||
|
|
||||||
def render_frame(rendering, sim, mouse_pos):
|
|
||||||
"""Render all visual elements"""
|
|
||||||
# Draw particles
|
|
||||||
rendering.draw_particles(sim.particles, sim.active_particles, sim.particle_size, rendering.particle_colors)
|
|
||||||
|
|
||||||
# Draw UI elements
|
|
||||||
rendering.draw_buttons()
|
|
||||||
rendering.draw_brush_size_slider(sim.brush_size)
|
|
||||||
|
|
||||||
# Draw brush cursor
|
|
||||||
mouse_x, mouse_y = mouse_pos
|
|
||||||
rendering.render_brush_cursor(mouse_x, mouse_y, sim.brush_size * sim.particle_size)
|
|
||||||
|
|
||||||
def handle_input(event, sim, rendering, settings_visible, zoom_active, zoom_locked, zoom_pos):
|
def handle_input(event, sim, rendering, settings_visible, zoom_active, zoom_locked, zoom_pos):
|
||||||
"""Handle all input events"""
|
"""Handle all input events"""
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
@ -55,6 +38,7 @@ def handle_input(event, sim, rendering, settings_visible, zoom_active, zoom_lock
|
|||||||
return handle_key_press(event, rendering, sim)
|
return handle_key_press(event, rendering, sim)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def handle_mouse_down(event, sim, rendering, settings_visible, zoom_active):
|
def handle_mouse_down(event, sim, rendering, settings_visible, zoom_active):
|
||||||
"""Handle mouse button down events"""
|
"""Handle mouse button down events"""
|
||||||
mouse_pos = pygame.mouse.get_pos()
|
mouse_pos = pygame.mouse.get_pos()
|
||||||
@ -76,6 +60,7 @@ def handle_mouse_down(event, sim, rendering, settings_visible, zoom_active):
|
|||||||
return {'mouse_down_middle': True}
|
return {'mouse_down_middle': True}
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def handle_left_click(mouse_pos, sim, rendering, settings_visible, in_settings_area, zoom_active):
|
def handle_left_click(mouse_pos, sim, rendering, settings_visible, in_settings_area, zoom_active):
|
||||||
"""Handle left click interactions"""
|
"""Handle left click interactions"""
|
||||||
result = {'mouse_down_left': False, 'settings_visible': settings_visible, 'over_button': False}
|
result = {'mouse_down_left': False, 'settings_visible': settings_visible, 'over_button': False}
|
||||||
@ -104,6 +89,7 @@ def handle_left_click(mouse_pos, sim, rendering, settings_visible, in_settings_a
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def handle_settings_click(mouse_pos, sim):
|
def handle_settings_click(mouse_pos, sim):
|
||||||
"""Handle clicks in settings menu"""
|
"""Handle clicks in settings menu"""
|
||||||
settings_menu_y = 100
|
settings_menu_y = 100
|
||||||
@ -113,6 +99,7 @@ def handle_settings_click(mouse_pos, sim):
|
|||||||
setting_name = list(engine_settings.keys())[setting_index]
|
setting_name = list(engine_settings.keys())[setting_index]
|
||||||
engine_settings[setting_name] = not engine_settings[setting_name]
|
engine_settings[setting_name] = not engine_settings[setting_name]
|
||||||
|
|
||||||
|
|
||||||
def handle_ui_click(mouse_pos, sim, rendering):
|
def handle_ui_click(mouse_pos, sim, rendering):
|
||||||
"""Handle clicks on UI elements"""
|
"""Handle clicks on UI elements"""
|
||||||
for category, button in rendering.category_buttons.items():
|
for category, button in rendering.category_buttons.items():
|
||||||
@ -131,6 +118,7 @@ def handle_ui_click(mouse_pos, sim, rendering):
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def handle_mouse_up(event):
|
def handle_mouse_up(event):
|
||||||
"""Handle mouse button up events"""
|
"""Handle mouse button up events"""
|
||||||
if event.button == 1:
|
if event.button == 1:
|
||||||
@ -141,6 +129,7 @@ def handle_mouse_up(event):
|
|||||||
return {'mouse_down_middle': False}
|
return {'mouse_down_middle': False}
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def handle_key_press(event, rendering, sim):
|
def handle_key_press(event, rendering, sim):
|
||||||
"""Handle keyboard press events"""
|
"""Handle keyboard press events"""
|
||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
@ -156,6 +145,7 @@ def handle_key_press(event, rendering, sim):
|
|||||||
return {'zoom_active': True, 'zoom_locked': False, 'zoom_pos': pygame.mouse.get_pos()}
|
return {'zoom_active': True, 'zoom_locked': False, 'zoom_pos': pygame.mouse.get_pos()}
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pygame.init()
|
pygame.init()
|
||||||
clock = pygame.time.Clock()
|
clock = pygame.time.Clock()
|
||||||
@ -296,20 +286,22 @@ def main():
|
|||||||
x, y = mouse_pos
|
x, y = mouse_pos
|
||||||
sim.create_particle(x, y)
|
sim.create_particle(x, y)
|
||||||
|
|
||||||
|
# Handle zoom window
|
||||||
|
if zoom_active or zoom_locked:
|
||||||
|
current_zoom_pos = zoom_pos if zoom_locked else mouse_pos
|
||||||
|
zoom_surface = rendering.draw_zoom_window(sim.particles, sim.particle_size,
|
||||||
|
rendering.particle_colors, current_zoom_pos)
|
||||||
|
zoom_x = 80 if current_zoom_pos[0] > width/2 else width - 110
|
||||||
|
zoom_y = 80 if current_zoom_pos[1] > height/2 else height - 110
|
||||||
|
screen.blit(zoom_surface, (zoom_x, zoom_y))
|
||||||
|
|
||||||
# Draw everything in correct order
|
# Draw everything in correct order
|
||||||
rendering.draw_particles(sim.particles, sim.active_particles, sim.particle_size, rendering.particle_colors)
|
rendering.draw_particles(sim.particles, sim.active_particles, sim.particle_size, rendering.particle_colors)
|
||||||
rendering.draw_buttons()
|
rendering.draw_buttons()
|
||||||
rendering.draw_brush_size_slider(sim.brush_size)
|
rendering.draw_brush_size_slider(sim.brush_size)
|
||||||
rendering.render_brush_cursor(mouse_pos[0], mouse_pos[1], sim.brush_size * sim.particle_size)
|
rendering.render_brush_cursor(mouse_pos[0], mouse_pos[1], sim.brush_size * sim.particle_size)
|
||||||
|
|
||||||
# Handle zoom window
|
|
||||||
if zoom_active or zoom_locked:
|
|
||||||
current_zoom_pos = zoom_pos if zoom_locked else mouse_pos
|
|
||||||
zoom_surface = rendering.draw_zoom_window(sim.particles, sim.particle_size,
|
|
||||||
rendering.particle_colors, current_zoom_pos)
|
|
||||||
zoom_x = 10 if current_zoom_pos[0] > width/2 else width - 110
|
|
||||||
zoom_y = 10 if current_zoom_pos[1] > height/2 else height - 110
|
|
||||||
screen.blit(zoom_surface, (zoom_x, zoom_y))
|
|
||||||
|
|
||||||
# Draw settings and debug overlay last
|
# Draw settings and debug overlay last
|
||||||
if settings_visible:
|
if settings_visible:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user