# Enemy Prefab Pattern

This manual explains how enemy prefabs are built in this project.

Current enemy prefabs:

```text
Assets/Prefabs/Characters/Monster.prefab
Assets/Prefabs/Characters/Witch.prefab
```

## Core Enemy Structure

Typical enemy:

```text
EnemyRoot
├── AttackHitbox
└── FirePoint optional
```

Monster:

```text
Monster
└── AttackHitbox
```

Witch:

```text
Witch
├── AttackHitbox
└── FirePoint
```

## Required Root Components

Enemy root should usually have:

```text
Transform
SpriteRenderer
Animator
CapsuleCollider2D
Rigidbody2D
EnemyHealth
MonsterAnimator
MonsterContactAttack
MonsterHealthBarGUI
```

Witch also has:

```text
WizardShooter
```

## Component Responsibilities

```text
SpriteRenderer        = shows enemy sprite and flip direction
Animator              = plays idle/walk/attack/death animations
CapsuleCollider2D     = solid enemy body
Rigidbody2D           = physics body for ground/collision
EnemyHealth           = HP and death behavior
MonsterAnimator       = wrapper for attack/death animation triggers
MonsterContactAttack  = patrol, chase, attack timing, ledge checks
MonsterAttackHitbox   = trigger damage during attack window
MonsterHealthBarGUI   = HP bar above enemy
WizardShooter         = shoots enemy projectile from FirePoint
```

## Enemy Health

Owned by:

```text
EnemyHealth
```

Current behavior:

```text
Stores current HP.
Receives bullet damage.
Disables body collider on death.
Stops Rigidbody2D movement.
Disables attack behavior.
Forces death animation.
Destroys enemy after delay.
```

Do not put death logic in bullet scripts. Bullet scripts should tell the enemy to take damage.

## Enemy Movement

Owned by:

```text
MonsterContactAttack
```

Current behavior:

```text
Wander inside range.
Detect Rika.
Chase Rika if within detection range.
Stop before ledges.
Attack when in range.
Ignore other enemy body colliders.
Lock facing during attack.
```

Tunable values:

```text
Detection Range
Attack Range
Vertical Aggro Range
Move Speed
Attack Cooldown
Wander Range
Wander Speed
Wander Pause Duration
Ledge Check values
```

## Attack Hitbox Pattern

Attack hitbox is a child GameObject.

```text
EnemyRoot
└── AttackHitbox
```

It should have:

```text
BoxCollider2D or other Collider2D
Is Trigger: true
MonsterAttackHitbox
```

It should be disabled by default. The attack animation enables it only during damage frames.

Animation event pattern:

```text
Attack animation starts
-> EnableAttackHitbox()
Damage window active
-> DisableAttackHitbox()
Damage window ends
```

This keeps visual attack timing and damage timing aligned.

## Wizard Projectile Pattern

Witch uses:

```text
WizardShooter
FirePoint
Wizard_bullet.prefab
```

Correct pattern:

```text
Witch attack animation starts
Face direction is locked
Animation event calls ShootBullet()
Wizard_bullet prefab is instantiated at FirePoint
Projectile moves by EnemyProjectile
```

Do not permanently attach projectile objects under Witch.

## Facing Direction Rules

Enemy facing is controlled by:

```text
SpriteRenderer.flipX
```

Current attack rule:

```text
When attack starts, facing is locked.
Wizard should not flip direction during an attack animation.
Bullet direction should match the locked facing direction.
```

## Death Priority

Death must override attack.

Current death rule:

```text
EnemyHealth.Die()
-> disables collider
-> stops Rigidbody2D
-> disables attack behavior
-> disables WizardShooter
-> plays Death immediately
```

`MonsterAnimator` also prevents future attack triggers after death.

## Rigidbody2D Rules

Enemies use `Rigidbody2D` so they can move on platforms.

Recommended:

```text
Body Type: Dynamic
Gravity Scale: 1
Freeze Rotation Z: enabled
```

Freezing rotation prevents enemies from rolling after collision.

## Collider Rules

Enemy body:

```text
Solid CapsuleCollider2D
Used for body collision
```

Attack hitbox:

```text
Trigger collider
Used only for damage window
```

Do not use the body collider as the attack range. Keep damage hitboxes separate.

## Sorting Layer

Enemies should use:

```text
Sorting Layer: Characters
```

Current order:

```text
Monster Order in Layer: 0
Witch Order in Layer: 1
```

Higher order draws in front.

## Checklist For New Enemy Prefab

```text
Root has SpriteRenderer
Root has Animator
Root has Rigidbody2D
Root has body Collider2D
Root has EnemyHealth
Root has MonsterAnimator
Root has movement/attack script
AttackHitbox child exists if melee damage is needed
AttackHitbox is trigger
AttackHitbox disabled by default
Animation events call hitbox methods
Death animation exists
Death has highest priority
Sorting Layer is Characters
Enemy tag is Enemy
Prefab tested in Play Mode
```

## Summary

The enemy prefab pattern is:

```text
Root object owns body, movement, health, animator.
Child hitboxes handle attack damage.
Projectile enemies use FirePoint and projectile prefab.
Death interrupts attack.
Prefab stores reusable setup.
Scene only places enemies.
```
