118 lines
3.7 KiB
Python
118 lines
3.7 KiB
Python
import math
|
|
|
|
import pygame
|
|
import time
|
|
import sys
|
|
import random
|
|
|
|
from world.world import World, Position, Rotation
|
|
from world.objects import FoodObject, TestVelocityObject, DefaultCell
|
|
from world.simulation_interface import Camera
|
|
|
|
from config.constants import *
|
|
|
|
from core.input_handler import InputHandler
|
|
from core.renderer import Renderer
|
|
|
|
from ui.hud import HUD
|
|
|
|
# Initialize Pygame
|
|
pygame.init()
|
|
|
|
|
|
def setup(world: World):
|
|
if FOOD_SPAWNING:
|
|
world.add_object(FoodObject(Position(x=random.randint(-100, 100), y=random.randint(-100, 100))))
|
|
|
|
for i in range(100):
|
|
world.add_object(DefaultCell(Position(x=random.randint(-100, 100),y=random.randint(-100, 100)), Rotation(angle=0)))
|
|
|
|
return world
|
|
|
|
|
|
def main():
|
|
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), vsync=1)
|
|
pygame.display.set_caption("Dynamic Abstraction System Testing")
|
|
clock = pygame.time.Clock()
|
|
camera = Camera(SCREEN_WIDTH, SCREEN_HEIGHT, RENDER_BUFFER)
|
|
|
|
last_tick_time = time.perf_counter() # Tracks the last tick time
|
|
last_tps_time = time.perf_counter() # Tracks the last TPS calculation time
|
|
tick_counter = 0 # Counts ticks executed
|
|
actual_tps = 0 # Stores the calculated TPS
|
|
total_ticks = 0 # Total ticks executed
|
|
|
|
# Initialize world
|
|
world = World(CELL_SIZE, (CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT))
|
|
|
|
# sets seed to 67 >_<
|
|
random.seed(0)
|
|
|
|
world = setup(world)
|
|
|
|
input_handler = InputHandler(camera, world)
|
|
renderer = Renderer(screen)
|
|
hud = HUD()
|
|
|
|
running = True
|
|
while running:
|
|
deltatime = clock.get_time() / 1000.0 # Convert milliseconds to seconds
|
|
tick_interval = 1.0 / input_handler.tps # Time per tick
|
|
|
|
# Handle events
|
|
running = input_handler.handle_events(pygame.event.get())
|
|
|
|
if not input_handler.is_paused:
|
|
# Tick logic (runs every tick interval)
|
|
current_time = time.perf_counter()
|
|
while current_time - last_tick_time >= tick_interval:
|
|
last_tick_time += tick_interval
|
|
tick_counter += 1
|
|
total_ticks += 1
|
|
|
|
# ensure selected objects are still valid or have not changed position, if so, reselect them
|
|
input_handler.update_selected_objects()
|
|
|
|
world.tick_all()
|
|
|
|
# Calculate TPS every second
|
|
if current_time - last_tps_time >= 1.0:
|
|
actual_tps = tick_counter
|
|
tick_counter = 0
|
|
last_tps_time += 1.0
|
|
else:
|
|
last_tick_time = time.perf_counter()
|
|
last_tps_time = time.perf_counter()
|
|
|
|
# Get pressed keys for smooth movement
|
|
keys = pygame.key.get_pressed()
|
|
input_handler.update_camera(keys, deltatime)
|
|
|
|
renderer.clear_screen()
|
|
renderer.draw_grid(camera, input_handler.show_grid)
|
|
renderer.render_world(world, camera)
|
|
|
|
renderer.render_interaction_radius(world, camera, input_handler.selected_objects, input_handler.show_interaction_radius)
|
|
|
|
renderer.render_selection_rectangle(input_handler.get_selection_rect())
|
|
renderer.render_selected_objects_outline(input_handler.selected_objects, camera)
|
|
|
|
hud.render_mouse_position(screen, camera)
|
|
hud.render_fps(screen, clock)
|
|
hud.render_tps(screen, actual_tps)
|
|
hud.render_tick_count(screen, total_ticks)
|
|
hud.render_selected_objects_info(screen, input_handler.selected_objects)
|
|
hud.render_legend(screen, input_handler.show_legend)
|
|
hud.render_pause_indicator(screen, input_handler.is_paused)
|
|
|
|
# Update display
|
|
pygame.display.flip()
|
|
clock.tick(MAX_FPS)
|
|
|
|
pygame.quit()
|
|
sys.exit()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|