Skip to content

Commit

Permalink
server select visual fix, new applydamage packet struct
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Jul 14, 2024
1 parent 8c38ae5 commit 9a32ba1
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 23 deletions.
2 changes: 1 addition & 1 deletion l2-unity/Assets/Scripts/Game/Character/CharacterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Awake() {
// Load player animations, face and hair
public GameObject BuildCharacterBase(CharacterRaceAnimation raceId, PlayerAppearance appearance, EntityType entityType) {

Debug.Log($"Building character: Race:{raceId} Sex:{appearance.Sex} Face:{appearance.Face} Hair:{appearance.HairStyle} HairC:{appearance.HairColor}");
//Debug.Log($"Building character: Race:{raceId} Sex:{appearance.Sex} Face:{appearance.Face} Hair:{appearance.HairStyle} HairC:{appearance.HairColor}");

GameObject entity = Instantiate(ModelTable.Instance.GetContainer(raceId, entityType));
GameObject face = Instantiate(ModelTable.Instance.GetFace(raceId, appearance.Face));
Expand Down
4 changes: 2 additions & 2 deletions l2-unity/Assets/Scripts/Game/Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public virtual void Initialize() {
}

// Called when ApplyDamage packet is received
public void ApplyDamage(int damage, int newHp, bool criticalHit) {
public void ApplyDamage(int damage, bool criticalHit) {
if(_status.Hp <= 0) {
Debug.LogWarning("Trying to apply damage to a dead entity");
return;
}

_status.Hp = Mathf.Max(newHp, 0);
_status.Hp = Mathf.Max(_status.Hp - damage, 0);

OnHit(criticalHit);

Expand Down
13 changes: 8 additions & 5 deletions l2-unity/Assets/Scripts/Game/Manager/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ public void SpawnNpc(NetworkIdentity identity, NpcStatus status, Stats stats) {

npc.Status = status;

npc.Stats = stats;

npc.Identity = identity;
npc.Identity.NpcClass = npcgrp.ClassName;
npc.Identity.Name = npcName.Name;
Expand All @@ -229,7 +231,6 @@ public void SpawnNpc(NetworkIdentity identity, NpcStatus status, Stats stats) {
}
npc.Identity.TitleColor = npcName.TitleColor;

npc.Stats = stats;
npc.Appearance = appearance;

npcGo.transform.eulerAngles = new Vector3(npcGo.transform.eulerAngles.x, identity.Heading, npcGo.transform.eulerAngles.z);
Expand Down Expand Up @@ -285,12 +286,12 @@ public Task UpdateObjectAnimation(int id, int animId, float value) {
});
}

public Task InflictDamageTo(int sender, int target, int damage, int newHp, bool criticalHit) {
public Task InflictDamageTo(int sender, int target, int damage, bool criticalHit) {
return ExecuteWithEntitiesAsync(sender, target, (senderEntity, targetEntity) => {
if (senderEntity != null) {
WorldCombat.Instance.InflictAttack(senderEntity.transform, targetEntity.transform, damage, newHp, criticalHit);
WorldCombat.Instance.InflictAttack(senderEntity.transform, targetEntity.transform, damage, criticalHit);
} else {
WorldCombat.Instance.InflictAttack(targetEntity.transform, damage, newHp, criticalHit);
WorldCombat.Instance.InflictAttack(targetEntity.transform, damage, criticalHit);
}
});
}
Expand Down Expand Up @@ -327,7 +328,9 @@ public Task EntityStopAutoAttacking(int id) {
public Task StatusUpdate(int id, List<StatusUpdatePacket.Attribute> attributes) {
return ExecuteWithEntityAsync(id, e => {
WorldCombat.Instance.StatusUpdate(e, attributes);
CharacterInfoWindow.Instance.UpdateValues();
if(e == PlayerEntity.Instance) {
CharacterInfoWindow.Instance.UpdateValues();
}
});
}

Expand Down
15 changes: 9 additions & 6 deletions l2-unity/Assets/Scripts/Game/Manager/WorldCombat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ void OnDestroy() {
}


public void InflictAttack(Transform target, int damage, int newHp, bool criticalHit) {
ApplyDamage(target, damage, newHp, criticalHit);
public void InflictAttack(Transform target, int damage, bool criticalHit) {
ApplyDamage(target, damage, criticalHit);
}

public void InflictAttack(Transform attacker, Transform target, int damage, int newHp, bool criticalHit) {
ApplyDamage(target, damage, newHp, criticalHit);
public void InflictAttack(Transform attacker, Transform target, int damage, bool criticalHit) {
ApplyDamage(target, damage, criticalHit);

// Instantiate hit particle
ParticleImpact(attacker, target);
}

private void ApplyDamage(Transform target, int damage, int newHp, bool criticalHit) {
private void ApplyDamage(Transform target, int damage, bool criticalHit) {
Entity entity = target.GetComponent<Entity>();
if (entity != null) {
// Apply damage to target
entity.ApplyDamage(damage, newHp, criticalHit);
entity.ApplyDamage(damage, criticalHit);
}
}

Expand Down Expand Up @@ -73,6 +73,9 @@ public void StatusUpdate(Entity entity, List<Attribute> attributes) {
Stats stats = entity.Stats;

foreach (Attribute attribute in attributes) {
if (entity != PlayerEntity.Instance) {
Debug.LogWarning($"{entity.Identity.Name} - {(AttributeType)attribute.id}");
}
switch((AttributeType) attribute.id) {
case AttributeType.LEVEL:
stats.Level = attribute.value;
Expand Down
44 changes: 44 additions & 0 deletions l2-unity/Assets/Scripts/Networking/ClientLibrary/Model/Hit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hit
{
private static int HITFLAG_USESS = 0x10;
private static int HITFLAG_CRIT = 0x20;
private static int HITFLAG_SHLD = 0x40;
private static int HITFLAG_MISS = 0x80;

private int _targetId;
private int _damage;
private int _flags = 0;

public int TargetId { get { return _targetId; } }
public int Damage { get { return _damage; } }

public Hit(int targetId, int damage, int flags) {
this._targetId = targetId;
this._damage = damage;
this._flags = flags;
}

public bool isCrit() {
return (_flags & HITFLAG_CRIT) != 0;
}

public bool isMiss() {
return (_flags & HITFLAG_MISS) != 0;
}

public bool hasSoulshot() {
return (_flags & HITFLAG_USESS) != 0;
}

public bool isShielded() {
return (_flags & HITFLAG_SHLD) != 0;
}

public int getSsGrade() {
return _flags & 0x0F;
}
}
11 changes: 11 additions & 0 deletions l2-unity/Assets/Scripts/Networking/ClientLibrary/Model/Hit.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using UnityEngine;
using System;
public class InflictDamagePacket : ServerPacket {
private Hit[] _hits;
public int SenderId { get; private set; }
public int TargetId { get; private set; }
public int Value { get; private set; }
public int NewHp { get; private set; }
public bool CriticalHit { get; private set; }
public Hit[] Hits { get { return _hits; }}

public InflictDamagePacket(byte[] d) : base(d) {
Parse();
Expand All @@ -14,10 +12,19 @@ public InflictDamagePacket(byte[] d) : base(d) {
public override void Parse() {
try {
SenderId = ReadI();
TargetId = ReadI();
Value = ReadI();
NewHp = ReadI();
CriticalHit = ReadB() == 0 ? false : true;

byte hitCount = ReadB();
_hits = new Hit[hitCount];

for (int i = 0; i < hitCount; i++) {
int targetId = ReadI();
int damage = ReadI();
int hitFlags = ReadI();

Hit hit = new Hit(targetId, damage, hitFlags);
_hits[i] = hit;
}

} catch(Exception e) {
Debug.LogError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ private void OnUpdateAnimation(byte[] data) {

private void OnInflictDamage(byte[] data) {
InflictDamagePacket packet = new InflictDamagePacket(data);
World.Instance.InflictDamageTo(packet.SenderId, packet.TargetId, packet.Value, packet.NewHp, packet.CriticalHit);
Hit[] hits = packet.Hits;

for (int i = 0; i < hits.Length; i++) {
World.Instance.InflictDamageTo(packet.SenderId, hits[i].TargetId, hits[i].Damage, hits[i].isCrit());
}
}

private void OnNpcInfoReceive(byte[] data) {
Expand Down
2 changes: 2 additions & 0 deletions l2-unity/Assets/Scripts/UI/Login/ServerSelectWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ protected override IEnumerator BuildWindow(VisualElement root) {
}

public void UpdateServerList(int lastServer, List<ServerData> serverData, Dictionary<int, int> charsOnServers) {
ResetWindow();

_serverData = serverData;

for (int i = 0; i < serverData.Count; i++) {
Expand Down

0 comments on commit 9a32ba1

Please sign in to comment.