cirnogodot/Scripts/Editor/CreateWeapon3D.gd

181 lines
5.9 KiB
GDScript

@tool
extends EditorScript
# EditorScript to create a new 3D weapon with all required resources
# Usage: Open this file in the Godot editor, then go to File > Run
func _run():
# Configuration - modify these values before running
var weapon_name = "New Weapon"
var weapon_item_key = "NEW_WEAPON"
var weapon_short_name = "NW-1"
var weapon_description = "A new weapon for testing"
# Default bullet resource to use
var default_bullet_path = "res://Resources/Bullets/simple_ice_bullet.tres"
# Resource paths
var weapon_resource_path = "res://Resources/Weapons/" + weapon_item_key + ".tres"
var item_resource_path = "res://Resources/Items/" + weapon_item_key + "_Item.tres"
var items_database_path = "res://Resources/ItemsDatabase.tres"
print("=== Starting Weapon Creation ===")
print("Weapon Name: ", weapon_name)
print("Item Key: ", weapon_item_key)
# Step 1: Create WeaponResource
if create_weapon_resource(weapon_resource_path, weapon_name, weapon_item_key, default_bullet_path):
print("✓ Created WeaponResource at: ", weapon_resource_path)
else:
print("✗ Failed to create WeaponResource")
return
# Step 2: Create LootItem resource
if create_loot_item_resource(item_resource_path, weapon_name, weapon_short_name,
weapon_description, weapon_item_key, weapon_resource_path):
print("✓ Created LootItem at: ", item_resource_path)
else:
print("✗ Failed to create LootItem")
return
# Step 3: Add to ItemsDatabase
if add_to_items_database(items_database_path, item_resource_path):
print("✓ Added to ItemsDatabase")
else:
print("✗ Failed to add to ItemsDatabase")
return
print("=== Weapon Creation Complete ===")
print("Remember to:")
print("1. Set appropriate weapon stats in ", weapon_resource_path)
print("2. Add a sprite texture to the LootItem in ", item_resource_path)
print("3. Save all modified resources")
func create_weapon_resource(path: String, weapon_name: String, item_key: String, bullet_path: String) -> bool:
# Check if file already exists
if ResourceLoader.exists(path):
print("Warning: WeaponResource already exists at ", path)
return false
# Load the WeaponResource script
var weapon_script = load("res://Scripts/Resources/WeaponResource.cs")
if weapon_script == null:
print("Error: Could not load WeaponResource.cs script")
return false
# Load the bullet resource
var bullet_resource = load(bullet_path)
if bullet_resource == null:
print("Error: Could not load bullet resource from ", bullet_path)
return false
# Create new WeaponResource
var weapon_resource = Resource.new()
weapon_resource.set_script(weapon_script)
# Set properties (using reflection-like approach in GDScript)
weapon_resource.set("Name", weapon_name)
weapon_resource.set("BulletData", bullet_resource)
weapon_resource.set("ItemKey", item_key)
weapon_resource.set("AmmoKey", "BATTERY")
weapon_resource.set("Priority", 10)
weapon_resource.set("AmmoPerShot", 1)
weapon_resource.set("RateOfFire", 0.2)
weapon_resource.set("BulletCapacity", 50)
weapon_resource.set("ReloadTime", 1.0)
weapon_resource.set("InfiniteAmmo", false)
weapon_resource.set("AutoReload", true)
weapon_resource.set("RechargeTime", 0.5)
weapon_resource.set("RechargeAmount", 5)
weapon_resource.set("BulletsPerShot", 1)
weapon_resource.set("SpreadAngle", 0.0)
weapon_resource.set("RandomSpread", 0.0)
# Save the resource
var err = ResourceSaver.save(weapon_resource, path)
if err != OK:
print("Error saving WeaponResource: ", err)
return false
return true
func create_loot_item_resource(path: String, item_name: String, short_name: String,
description: String, item_key: String,
weapon_resource_path: String) -> bool:
# Check if file already exists
if ResourceLoader.exists(path):
print("Warning: LootItem already exists at ", path)
return false
# Load the LootItem script
var loot_item_script = load("res://Scripts/Resources/LootItem.cs")
if loot_item_script == null:
print("Error: Could not load LootItem.cs script")
return false
# Load the weapon resource we just created
var weapon_resource = load(weapon_resource_path)
if weapon_resource == null:
print("Error: Could not load weapon resource from ", weapon_resource_path)
return false
# Create new LootItem
var loot_item = Resource.new()
loot_item.set_script(loot_item_script)
# Set properties
loot_item.set("ItemName", item_name)
loot_item.set("ShortName", short_name)
loot_item.set("ItemDescription", description)
loot_item.set("ItemKey", item_key)
loot_item.set("Item", 13) # ItemTypes.Weapon = 13 (0-indexed)
loot_item.set("WeaponData3D", weapon_resource)
loot_item.set("Amount", 1)
loot_item.set("Max", 1)
loot_item.set("Selectable", true)
loot_item.set("DropScenePath3D", "res://Scenes/Items/GenericItem3D.tscn")
# Save the resource
var err = ResourceSaver.save(loot_item, path)
if err != OK:
print("Error saving LootItem: ", err)
return false
return true
func add_to_items_database(database_path: String, item_resource_path: String) -> bool:
# Load the ItemsDatabase
var items_database = load(database_path)
if items_database == null:
print("Error: Could not load ItemsDatabase from ", database_path)
return false
# Load the item we just created
var new_item = load(item_resource_path)
if new_item == null:
print("Error: Could not load item from ", item_resource_path)
return false
# Get current items array
var loot_items = items_database.get("LootItems")
if loot_items == null:
print("Error: Could not get LootItems array from database")
return false
# Check if item already exists (by comparing resource paths)
for item in loot_items:
if item.resource_path == item_resource_path:
print("Warning: Item already exists in database")
return false
# Append the new item
loot_items.append(new_item)
items_database.set("LootItems", loot_items)
# Save the database
var err = ResourceSaver.save(items_database, database_path)
if err != OK:
print("Error saving ItemsDatabase: ", err)
return false
return true