This commit is contained in:
Nim-XD 2025-07-09 10:31:02 +05:30
parent 6f66dd5023
commit 19fff3745e
11 changed files with 348 additions and 226 deletions

BIN
.DS_Store vendored

Binary file not shown.

BIN
Assets/.DS_Store vendored

Binary file not shown.

Binary file not shown.

View File

@ -314,7 +314,7 @@ BoxCollider2D:
m_CallbackLayers: m_CallbackLayers:
serializedVersion: 2 serializedVersion: 2
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 1
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: -0.1117599, y: 0.8405181} m_Offset: {x: -0.1117599, y: 0.8405181}
@ -347,6 +347,7 @@ MonoBehaviour:
syncInterval: 0 syncInterval: 0
health: 100 health: 100
magicalHealth: 0 magicalHealth: 0
shieldBreak: 0
healthBar: {fileID: 3653340846896160726} healthBar: {fileID: 3653340846896160726}
MagicalhealthBar: {fileID: 3972942435470074426} MagicalhealthBar: {fileID: 3972942435470074426}
speed: -0.65 speed: -0.65

View File

@ -333,7 +333,7 @@ BoxCollider2D:
m_CallbackLayers: m_CallbackLayers:
serializedVersion: 2 serializedVersion: 2
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 1
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: 0.16937256, y: 0.6145453} m_Offset: {x: 0.16937256, y: 0.6145453}
@ -366,6 +366,7 @@ MonoBehaviour:
syncInterval: 0 syncInterval: 0
health: 100 health: 100
magicalHealth: 0 magicalHealth: 0
shieldBreak: 0
healthBar: {fileID: 3653340846896160726} healthBar: {fileID: 3653340846896160726}
MagicalhealthBar: {fileID: 2197384177600481826} MagicalhealthBar: {fileID: 2197384177600481826}
speed: -0.55 speed: -0.55

View File

@ -217,7 +217,7 @@ BoxCollider2D:
m_CallbackLayers: m_CallbackLayers:
serializedVersion: 2 serializedVersion: 2
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 1
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: 0.045366943, y: 3.246708} m_Offset: {x: 0.045366943, y: 3.246708}
@ -250,6 +250,7 @@ MonoBehaviour:
syncInterval: 0 syncInterval: 0
health: 100 health: 100
magicalHealth: 0 magicalHealth: 0
shieldBreak: 0
healthBar: {fileID: 3653340846896160726} healthBar: {fileID: 3653340846896160726}
MagicalhealthBar: {fileID: 5662903864418491351} MagicalhealthBar: {fileID: 5662903864418491351}
speed: -0.8 speed: -0.8

View File

@ -250,7 +250,7 @@ BoxCollider2D:
m_CallbackLayers: m_CallbackLayers:
serializedVersion: 2 serializedVersion: 2
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 1
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: -0.23830795, y: 0.44725013} m_Offset: {x: -0.23830795, y: 0.44725013}
@ -283,6 +283,7 @@ MonoBehaviour:
syncInterval: 0 syncInterval: 0
health: 100 health: 100
magicalHealth: 0 magicalHealth: 0
shieldBreak: 0
healthBar: {fileID: 3653340846896160726} healthBar: {fileID: 3653340846896160726}
MagicalhealthBar: {fileID: 6929609094885432149} MagicalhealthBar: {fileID: 6929609094885432149}
speed: -0.5 speed: -0.5

View File

@ -314,7 +314,7 @@ BoxCollider2D:
m_CallbackLayers: m_CallbackLayers:
serializedVersion: 2 serializedVersion: 2
m_Bits: 4294967295 m_Bits: 4294967295
m_IsTrigger: 0 m_IsTrigger: 1
m_UsedByEffector: 0 m_UsedByEffector: 0
m_UsedByComposite: 0 m_UsedByComposite: 0
m_Offset: {x: 0.31513762, y: 0.77473223} m_Offset: {x: 0.31513762, y: 0.77473223}
@ -347,6 +347,7 @@ MonoBehaviour:
syncInterval: 0 syncInterval: 0
health: 100 health: 100
magicalHealth: 0 magicalHealth: 0
shieldBreak: 0
healthBar: {fileID: 3653340846896160726} healthBar: {fileID: 3653340846896160726}
MagicalhealthBar: {fileID: 2709162945487164275} MagicalhealthBar: {fileID: 2709162945487164275}
speed: -0.5 speed: -0.5

View File

@ -912,8 +912,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 0} m_AnchorMax: {x: 1, y: 0}
m_AnchoredPosition: {x: 0.00012207031, y: 20} m_AnchoredPosition: {x: 0.000061035156, y: 20}
m_SizeDelta: {x: -0.00025510788, y: 30} m_SizeDelta: {x: 0.00012207, y: 30}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &6875305720456407933 --- !u!114 &6875305720456407933
MonoBehaviour: MonoBehaviour:
@ -9073,9 +9073,9 @@ RectTransform:
- {fileID: 6834699274966114621} - {fileID: 6834699274966114621}
m_Father: {fileID: 288749914621996688} m_Father: {fileID: 288749914621996688}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 19.964722, y: 0} m_AnchoredPosition: {x: -512.0353, y: 0}
m_SizeDelta: {x: 30, y: 30} m_SizeDelta: {x: 30, y: 30}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &5623080609460378711 --- !u!222 &5623080609460378711
@ -9833,8 +9833,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -200, y: 0} m_AnchoredPosition: {x: -178.037, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &8114871264030549166 --- !u!222 &8114871264030549166
CanvasRenderer: CanvasRenderer:
@ -13203,8 +13203,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -326.67, y: 0} m_AnchoredPosition: {x: -292.94977, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6437560145585663992 --- !u!222 &6437560145585663992
CanvasRenderer: CanvasRenderer:
@ -19127,8 +19127,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 180, y: 0} m_AnchoredPosition: {x: 166.69412, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &9180523985572885400 --- !u!222 &9180523985572885400
CanvasRenderer: CanvasRenderer:
@ -21201,8 +21201,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 53.33, y: 0} m_AnchoredPosition: {x: 51.781, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &4521138676283010074 --- !u!222 &4521138676283010074
CanvasRenderer: CanvasRenderer:
@ -34847,8 +34847,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -73.33, y: 0} m_AnchoredPosition: {x: -63.124, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &3385706722609023602 --- !u!222 &3385706722609023602
CanvasRenderer: CanvasRenderer:
@ -36103,7 +36103,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5334876594431442471} m_GameObject: {fileID: 5334876594431442471}
m_Enabled: 1 m_Enabled: 0
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cfabb0440166ab443bba8876756fdfa9, type: 3} m_Script: {fileID: 11500000, guid: cfabb0440166ab443bba8876756fdfa9, type: 3}
m_Name: m_Name:
@ -36995,8 +36995,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: -453.33, y: 0} m_AnchoredPosition: {x: -407.853, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &7247761221542065640 --- !u!222 &7247761221542065640
CanvasRenderer: CanvasRenderer:
@ -38522,8 +38522,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 561.29, y: 0} m_AnchoredPosition: {x: 492.82718, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6825953737906669211 --- !u!222 &6825953737906669211
CanvasRenderer: CanvasRenderer:
@ -38546,7 +38546,7 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
m_Material: {fileID: 0} m_Material: {fileID: 0}
m_Color: {r: 0.4, g: 0.73333335, b: 0.41568628, a: 1} m_Color: {r: 0.4, g: 0.73333335, b: 0.41568628, a: 0}
m_RaycastTarget: 1 m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1 m_Maskable: 1
@ -39021,8 +39021,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 306.67, y: 0} m_AnchoredPosition: {x: 281.60678, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &1671463973499466441 --- !u!222 &1671463973499466441
CanvasRenderer: CanvasRenderer:
@ -39261,9 +39261,9 @@ RectTransform:
- {fileID: 7854863165062211890} - {fileID: 7854863165062211890}
m_Father: {fileID: 288749914621996688} m_Father: {fileID: 288749914621996688}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 19.964844, y: 0} m_AnchoredPosition: {x: -512.03516, y: 0}
m_SizeDelta: {x: 30, y: 30} m_SizeDelta: {x: 30, y: 30}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &3746002634997393255 --- !u!222 &3746002634997393255
@ -39352,8 +39352,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0.5} m_AnchorMin: {x: 0, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5} m_AnchorMax: {x: 1, y: 0.5}
m_AnchoredPosition: {x: -2.7998047, y: 0} m_AnchoredPosition: {x: -4.0448, y: 0}
m_SizeDelta: {x: -49.190063, y: 16} m_SizeDelta: {x: -46.7, y: 16}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &5990951506473996236 --- !u!1 &5990951506473996236
GameObject: GameObject:
@ -57453,8 +57453,8 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 433.33, y: 0} m_AnchoredPosition: {x: 396.5109, y: 0}
m_SizeDelta: {x: 25, y: 25} m_SizeDelta: {x: 22.68, y: 25}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &1598896423630024094 --- !u!222 &1598896423630024094
CanvasRenderer: CanvasRenderer:

View File

@ -134,4 +134,4 @@ public class PlayerAttack : NetworkBehaviour{
// } // }
} }

View File

@ -15,6 +15,11 @@ public class enemyScript : NetworkBehaviour
public int health; public int health;
[SyncVar(hook = nameof(OnMagicalHealthChange))] [SyncVar(hook = nameof(OnMagicalHealthChange))]
public int magicalHealth; public int magicalHealth;
// NEW: Shield break boolean
[SyncVar]
public bool shieldBreak = false;
public SpriteHealthBar healthBar; public SpriteHealthBar healthBar;
public SpriteHealthBar MagicalhealthBar; public SpriteHealthBar MagicalhealthBar;
public float speed; public float speed;
@ -40,63 +45,73 @@ public class enemyScript : NetworkBehaviour
MeshRenderer meshRenderer; MeshRenderer meshRenderer;
public GameObject hitVfx; public GameObject hitVfx;
void Awake(){ void Awake()
{
meshRenderer = GetComponent<MeshRenderer>(); meshRenderer = GetComponent<MeshRenderer>();
scanCooldown = Random.Range(0.5f, 1.5f); scanCooldown = Random.Range(0.5f, 1.5f);
} }
private void Start(){ private void Start()
rb2 = GetComponent<Rigidbody2D>(); {
rb2 = GetComponent<Rigidbody2D>();
//target = GameObject.FindWithTag("Player").transform; //target = GameObject.FindWithTag("Player").transform;
UpdateAnimation(directionString,animationString); UpdateAnimation(directionString, animationString);
defaultPos = transform.position; defaultPos = transform.position;
} }
[SyncVar(hook =nameof(OnLevelChanged))] [SyncVar(hook = nameof(OnLevelChanged))]
public int level; public int level;
void OnLevelChanged(int oldVal, int newVal){ void OnLevelChanged(int oldVal, int newVal)
if(isServer){return;} {
if (isServer) { return; }
SetLevel(newVal); SetLevel(newVal);
} }
public void SetLevel(int _level){ public void SetLevel(int _level)
{
if(enemyLevel != null){ if (enemyLevel != null)
enemyLevel.text = _level.ToString(); {
enemyLevel.text = _level.ToString();
} }
level = _level; level = _level;
int healthIncrement =level * HEALTH_INC; int healthIncrement = level * HEALTH_INC;
maxHealth = 100 + healthIncrement; maxHealth = 100 + healthIncrement;
health = (int)maxHealth; health = (int)maxHealth;
magicalHealth = (int)maxHealth; magicalHealth = (int)maxHealth;
enemyAttackDamage += (int)(level * DAMAGE_INC); enemyAttackDamage += (int)(level * DAMAGE_INC);
// MODIFIED: Reset shield break when level is set
shieldBreak = false;
// Debug.Log($"{health}/{maxHealth}"); // Debug.Log($"{health}/{maxHealth}");
} }
public Vector3 defScale; public Vector3 defScale;
Vector3 defaultPos; Vector3 defaultPos;
float playerDistCheckTimer = 0f;
float playerDistCheckTimer=0f; void LateUpdate()
void LateUpdate(){ {
LOD(); LOD();
} }
public const float disappearDistFromPlayer = 15f; public const float disappearDistFromPlayer = 15f;
void LOD(){ void LOD()
if(playerDistCheckTimer > 0){playerDistCheckTimer -= Time.deltaTime;return;} {
if (playerDistCheckTimer > 0) { playerDistCheckTimer -= Time.deltaTime; return; }
playerDistCheckTimer = Random.Range(1.5f,2.5f); playerDistCheckTimer = Random.Range(1.5f, 2.5f);
if(playerNetwork.localPlayerTransform == null){return;} if (playerNetwork.localPlayerTransform == null) { return; }
float distToPlayer = Vector3.Distance(playerNetwork.localPlayerTransform.position, transform.position); float distToPlayer = Vector3.Distance(playerNetwork.localPlayerTransform.position, transform.position);
meshRenderer.enabled = distToPlayer < disappearDistFromPlayer; meshRenderer.enabled = distToPlayer < disappearDistFromPlayer;
} }
#if UNITY_SERVER || UNITY_EDITOR #if UNITY_SERVER || UNITY_EDITOR
[Server] [Server]
private void Update(){ private void Update()
{
// animator.skeleton.SetSkin // animator.skeleton.SetSkin
// set animation state to running if in chase Range // set animation state to running if in chase Range
@ -104,54 +119,67 @@ public class enemyScript : NetworkBehaviour
// isInChaseRange = Physics2D.OverlapCircle(transform.position, chaseRadius , layerMask); // isInChaseRange = Physics2D.OverlapCircle(transform.position, chaseRadius , layerMask);
// isInAttackRange = Physics2D.OverlapCircle(transform.position, attackRadius, layerMask); // isInAttackRange = Physics2D.OverlapCircle(transform.position, attackRadius, layerMask);
if (health <= 0 ){
// MODIFIED: Check both health and magicalHealth for death condition
if (health <= 0 || (shieldBreak && magicalHealth <= 0))
{
return; return;
} }
if(target != null){
if (target != null)
{
isInChaseRange = Vector3.Distance(transform.position, target.transform.position) < chaseRadius; isInChaseRange = Vector3.Distance(transform.position, target.transform.position) < chaseRadius;
isInAttackRange = Vector3.Distance(transform.position, target.transform.position) < attackRadius; isInAttackRange = Vector3.Distance(transform.position, target.transform.position) < attackRadius;
}else{ }
else
{
isInChaseRange = false; isInChaseRange = false;
isInAttackRange = false; isInAttackRange = false;
} }
ScanPlayers(); ScanPlayers();
if(target !=null){ if (target != null)
{
enemyFollow(); enemyFollow();
} }
} }
#endif #endif
float scanTimer =0; float scanTimer = 0;
float scanCooldown; float scanCooldown;
public void ScanPlayers(){ public void ScanPlayers()
{
if(scanTimer >0){scanTimer-=Time.deltaTime; return;}
if (scanTimer > 0) { scanTimer -= Time.deltaTime; return; }
scanTimer = scanCooldown; scanTimer = scanCooldown;
playerNetwork[] playersinNetwork = FindObjectsOfType<playerNetwork>(); playerNetwork[] playersinNetwork = FindObjectsOfType<playerNetwork>();
float closestDist = float.MaxValue; float closestDist = float.MaxValue;
playerNetwork closestPlayer = null; playerNetwork closestPlayer = null;
foreach(playerNetwork player in playersinNetwork ){ foreach (playerNetwork player in playersinNetwork)
if(player.health <= 0 ){continue;} {
if (player.health <= 0) { continue; }
float dist = Vector3.Distance(transform.position, player.transform.position); float dist = Vector3.Distance(transform.position, player.transform.position);
if(dist < closestDist){ if (dist < closestDist)
{
closestPlayer = player; closestPlayer = player;
closestDist = dist; closestDist = dist;
} }
} }
if(closestDist < chaseRadius){ if (closestDist < chaseRadius)
target = closestPlayer ; {
target = closestPlayer;
} }
else { else
{
target = null; target = null;
} }
//if(target == null) {return;} //if(target == null) {return;}
} }
@ -160,79 +188,99 @@ public class enemyScript : NetworkBehaviour
// void RpcUpdateAnim(string animDir , string animName, bool isLoop){ // void RpcUpdateAnim(string animDir , string animName, bool isLoop){
// UpdateAnimation(animDir , animName, isLoop); // UpdateAnimation(animDir , animName, isLoop);
// } // }
[SyncVar(hook =nameof(OnFlipped))] [SyncVar(hook = nameof(OnFlipped))]
bool isFlipped= false; bool isFlipped = false;
void OnFlipped(bool oldVal, bool newVal){ void OnFlipped(bool oldVal, bool newVal)
if(isServer){return;} {
transform.localScale = new Vector3(defScale.x * (newVal ? -1 : 1),defScale.y,defScale.z); if (isServer) { return; }
transform.localScale = new Vector3(defScale.x * (newVal ? -1 : 1), defScale.y, defScale.z);
HandleFlip(); HandleFlip();
} }
void HandleFlip(){ void HandleFlip()
if(uiEnemy == null){ {
return; if (uiEnemy == null)
{
return;
} }
if(transform.localScale.x < 0 ){ if (transform.localScale.x < 0)
uiEnemy.localScale = new Vector3(-1,1,1); {
uiEnemy.localScale = new Vector3(-1, 1, 1);
} }
else{ else
uiEnemy.localScale = new Vector3(1,1,1); {
uiEnemy.localScale = new Vector3(1, 1, 1);
} }
} }
private void enemyFollow(){ private void enemyFollow()
{
if(Mathf.Abs(dir.y) > Mathf.Abs(dir.x)){
if(dir.y < 0){ if (Mathf.Abs(dir.y) > Mathf.Abs(dir.x))
{
if (dir.y < 0)
{
directionString = "Back"; directionString = "Back";
}else{ }
else
{
directionString = "Front"; directionString = "Front";
} }
}else{ }
else
{
directionString = "Side"; directionString = "Side";
if(dir.x < 0){ if (dir.x < 0)
transform.localScale = new Vector3(defScale.x,defScale.y,0); {
isFlipped=false; transform.localScale = new Vector3(defScale.x, defScale.y, 0);
}else{ isFlipped = false;
transform.localScale = new Vector3(-defScale.x,defScale.y,0); }
else
{
transform.localScale = new Vector3(-defScale.x, defScale.y, 0);
isFlipped = true; isFlipped = true;
} }
HandleFlip(); HandleFlip();
} }
if(animationHistory != directionString + animationString){ if (animationHistory != directionString + animationString)
{
UpdateAnimation(directionString, animationString); UpdateAnimation(directionString, animationString);
// RpcUpdateAnim(directionString, animationString,true); // RpcUpdateAnim(directionString, animationString,true);
} }
animationHistory=directionString + animationString; animationHistory = directionString + animationString;
if(target != null){ if (target != null)
{
dir = transform.position - target.transform.position; dir = transform.position - target.transform.position;
} }
float angle = Mathf.Atan2(dir.y , dir.x ) * Mathf.Rad2Deg; float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
dir.Normalize(); dir.Normalize();
movement = dir; movement = dir;
if(rotate){ if (rotate)
{
//set anim direction x, y dir //set anim direction x, y dir
} }
} }
string animationHistory =""; string animationHistory = "";
[SyncVar(hook =nameof(OnAnimationDirectionChanged))] [SyncVar(hook = nameof(OnAnimationDirectionChanged))]
public string directionString = "Side"; public string directionString = "Side";
[SyncVar(hook =nameof(OnAnimationNameChanged))] [SyncVar(hook = nameof(OnAnimationNameChanged))]
public string animationString = "Idle"; public string animationString = "Idle";
void OnAnimationDirectionChanged(string oldVal, string newVal){ void OnAnimationDirectionChanged(string oldVal, string newVal)
{
UpdateAnimation(newVal, animationString); UpdateAnimation(newVal, animationString);
} }
void OnAnimationNameChanged(string oldVal, string newVal){ void OnAnimationNameChanged(string oldVal, string newVal)
{
UpdateAnimation(directionString, newVal); UpdateAnimation(directionString, newVal);
} }
@ -241,67 +289,85 @@ public class enemyScript : NetworkBehaviour
[SyncVar] [SyncVar]
public float maxHealth; public float maxHealth;
#if UNITY_SERVER || UNITY_EDITOR #if UNITY_SERVER || UNITY_EDITOR
[Server] [Server]
private void FixedUpdate() { private void FixedUpdate()
if (health <= 0 || magicalHealth <= 0){ {
// MODIFIED: Updated death condition
if (health <= 0 || (shieldBreak && magicalHealth <= 0))
{
return; return;
} }
healthBar.SetHealth(health, maxHealth); healthBar.SetHealth(health, maxHealth);
MagicalhealthBar.SetHealth(magicalHealth, maxHealth); //magical health maxout err MagicalhealthBar.SetHealth(magicalHealth, maxHealth);
if(isInChaseRange && !isInAttackRange ){
if (isInChaseRange && !isInAttackRange)
{
MoveEnemy(movement); MoveEnemy(movement);
//Set animation to moving //Set animation to moving
animationString = "Walk"; animationString = "Walk";
} }
if(isInAttackRange){ if (isInAttackRange)
{
rb2.velocity = Vector2.zero; rb2.velocity = Vector2.zero;
//Set animation to attack //Set animation to attack
animationString = "Attack"; animationString = "Attack";
if(attackTimer < attackDuration){ if (attackTimer < attackDuration)
{
attackTimer += Time.deltaTime; attackTimer += Time.deltaTime;
}else{ }
attackTimer = 0 ; else
{
attackTimer = 0;
Attack(); Attack();
} }
//TODO: ATTACK HERE //TODO: ATTACK HERE
} }
if(!isInAttackRange && !isInChaseRange){ if (!isInAttackRange && !isInChaseRange)
{
//SetAnimation to idle //SetAnimation to idle
animationString = "Idle"; animationString = "Idle";
} }
} }
#endif #endif
public void Attack(){ public void Attack()
{
target.TakeDamage(enemyAttackDamage); target.TakeDamage(enemyAttackDamage);
} }
private void MoveEnemy(Vector2 dir){ private void MoveEnemy(Vector2 dir)
{
rb2.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime)); rb2.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime));
} }
void UpdateAnimation(string direction, string animationName){ void UpdateAnimation(string direction, string animationName)
{
// try{ // try{
StartCoroutine(CoroutineUpdateAnim(direction, animationName)); StartCoroutine(CoroutineUpdateAnim(direction, animationName));
} }
IEnumerator CoroutineUpdateAnim(string direction, string animationName){ IEnumerator CoroutineUpdateAnim(string direction, string animationName)
while(animator == null){ {
while (animator == null)
{
yield return new WaitForSeconds(0.1f); yield return new WaitForSeconds(0.1f);
Debug.LogError("animator is null!"); Debug.LogError("animator is null!");
} }
while(animator.skeleton == null){ while (animator.skeleton == null)
{
yield return new WaitForSeconds(0.1f); yield return new WaitForSeconds(0.1f);
Debug.LogError("animator skelton is null!"); Debug.LogError("animator skelton is null!");
} }
while(animator.AnimationState == null){ while (animator.AnimationState == null)
{
yield return new WaitForSeconds(0.1f); yield return new WaitForSeconds(0.1f);
Debug.LogError("animator state is null!"); Debug.LogError("animator state is null!");
@ -316,90 +382,150 @@ public class enemyScript : NetworkBehaviour
Debug.Log($"Updating enemy animation {direction}_{animationName}"); Debug.Log($"Updating enemy animation {direction}_{animationName}");
} }
[Command(requiresAuthority =false)] [Command(requiresAuthority = false)]
void CmdTakeDamage(int damage,uint id){ void CmdTakeDamage(int damage, uint id)
takedmg(damage,id); {
takedmg(damage, id);
Debug.Log("Enemy Attack Recieved "); Debug.Log("Enemy Attack Recieved ");
} }
public void TakeDamage(int damage, uint id){
if(isServer){ public void TakeDamage(int damage, uint id)
takedmg(damage,id); {
if (isServer)
{
takedmg(damage, id);
} }
else{ else
CmdTakeDamage(damage,id); {
CmdTakeDamage(damage, id);
} }
} }
void takedmg(int damage,uint id){ // MODIFIED: Completely rewritten damage system
if(health<=0){return;} void takedmg(int damage, uint id)
health -= damage; {
//hit vfx if (health <= 0) { return; }
// GameObject newObject = Instantiate(hitVfx , transform.position , Quaternion.identity );
// newObject.transform.localPosition = Vector3.zero; int finalDamage = damage;
// newObject.transform.parent = transform;
if(health<= 0 ){ // If shield is not broken, reduce damage by half and damage magical health
if (!shieldBreak && magicalHealth > 0)
{
finalDamage = damage / 2;
// Calculate magical health damage based on player attack damage + enemy level
int magicalDamage = damage + level;
magicalHealth -= magicalDamage;
// Check if shield breaks
if (magicalHealth <= 0)
{
shieldBreak = true;
magicalHealth = 0;
Debug.Log("Shield Broken!");
}
}
// Apply damage to health
health -= finalDamage;
// Check for death
if (health <= 0)
{
StartCoroutine(couroutineDeath()); StartCoroutine(couroutineDeath());
foreach(playerNetwork player in FindObjectsOfType<playerNetwork>()){ foreach (playerNetwork player in FindObjectsOfType<playerNetwork>())
if(player.netId == id){ {
if (player.netId == id)
//This one attacked me {
player.OnEnemyKilled(level); player.OnEnemyKilled(level);
} }
} }
} }
Debug.Log("Enemy Takes Damage ***");
Debug.Log($"Enemy Takes Damage: {finalDamage} | Shield Broken: {shieldBreak} | Health: {health} | Magical Health: {magicalHealth}");
} }
[Command(requiresAuthority =false)] [Command(requiresAuthority = false)]
void CmdTakeMagicalDamage(int damage,uint id){ void CmdTakeMagicalDamage(int damage, uint id)
takeMagicalDmg(damage,id); {
Debug.Log("Enemy Attack Recieved "); takeMagicalDmg(damage, id);
Debug.Log("Enemy Magical Attack Recieved ");
} }
public void TakeMagicalDamage(int damage, uint id){
if(isServer){ public void TakeMagicalDamage(int damage, uint id)
takeMagicalDmg(damage,id); {
if (isServer)
{
takeMagicalDmg(damage, id);
} }
else{ else
CmdTakeMagicalDamage(damage,id); {
CmdTakeMagicalDamage(damage, id);
} }
} }
void takeMagicalDmg(int damage,uint id){ // MODIFIED: Updated magical damage to use same system
if(magicalHealth<=0){return;} void takeMagicalDmg(int damage, uint id)
magicalHealth -= damage; {
if(magicalHealth<= 0 ){ if (magicalHealth <= 0 && shieldBreak) { return; }
int finalDamage = damage;
// If shield is not broken, reduce damage by half and damage magical health
if (!shieldBreak && magicalHealth > 0)
{
finalDamage = damage / 2;
// Calculate magical health damage based on player attack damage + enemy level
int magicalDamage = damage + level;
magicalHealth -= magicalDamage;
// Check if shield breaks
if (magicalHealth <= 0)
{
shieldBreak = true;
magicalHealth = 0;
Debug.Log("Shield Broken!");
}
}
// Apply damage to health
health -= finalDamage;
// Check for death
if (health <= 0)
{
StartCoroutine(couroutineDeath()); StartCoroutine(couroutineDeath());
foreach(playerNetwork player in FindObjectsOfType<playerNetwork>()){ foreach (playerNetwork player in FindObjectsOfType<playerNetwork>())
if(player.netId == id){ {
if (player.netId == id)
//This one attacked me {
player.OnEnemyKilled(level); player.OnEnemyKilled(level);
} }
} }
} }
Debug.Log("Enemy Takes Damage ***");
Debug.Log($"Enemy Takes Magical Damage: {finalDamage} | Shield Broken: {shieldBreak} | Health: {health} | Magical Health: {magicalHealth}");
} }
IEnumerator couroutineDeath(){ IEnumerator couroutineDeath()
{
animationString = "Death"; animationString = "Death";
StartCoroutine(PopDisappearUI()); StartCoroutine(PopDisappearUI());
UpdateAnimation(directionString , animationString); UpdateAnimation(directionString, animationString);
// RpcUpdateAnim(directionString, animationString,false); // RpcUpdateAnim(directionString, animationString,false);
Vector3 lootSpawnPos = transform.position; Vector3 lootSpawnPos = transform.position;
lootSpawnPos.z = GameManager.instance.LootSpawnPointsParent.GetChild(0).position.z; lootSpawnPos.z = GameManager.instance.LootSpawnPointsParent.GetChild(0).position.z;
//instantiate loot item //instantiate loot item
GameObject newLoot = Instantiate(GameManager.instance.GetRandomLoot(), lootSpawnPos, Quaternion.identity); GameObject newLoot = Instantiate(GameManager.instance.GetRandomLoot(), lootSpawnPos, Quaternion.identity);
NetworkServer.Spawn(newLoot); NetworkServer.Spawn(newLoot);
yield return new WaitForSecondsRealtime(7);// dead corpse delay yield return new WaitForSecondsRealtime(7);// dead corpse delay
if (!isServer) if (!isServer)
{ {
CmdDie(); CmdDie();
@ -408,66 +534,57 @@ public class enemyScript : NetworkBehaviour
{ {
GameManager.OnEnemyDeath(this, defaultPos); GameManager.OnEnemyDeath(this, defaultPos);
} }
/* transform.position = defaultPos;
health = (int)maxHealth;
magicalHealth = (int)maxHealth;*/
//animationString = "Idle";
} }
[Command] [Command]
void CmdDie() void CmdDie()
{ {
GameManager.OnEnemyDeath(this,defaultPos); GameManager.OnEnemyDeath(this, defaultPos);
} }
public void OnHealthChange(int oldVlaue, int newValue)
{
public void OnHealthChange(int oldVlaue, int newValue){ healthBar.SetHealth(newValue, maxHealth);
healthBar.SetHealth(newValue,maxHealth);
} }
public void OnMagicalHealthChange(int oldVlaue, int newValue){
MagicalhealthBar.SetHealth(newValue,maxHealth); public void OnMagicalHealthChange(int oldVlaue, int newValue)
{
MagicalhealthBar.SetHealth(newValue, maxHealth);
} }
//etc for ui Disspear coroutine //etc for ui Disspear coroutine
IEnumerator PopDisappearUI(){ IEnumerator PopDisappearUI()
Vector3 originalScale = uiEnemy.localScale;
// First, scale up slightly
float popDuration = 0.15f;
float elapsedTime = 0f;
Vector3 popScale = originalScale * 1.2f;
while(elapsedTime < popDuration)
{ {
float t = elapsedTime / popDuration; Vector3 originalScale = uiEnemy.localScale;
uiEnemy.localScale = Vector3.Lerp(originalScale, popScale, t);
elapsedTime += Time.deltaTime;
yield return null;
}
// Then scale down to zero quickly
float shrinkDuration = 0.3f;
elapsedTime = 0f;
while(elapsedTime < shrinkDuration)
{
float t = elapsedTime / shrinkDuration;
// Use ease-in curve for faster shrinking
float easedT = t * t;
uiEnemy.localScale = Vector3.Lerp(popScale, Vector3.zero, easedT);
elapsedTime += Time.deltaTime;
yield return null;
}
uiEnemy.localScale = Vector3.zero;
uiEnemy.gameObject.SetActive(false);
}
// First, scale up slightly
float popDuration = 0.15f;
float elapsedTime = 0f;
Vector3 popScale = originalScale * 1.2f;
while (elapsedTime < popDuration)
{
float t = elapsedTime / popDuration;
uiEnemy.localScale = Vector3.Lerp(originalScale, popScale, t);
elapsedTime += Time.deltaTime;
yield return null;
}
// Then scale down to zero quickly
float shrinkDuration = 0.3f;
elapsedTime = 0f;
while (elapsedTime < shrinkDuration)
{
float t = elapsedTime / shrinkDuration;
// Use ease-in curve for faster shrinking
float easedT = t * t;
uiEnemy.localScale = Vector3.Lerp(popScale, Vector3.zero, easedT);
elapsedTime += Time.deltaTime;
yield return null;
}
uiEnemy.localScale = Vector3.zero;
uiEnemy.gameObject.SetActive(false);
}
} }