Added option to customize enemy image path, and customize weapons

This commit is contained in:
csd4ni3l
2025-06-18 22:35:11 +02:00
parent ab11d92e3a
commit 613ff7eef7
8 changed files with 145 additions and 27 deletions

View File

@@ -1,12 +1,15 @@
from game.inventory import Inventory
from game.player import Player
from game.enemy import Enemy
from utils.constants import weapons, min_enemy_y, max_enemy_y
from utils.constants import min_enemy_y, max_enemy_y
from ursina import *
from ursina.shaders import lit_with_shadows_shader
import os, json
enemy_file_names = os.listdir("assets/graphics/enemy")
from pathlib import Path
import os, json
class Game():
def __init__(self, pypresence_client) -> None:
@@ -20,16 +23,22 @@ class Game():
pypresence_client.update(state='Training Aim', details=f'Hits: 0/0 Accuracy: 0%')
with open("settings.json", "r") as file:
self.settings_dict = json.load(file)
self.enemy_image_dir = self.settings_dict.get("enemy_image_directory", "assets/graphics/enemy")
self.enemy_file_names = [file_name for file_name in os.listdir(self.enemy_image_dir) if file_name.split(".")[1] in ["png", "jpg", "JPG"]]
self.ground = Entity(model='plane', collider='box', scale=64, texture='grass', texture_scale=(4,4), shader=lit_with_shadows_shader)
self.info_label = Text("Score: 0 Hits: 0/0 Accuracy: 0%", parent=camera.ui, position=(-0.1, 0.475))
self.inventory = Inventory(slots=len(weapons))
self.inventory = Inventory(slots=len(self.settings_dict.get("weapons")))
for n, weapon in enumerate(weapons):
self.inventory.append(weapons[weapon]["image"], weapon, n)
for n, weapon in enumerate(self.settings_dict.get("weapons")):
self.inventory.append(self.settings_dict.get("weapons")[weapon]["image"], weapon, n)
self.player = Player(self.high_score, self.info_label, self.inventory, pypresence_client)
self.player = Player(self.settings_dict, self.high_score, self.info_label, self.inventory, pypresence_client)
self.shootables_parent = Entity()
mouse.traverse_target = self.shootables_parent
@@ -47,10 +56,18 @@ class Game():
self.sky = Sky()
self.sky.update = self.update
def summon_enemy(self):
if not len(self.enemies) >= 50:
self.enemies.append(Enemy(self.player, self.shootables_parent, self.player.x + (random.randint(12, 24) * random.choice([1, -1])), random.randint(min_enemy_y, max_enemy_y), self.player.z + (random.randint(12, 24) * random.choice([1, -1])), "assets/graphics/enemy/" + random.choice(enemy_file_names)))
self.enemies.append(
Enemy(
self.player,
self.shootables_parent,
self.player.x + (random.randint(12, 24) * random.choice([1, -1])),
random.randint(min_enemy_y, max_enemy_y),
self.player.z + (random.randint(12, 24) * random.choice([1, -1])),
Texture(Path(os.path.join(self.enemy_image_dir, random.choice(self.enemy_file_names))))
)
)
def update(self):
Sky.update(self.sky)

View File

@@ -1,4 +1,5 @@
from ursina import Entity, Quad, color, camera, held_keys, destroy
from ursina import Entity, Quad, color, camera, destroy, Texture
from pathlib import Path
class Inventory():
def __init__(self, x=0, y=4.5, slot_width=0.1, slot_height=0.1, slots=5):
@@ -35,7 +36,7 @@ class Inventory():
self.slot_grid[slot] = Entity(
parent = camera.ui,
model = Quad(radius=.015),
texture = item,
texture = Texture(Path(item)),
scale = (self.slot_width, self.slot_height),
position = (-.3 + slot * (self.slot_width * 1.25), -.4),
z=-1,

View File

@@ -1,34 +1,40 @@
from ursina import *
from ursina.shaders import lit_with_shadows_shader
from ursina.prefabs.ursfx import ursfx
from ursina.prefabs.first_person_controller import FirstPersonController
from ursina import *
from utils.constants import weapons, max_enemy_speed
from ursina.shaders import lit_with_shadows_shader
from utils.preload import death_sound
from utils.constants import max_enemy_speed, weapons
import json
from pathlib import Path
class Player(FirstPersonController):
def __init__(self, high_score, info_label, inventory, pypresence_client) -> None:
def __init__(self, settings_dict, high_score, info_label, inventory, pypresence_client) -> None:
super().__init__(model='cube', z=16, color=color.orange, origin_y=-.5, speed=8, collider='box', gravity=True, shader=lit_with_shadows_shader)
self.collider = BoxCollider(self, Vec3(0,1,0), Vec3(1,2,1))
self.gun = Entity(model='cube', parent=camera, position=(.5,-.25,.25), scale=(.3,.2,1), origin_z=-.5, color=color.red, on_cooldown=True, shader=lit_with_shadows_shader)
invoke(setattr, self.gun, 'on_cooldown', False, delay=1)
self.gun.muzzle_flash = Entity(parent=self.gun, z=1, world_scale=.5, model='quad', color=color.yellow, enabled=False, shader=lit_with_shadows_shader)
self.info_label = info_label
self.inventory = inventory
self.pypresence_client = pypresence_client
self.high_score = high_score
self.settings_dict = settings_dict
self.last_presence_update = time.perf_counter()
self.shots_fired = 0
self.shots_hit = 0
self.accuracy = 0
self.score = 0
self.weapon_attack_speed = 0
self.weapon_dmg = 0
self.gun = Entity(model='cube', texture=Texture(Path(self.settings_dict.get("weapons", weapons)[self.inventory.slot_names[self.inventory.current_slot]]["image"])), parent=camera, position=(.5,-.25,.25), scale=(.3,.2,1), origin_z=-.5, on_cooldown=True, shader=lit_with_shadows_shader)
invoke(setattr, self.gun, 'on_cooldown', False, delay=1)
self.gun.muzzle_flash = Entity(parent=self.gun, z=1, world_scale=.5, model='quad', color=color.yellow, enabled=False, shader=lit_with_shadows_shader)
with open("settings.json", "r") as file:
self.settings_dict = json.load(file)
@@ -43,8 +49,9 @@ class Player(FirstPersonController):
self.info_label.text = f"High Score: {self.high_score} Score: {self.score} Hits: {self.shots_fired}/{self.shots_hit} Accuracy: {round(self.accuracy, 2)}%"
weapon_name = self.inventory.slot_names[self.inventory.current_slot]
self.weapon_attack_speed = weapons[weapon_name]["atk_speed"]
self.weapon_dmg = weapons[weapon_name]["dmg"]
self.gun.texture = Texture(Path(self.settings_dict.get("weapons", weapons)[weapon_name]["image"]))
self.weapon_attack_speed = self.settings_dict.get("weapons", weapons)[weapon_name]["atk_speed"]
self.weapon_dmg = self.settings_dict.get("weapons", weapons)[weapon_name]["dmg"]
if self.score > self.high_score:
self.high_score = self.score

View File

@@ -8,7 +8,7 @@ class MenuButton(Button):
super().__init__(text, scale=(.25, .075), highlight_color=color.azure, **kwargs)
for key, value in kwargs.items():
setattr(self, key ,value)
setattr(self, key, value)
class Main():
def __init__(self, pypresence_client=None) -> None:

View File

@@ -2,8 +2,9 @@ from ursina import *
from ursina.prefabs.slider import ThinSlider
from ursina.prefabs.dropdown_menu import DropdownMenuButton
from ursina.prefabs.button_group import ButtonGroup
import pypresence, json, copy
from utils.utils import FakePyPresence, Dropdown
from utils.utils import FakePyPresence, Dropdown, FileManager
from utils.constants import discord_presence_id, settings, settings_start_category
from utils.preload import music_sound
@@ -27,6 +28,12 @@ class Settings:
self.ui += [self.main, self.back, self.category_group]
self.dmg_inputs = {}
self.atk_speed_inputs = {}
self.weapon_img_paths = {}
self.img_path_buttons = {}
self.save_buttons = {}
self.show(self.category)
def show(self, category):
@@ -36,6 +43,9 @@ class Settings:
if category == "Credits":
self.credits()
return
elif category == "Weapons":
self.weapons()
return
y = .2
@@ -57,7 +67,7 @@ class Settings:
slider.on_value_changed = lambda slider=slider, n=name: self.update(n, int(slider.value))
self.ui.append(slider)
else:
elif type == "option":
menu_buttons = []
for opt in info['options']:
menu_button = DropdownMenuButton(opt)
@@ -71,11 +81,34 @@ class Settings:
dropdown_menu.position = (.2, y)
self.ui.append(dropdown_menu)
elif type == "directory_select":
directory_select_button = Button(text=f"Select Directory ({val})", scale_x=1.1, scale_y=0.1, text_size=.7, position = (.33, y))
directory_select_button.on_click = lambda btn=directory_select_button, name=name: self.select_directory(btn, name)
self.ui.append(directory_select_button)
y -= .08
self.apply_button = Button('Apply', parent=camera.ui, color=color.green, scale=(.15, .08), position=(.6, -.4), on_click=self.apply_changes)
self.ui.append(self.apply_button)
def directory_selected(self, btn, name, value):
btn.text = f"Select Directory ({value})"
self.update(name, value)
def select_directory(self, btn, name):
self.dir_file_manager = FileManager(return_folders=True, z=-1)
self.dir_file_manager.on_submit = lambda value, btn=btn, name=name: self.directory_selected(btn, name, str(value[0]))
def image_file_selected(self, btn, name, value):
btn.text = f"Select File ({value})"
self.weapon_img_paths[name] = value
def select_image_file(self, btn, name):
self.file_manager = FileManager(z=-1)
self.file_manager.on_submit = lambda value, btn=btn, name=name: self.image_file_selected(btn, name, str(value[0]))
def dropdown_update(self, n, dropdown_menu, btn):
dropdown_menu.text = btn.text
@@ -120,6 +153,7 @@ class Settings:
music_sound.stop()
json.dump(self.data, open('settings.json', 'w'), indent=4)
self.hide()
self.__init__(self.rpc)
@@ -168,3 +202,38 @@ class Settings:
self.credits_label = Text(text=text, parent=camera.ui, position=(0, 0), origin=(0, 0), scale=font_size, color=color.white)
self.credits_label.type = 'credits_text'
self.ui.append(self.credits_label)
def save_weapon(self, name):
dmg, attack_speed, image = self.dmg_inputs[name].text, self.atk_speed_inputs[name].text, self.weapon_img_paths[name]
self.edits["weapons"] = self.edits.get("weapons", settings["Weapons"]["default"])
self.edits["weapons"][name] = {"dmg": float(dmg), "atk_speed": float(attack_speed), "image": image}
self.apply_changes()
def weapons(self):
y = .2
for weapon_name, weapon_dict in self.data.get("weapons", settings["Weapons"]["default"]).items():
dmg, atk_speed, image = weapon_dict["dmg"], weapon_dict["atk_speed"], weapon_dict["image"]
self.ui.append(Text(weapon_name, parent=camera.ui, position=(-.8, y), scale=1.2))
self.ui.append(Text("DMG: ", parent=camera.ui, position=(-.6, y), scale=1.2))
self.dmg_inputs[weapon_name] = InputField(default_value=str(round(dmg, 2)), parent=camera.ui, position=(-.45, y - .01), scale_x=0.125, scale_y=.05)
self.ui.append(self.dmg_inputs[weapon_name])
self.ui.append(Text("Attack Speed: ", parent=camera.ui, position=(-.35, y), scale=1.2))
self.atk_speed_inputs[weapon_name] = InputField(default_value=str(round(atk_speed, 2)), parent=camera.ui, position=(-0.075, y - .01), scale_x=0.125, scale_y=.05)
self.ui.append(self.atk_speed_inputs[weapon_name])
self.ui.append(Text("Image Path: ", parent=camera.ui, position=(0.05, y), scale=1.2))
self.img_path_buttons[weapon_name] = Button(text=f"Select File ({image})", scale_x=.3, scale_y=0.05, text_size=.5, position=(0.4, y - .01))
self.img_path_buttons[weapon_name].on_click = lambda name=weapon_name, btn=self.img_path_buttons[weapon_name]: self.select_image_file(btn, name)
self.ui.append(self.img_path_buttons[weapon_name])
self.save_buttons[weapon_name] = Button(text="Save", scale_x=.1, scale_y=0.05, text_size=.7, position=(0.7, y - .01))
self.save_buttons[weapon_name].on_click = lambda name=weapon_name: self.save_weapon(name)
self.ui.append(self.save_buttons[weapon_name])
y -= 0.08

1
run.py
View File

@@ -33,6 +33,7 @@ if not args["fullscreen"]:
args["size"] = list(map(int, settings['resolution'].split('x')))
app = Ursina(title="Aim Trainer", development_mode=False, **args)
window.editor_ui.enabled = True
window.fps_counter.enabled = True

View File

@@ -18,6 +18,10 @@ weapons = {
discord_presence_id = 1380237183352311838
settings = {
"Gameplay": {
"Enemy Image Directory": {"type": "directory_select", "config_key": "enemy_image_directory", "default": "assets/graphics/enemy"}
},
"Weapons": {"default": weapons},
"Graphics": {
"Window Mode": {"type": "option", "options": ["Windowed", "Fullscreen", "Borderless"], "config_key": "window_mode", "default": "Windowed"},
"Resolution": {"type": "option", "options": ["1366x768", "1440x900", "1600x900", "1920x1080", "2560x1440", "3840x2160"], "config_key": "resolution"},
@@ -33,7 +37,7 @@ settings = {
"Miscellaneous": {
"Discord RPC": {"type": "bool", "config_key": "discord_rpc", "default": True},
},
"Credits": {}
"Credits": None
}
settings_start_category = "Graphics"
settings_start_category = "Gameplay"

View File

@@ -1,5 +1,6 @@
from panda3d.core import GraphicsPipeSelection
from ursina.prefabs.dropdown_menu import DropdownMenu
from ursina.prefabs.file_browser import FileBrowser
def get_closest_resolution():
allowed_resolutions = [(1366, 768), (1440, 900), (1600,900), (1920,1080), (2560,1440), (3840,2160)]
@@ -44,6 +45,24 @@ class Dropdown(DropdownMenu):
else:
self.close()
class FileManager(FileBrowser):
def open(self, path=None):
if not self.selection:
return
if not self.return_folders:
if self.selection[0].is_dir():
self.path = self.selection[0]
return
elif not self.selection[0].is_dir():
return
if hasattr(self, 'on_submit'):
self.on_submit(self.selection)
self.close()
class FakePyPresence():
def __init__(self):
...