ENEMY_WEAPON_SYSTEM_UPDATE.md 4.0 KB

Enemy Character Weapon System Update

Overview

Updated the EnemyCharacterData system to use WeaponItem assets instead of WeaponType enums, providing better integration with the existing weapon system and centralizing weapon configuration.

Key Changes

EnemyCharacterData.cs

BEFORE:

  • Used WeaponType preferredWeaponType enum field
  • Had separate Weapon weaponPrefab and GameObject arrowPrefab fields
  • Generated hardcoded weapon creation logic based on weapon type

AFTER:

  • Uses WeaponItem preferredWeapon asset reference
  • Removed redundant prefab fields (moved to WeaponItem)
  • Simplified weapon creation using preferredWeapon.CreateWeaponInstance()

WeaponItem.cs

ENHANCED:

  • Added GameObject weaponPrefab field for 3D weapon models
  • Already had GameObject arrowPrefab field for ranged weapons
  • Centralized all weapon configuration in one asset

Benefits

1. Type Safety

  • Enemies can only use weapons that exist as assets
  • No more hardcoded weapon types in enemy data
  • Inspector shows drag-drop fields for weapon selection

2. Centralized Configuration

  • All weapon properties (damage, range, speed, prefabs) in WeaponItem
  • No duplication of weapon data across different systems
  • Easy to create new weapons and assign to enemies

3. Better Integration

  • Uses same weapon system as player characters
  • Consistent weapon creation across all character types
  • Leverages existing WeaponItem.CreateWeaponInstance() method

4. Simplified Maintenance

  • Adding new weapon types only requires creating WeaponItem assets
  • No need to update enemy creation code for new weapons
  • Weapon prefabs managed in one place

Usage Workflow

Creating Enemy Weapons

  1. Create WeaponItem asset: Create → RPG → Items → Weapon
  2. Configure weapon stats (damage, range, speed, etc.)
  3. Set weaponClassName (e.g., "SimpleSword", "SimpleBow")
  4. Assign weaponPrefab for 3D model (optional)
  5. Assign arrowPrefab for ranged weapons (if applicable)

Assigning to Enemies

  1. Open existing EnemyCharacterData asset
  2. Drag WeaponItem asset into Preferred Weapon field
  3. Generated enemy characters will use this weapon automatically

Fallback Behavior

  • If no WeaponItem assigned, enemies get basic sword
  • Ensures all enemies have functional weapons
  • Prevents null reference errors

Generated Character Code

The updated system generates cleaner character classes:

protected override Weapon CreateDirectWeapon()
{
    // Create weapon from WeaponItem asset
    if (preferredWeapon != null)
    {
        spawnedWeapon = preferredWeapon.CreateWeaponInstance(this.transform);
        if (spawnedWeapon != null)
        {
            spawnedWeapon.SetWielder(this);
            return spawnedWeapon;
        }
    }
    
    // Fallback: Create basic sword if no weapon item assigned
    GameObject weaponObject = new GameObject("Basic Weapon");
    weaponObject.transform.SetParent(this.transform, false);
    spawnedWeapon = weaponObject.AddComponent<SimpleSword>();
    spawnedWeapon.SetWielder(this);
    return spawnedWeapon;
}

Migration Guide

For Existing Enemy Assets

  1. Open each EnemyCharacterData asset
  2. Create appropriate WeaponItem for the current preferredWeaponType
  3. Assign the WeaponItem to preferredWeapon field
  4. The old WeaponType field will be automatically removed

For New Enemies

  1. Create WeaponItem assets first
  2. Then create EnemyCharacterData and assign weapons
  3. Use the enhanced enemy creation workflow

Technical Integration

With Travel Events

  • CombatTravelEvent continues to use EnemyCharacterData assets
  • Weapons are automatically configured when enemies spawn
  • No changes needed to travel event system

With Battle System

  • Generated enemy characters work seamlessly with existing battle system
  • Weapon creation happens during character initialization
  • All weapon types supported through WeaponItem system

This update provides a much cleaner, more maintainable approach to enemy weapon configuration while maintaining full backward compatibility through the fallback system.