npc get animation wound and text damage

This commit is contained in:
VDH
2025-10-13 18:11:35 +07:00
parent 7bc5c65229
commit 7b73223b55
8 changed files with 506 additions and 236 deletions
@@ -1,4 +1,7 @@
using BrewMonster;
using CSNetwork.GPDataType;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
@@ -6,23 +9,20 @@ using static CECAttacksMan;
public class CECAttacksMan : MonoSingleton<CECAttacksMan>
{
private readonly List<CECAttackEvent> m_AttackList = new List<CECAttackEvent>();
private readonly LinkedList<CECAttackEvent> m_AttackLinkedList = new LinkedList<CECAttackEvent>();
private void Update()
{
for (int i = 0; i < m_AttackList.Count; i++)
var node = m_AttackLinkedList.First;
while (node != null)
{
var attack = m_AttackList[i];
if (attack.m_bFinished)
{
m_AttackList.RemoveAt(i);
}
else
{
}
var next = node.Next;
if (node.Value.m_bFinished)
m_AttackLinkedList.Remove(node);
else node.Value.Tick((uint)(Time.deltaTime * 1000));
node = next;
}
}
@@ -30,7 +30,7 @@ public class CECAttacksMan : MonoSingleton<CECAttacksMan>
{
CECAttackerEvents result = new CECAttackerEvents();
foreach (var attack in m_AttackList)
foreach (var attack in m_AttackLinkedList)
{
if (attack.m_idHost == idHost)
{
@@ -56,10 +56,10 @@ public class CECAttacksMan : MonoSingleton<CECAttacksMan>
200, // timeToBeFired
nTimeFly // timeToDoDamage
);
m_AttackList.Add(newEvent);
m_AttackLinkedList.AddLast(newEvent);
newEvent.UpdateTargetFlag();
return m_AttackList[m_AttackList.Count - 1];
return m_AttackLinkedList.Last.Value;
}
public CECAttackEvent AddSkillAttack(int idHost, int idCastTarget, int idTarget, int idWeapon, int idSkill, int nSkillLevel, uint dwModifier, int nDamage)
@@ -77,16 +77,16 @@ public class CECAttacksMan : MonoSingleton<CECAttacksMan>
200, // timeToBeFired
1000 // timeToDoDamage
);
m_AttackList.Add(newEvent);
m_AttackLinkedList.AddLast(newEvent);
newEvent.UpdateTargetFlag();
return m_AttackList[m_AttackList.Count - 1];
return m_AttackLinkedList.Last.Value;
}
// === thêm tạm để code có thể compile ===
public void AddAttack(CECAttackEvent evt)
{
m_AttackList.Add(evt);
m_AttackLinkedList.AddLast(evt);
}
public class TARGET_DATA
{
@@ -136,6 +136,126 @@ public class CECAttackEvent
AddTarget(idTarget, dwModifier, nDamage);
}
public bool Tick(uint dwDeltaTime)
{
m_timeLived += dwDeltaTime;
if (!m_bSignaled)
{
if (m_timeLived > 3500)
{
// too long time, this event will be deleted now
m_bFinished = true;
//DoFire();
DoDamage();
}
return true;
}
else
{
if (m_timeToBeFired != 0)
{
if (m_timeToBeFired <= dwDeltaTime)
{
m_timeToBeFired = 0;
// Fire here
//DoFire();
}
else
m_timeToBeFired -= dwDeltaTime;
}
else if (m_timeToDoDamage != 0)
{
if (m_timeToDoDamage <= dwDeltaTime)
{
m_timeToDoDamage = 0;
// Do damage here
DoDamage();
}
else
m_timeToDoDamage -= dwDeltaTime;
}
}
return true;
}
private bool DoDamage()
{
m_bDoDamaged = true;
m_bFinished = true;
/* CECGameRun pGameRun = g_pGame-GetGameRun();
int idHostPlayer = pGameRun->GetHostPlayer()->GetCharacterID();*/
// Get host name
/* ACString strHostName;
CECObject* pHostObject = pGameRun->GetWorld()->GetObject(m_idHost, 0);
if (pHostObject)
{
if (ISNPCID(m_idHost))
strHostName = ((CECNPC*)pHostObject)->GetName();
else if (ISPLAYERID(m_idHost))
strHostName = GetPlayerName((CECPlayer*)pHostObject);
}*/
int nNumTargets = m_targets.Count;
for (int i = 0; i < nNumTargets; i++)
{
TARGET_DATA data = m_targets[i];
int idTarget = data.idTarget;
string strName;
if (GPDataTypeHelper.ISNPCID(idTarget))
{
CECNPC pNPC = null;
if ((data.dwModifier & (uint)MOD.MOD_SUCCESS) != 0)
pNPC = EC_ManMessageMono.Instance._CECNPCMan.GetNPCFromAll(idTarget);
else
{
pNPC = EC_ManMessageMono.Instance._CECNPCMan.GetNPCFromAll(idTarget);
if (!pNPC)
return true;
//strName = pNPC->GetNameToShow();
}
if (!pNPC)
return true;
pNPC.Damaged(data.nDamage, data.dwModifier);
}
else if (GPDataTypeHelper.ISPLAYERID(idTarget))
{
/* CECPlayer* pPlayer = pGameRun->GetWorld()->GetPlayerMan()->GetPlayer(idTarget);
if (!pPlayer)
return true;
strName = GetPlayerName(pPlayer);
pPlayer->Damaged(data.nDamage, data.dwModifier, m_idSkill);*/
}
/* if (data.nDamage > 0)
{
if (m_idHost == idHostPlayer)
{
if (!strName.IsEmpty())
pGameRun->AddFixedChannelMsg(FIXMSG_DODAMAGE, GP_CHAT_DAMAGE, strName, data.nDamage);
}
else if (data.idTarget == idHostPlayer)
{
if (!strHostName.IsEmpty())
pGameRun->AddFixedChannelMsg(FIXMSG_BEDAMAGED, GP_CHAT_DAMAGE, strHostName, data.nDamage);
}
}*/
}
return true;
}
public bool AddTarget(int idTarget, uint dwModifier, int nDamage)
{
@@ -6,6 +6,7 @@ using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Unity.VisualScripting;
using UnityEngine;
public class CECNPCMan : CECObject, IMsgHandler
@@ -203,6 +204,22 @@ public class CECNPCMan : CECObject, IMsgHandler
return npc;
}
public CECNPC GetNPCFromAll(int nid)
{
CECNPC pNPC = GetNPC(nid);
if (pNPC)
return pNPC;
// Search from disappear array ?
/*for (int i = 0; i < m_aDisappearNPCs.GetSize(); i++)
{
CECNPC* pNPC = m_aDisappearNPCs[i];
if (pNPC->GetNPCID() == nid)
return pNPC;
}*/
return null;
}
public CECNPC CreateNPC(info_npc Info, bool bBornInSight, ReadOnlySpan<byte> packet, int infoOffset)
{
CECNPC pNPC = null;
@@ -0,0 +1,85 @@
using UnityEngine;
using System.Collections.Generic;
using TMPro;
using DG.Tweening; // cần DOTween
public class DamageTextManager : MonoBehaviour
{
public static DamageTextManager Instance { get; private set; }
[Header("Prefab")]
[SerializeField] private TextMeshPro damageTextPrefab;
[Header("Settings")]
[SerializeField] private int poolSize = 20;
[SerializeField] private Vector3 offset = new Vector3(0, 2f, 0);
[SerializeField] private float riseDistance = 1.5f;
[SerializeField] private float riseDuration = 0.8f;
private readonly Queue<TextMeshPro> pool = new();
private void Awake()
{
// Singleton
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
// Tạo sẵn pool
for (int i = 0; i < poolSize; i++)
{
var textObj = Instantiate(damageTextPrefab, transform);
textObj.gameObject.SetActive(false);
pool.Enqueue(textObj);
}
}
/// <summary>
/// Gọi để spawn text damage
/// </summary>
public void SpawnDamage(Vector3 worldPos, int damage, Color color, float scale = 1f)
{
var text = GetFromPool();
text.text = damage.ToString();
text.color = color;
text.fontSize = 6;
text.transform.localScale = Vector3.one * scale;
Vector3 startPos = worldPos + offset;
text.transform.position = startPos;
text.gameObject.SetActive(true);
// Hiệu ứng bay lên + mờ dần
text.transform.DOMoveY(startPos.y + riseDistance, riseDuration).SetEase(Ease.OutQuad);
text.DOFade(0f, riseDuration)
.SetEase(Ease.InQuad)
.OnComplete(() =>
{
text.alpha = 1f;
text.gameObject.SetActive(false);
ReturnToPool(text);
});
}
private TextMeshPro GetFromPool()
{
if (pool.Count > 0)
{
return pool.Dequeue();
}
// Nếu hết pool, tạo thêm
var text = Instantiate(damageTextPrefab, transform);
text.gameObject.SetActive(false);
return text;
}
private void ReturnToPool(TextMeshPro text)
{
pool.Enqueue(text);
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e7981b037b60f4a4989ff59265a308ad
+57
View File
@@ -264,6 +264,63 @@ public class CECNPC : CECObject
}
}
}
public void Damaged(int iDamage, uint dwModifier/* 0 */)
{
if (iDamage == -1 || iDamage == -2)
{
// when else player hit this npc iDamage is -1,
// so if iDamage is -1 we will shoud the wounded animation
if (iDamage == -1)
PlayModelAction((int)NPCActionIndex. ACT_WOUNDED);
DamageTextManager.Instance.SpawnDamage(transform.position, iDamage, Color.red, 1.0f);
/*if ((dwModifier & (uint)MOD.MOD_IMMUNE) != 0 *//* && !IsImmuneDisable()*//*)
textma
else if (dwModifier & CECAttackEvent::MOD_NULLITY)
BubbleText(BUBBLE_INVALIDHIT, 0);
else if (dwModifier & CECAttackEvent::MOD_ENCHANT_FAILED)
BubbleText(BUBBLE_LOSE, 0);
else if (dwModifier & CECAttackEvent::MOD_SUCCESS)
BubbleText(BUBBLE_SUCCESS, 0);
else if (dwModifier & CECAttackEvent::MOD_DODGE_DEBUFF)
BubbleText(BUBBLE_DODGE_DEBUFF, 0);*/
}
else
{
// this message is related to the host, so we should show a pop up message
// Popup a damage decal
/* bool bDeadlyStrike = (dwModifier & CECAttackEvent::MOD_CRITICAL_STRIKE) ? true : false;
bool bRetort = (dwModifier & CECAttackEvent::MOD_RETORT) ? true : false;*/
if (iDamage > 0)
{
PlayModelAction((int)NPCActionIndex.ACT_WOUNDED);
DamageTextManager.Instance.SpawnDamage(transform.position, iDamage, Color.red, 1.0f);
/* int p1 = 0;
if (bDeadlyStrike)
p1 |= 0x0001;
else if (bRetort)
p1 |= 0x0002;*/
/* if (dwModifier & CECAttackEvent::MOD_REBOUND)
BubbleText(BUBBLE_REBOUND, (DWORD)iDamage);
else if (dwModifier & CECAttackEvent::MOD_BEAT_BACK)
BubbleText(BUBBLE_BEAT_BACK, (DWORD)iDamage);
else
BubbleText(BUBBLE_DAMAGE, (DWORD)iDamage, p1);*/
}
/* else if ((dwModifier & CECAttackEvent::MOD_IMMUNE) && !IsImmuneDisable())
BubbleText(BUBBLE_IMMUNE, 0);
else if (dwModifier & CECAttackEvent::MOD_NULLITY)
BubbleText(BUBBLE_INVALIDHIT, 0);
else if (dwModifier & CECAttackEvent::MOD_ENCHANT_FAILED)
BubbleText(BUBBLE_LOSE, 0);
else if (dwModifier & CECAttackEvent::MOD_SUCCESS)
BubbleText(BUBBLE_SUCCESS, 0);
else
BubbleText(BUBBLE_HITMISSED, 0);*/
}
}
public void WorkFinished(int iWorkID)
{
+3 -1
View File
@@ -1,4 +1,5 @@
using Animancer;
using BrewMonster;
using UnityEngine;
public class NPCVisual : MonoBehaviour
@@ -6,10 +7,11 @@ public class NPCVisual : MonoBehaviour
[SerializeField] NamedAnimancerComponent namedAnimancer;
public bool TryPlayAction(string animationName)
{
BMLogger.LogError("HoangDev: TryPlayAction: " + animationName);
if (namedAnimancer == null) return false;
if (namedAnimancer.IsPlaying(animationName)) return false;
return namedAnimancer.TryPlay("慢速移动") == null;
return namedAnimancer.TryPlay(animationName) == null;
}
public void InitNPCEventDoneHandler()
{
+196 -1
View File
@@ -75386,6 +75386,182 @@ Mesh:
offset: 0
size: 0
path:
--- !u!1 &927529342
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 927529343}
- component: {fileID: 927529345}
- component: {fileID: 927529344}
m_Layer: 0
m_Name: DamageText
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &927529343
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 927529342}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1336646766}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 9000}
m_SizeDelta: {x: 20, y: 5}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &927529344
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 927529342}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9541d86e2fd84c1d9990edf0852d74ab, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_text:
m_isRightToLeft: 0
m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_fontSharedMaterials: []
m_fontMaterial: {fileID: 0}
m_fontMaterials: []
m_fontColor32:
serializedVersion: 2
rgba: 4294967295
m_fontColor: {r: 1, g: 1, b: 1, a: 1}
m_enableVertexGradient: 0
m_colorMode: 3
m_fontColorGradient:
topLeft: {r: 1, g: 1, b: 1, a: 1}
topRight: {r: 1, g: 1, b: 1, a: 1}
bottomLeft: {r: 1, g: 1, b: 1, a: 1}
bottomRight: {r: 1, g: 1, b: 1, a: 1}
m_fontColorGradientPreset: {fileID: 0}
m_spriteAsset: {fileID: 0}
m_tintAllSprites: 0
m_StyleSheet: {fileID: 0}
m_TextStyleHashCode: -1183493901
m_overrideHtmlColors: 0
m_faceColor:
serializedVersion: 2
rgba: 4294967295
m_fontSize: 2
m_fontSizeBase: 2
m_fontWeight: 400
m_enableAutoSizing: 0
m_fontSizeMin: 18
m_fontSizeMax: 72
m_fontStyle: 0
m_HorizontalAlignment: 2
m_VerticalAlignment: 512
m_textAlignment: 65535
m_characterSpacing: 0
m_wordSpacing: 0
m_lineSpacing: 0
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_TextWrappingMode: 0
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 0
m_ActiveFontFeatures: 6e72656b
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 0
m_isCullingEnabled: 0
m_horizontalMapping: 0
m_verticalMapping: 0
m_uvLineOffset: 0
m_geometrySortingOrder: 0
m_IsTextObjectScaleStatic: 0
m_VertexBufferAutoSizeReduction: 0
m_useMaxVisibleDescender: 1
m_pageToDisplay: 1
m_margin: {x: 0, y: 0, z: 0, w: 0}
m_isUsingLegacyAnimationComponent: 0
m_isVolumetricText: 0
_SortingLayer: 0
_SortingLayerID: 0
_SortingOrder: 0
m_hasFontAssetChanged: 0
m_renderer: {fileID: 927529345}
m_maskType: 0
--- !u!23 &927529345
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 927529342}
m_Enabled: 1
m_CastShadows: 0
m_ReceiveShadows: 0
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RayTracingAccelStructBuildFlagsOverride: 0
m_RayTracingAccelStructBuildFlags: 1
m_SmallMeshCulling: 1
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!1 &928368405
GameObject:
m_ObjectHideFlags: 0
@@ -104211,6 +104387,7 @@ GameObject:
m_Component:
- component: {fileID: 1336646766}
- component: {fileID: 1336646767}
- component: {fileID: 1336646768}
m_Layer: 0
m_Name: NPC Manager
m_TagString: Untagged
@@ -104230,7 +104407,8 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Children:
- {fileID: 927529343}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1336646767
@@ -104246,6 +104424,23 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
modelPlayerCharacter: {fileID: 960760103}
--- !u!114 &1336646768
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1336646764}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e7981b037b60f4a4989ff59265a308ad, type: 3}
m_Name:
m_EditorClassIdentifier:
damageTextPrefab: {fileID: 927529344}
poolSize: 20
offset: {x: 0, y: 2, z: 0}
riseDistance: 1.5
riseDuration: 0.8
--- !u!1 &1338403792
GameObject:
m_ObjectHideFlags: 0
File diff suppressed because one or more lines are too long