Skip to content

Commit

Permalink
entity rot no longer shared, calc on movedirection
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Jun 10, 2024
1 parent a365c3d commit d013ee4
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 111 deletions.
2 changes: 2 additions & 0 deletions l2-unity/Assets/Scripts/Game/Manager/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void SpawnPlayer(NetworkIdentity identity, PlayerStatus status, PlayerSta
CharacterRaceAnimation raceId = CharacterRaceAnimationParser.ParseRace(race, appearance.Race, identity.IsMage);

GameObject go = CharacterBuilder.Instance.BuildCharacterBase(raceId, appearance, true);
go.transform.eulerAngles = new Vector3(transform.eulerAngles.x, identity.Heading, transform.eulerAngles.z);
go.transform.position = identity.Position;
go.transform.name = "Player";

Expand Down Expand Up @@ -144,6 +145,7 @@ public void SpawnUser(NetworkIdentity identity, Status status, Stats stats, Play

GameObject go = CharacterBuilder.Instance.BuildCharacterBase(raceId, appearance, false);
go.transform.position = identity.Position;
go.transform.eulerAngles = new Vector3(transform.eulerAngles.x, identity.Heading, transform.eulerAngles.z);

UserEntity user = go.GetComponent<UserEntity>();
user.Status = status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,41 @@ void Start() {
if(_characterController == null || World.Instance.OfflineMode || _animationReceive == null) {
this.enabled = false;
}

_direction = Vector3.zero;
_destination = Vector3.zero;
}

public void UpdateMoveDirection(Vector3 direction) {
_speed = _entity.Stats.ScaledSpeed;
_direction = direction;
}

public void SetDestination(Vector3 destination) {
_speed = _entity.Stats.ScaledSpeed;
_destination = destination;
}

private void FixedUpdate() {

if(!_networkTransformReceive.IsPositionSynced()) {
if (!_networkTransformReceive.IsPositionSynced()) {
/* pause script during position sync */
return;
}

if(_destination != null && _destination != Vector3.zero) {
if (_destination != null && _destination != Vector3.zero) {
SetMoveDirectionToDestination();
}

Vector3 ajustedDirection = _direction * _speed * _moveSpeedMultiplier + Vector3.down * _gravity;
_characterController.Move(ajustedDirection * Time.deltaTime);
}

public void UpdateMoveDirection(Vector3 direction) {
_speed = _entity.Stats.ScaledSpeed;
_direction = direction;

Debug.Log("call: " + direction);
if (direction.x != 0 || direction.z != 0) {
_networkTransformReceive.SetFinalRotation(VectorUtils.CalculateMoveDirectionAngle(direction.x, direction.z));
}
}

public void SetDestination(Vector3 destination) {
_speed = _entity.Stats.ScaledSpeed;
_destination = destination;
}

public void SetMoveDirectionToDestination() {
Vector3 transformFlat = VectorUtils.To2D(transform.position);
Vector3 destinationFlat = VectorUtils.To2D(_destination);
Expand Down
11 changes: 2 additions & 9 deletions l2-unity/Assets/Scripts/Networking/NetworkTransformReceive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,12 @@ public void SetFinalRotation(float finalRotation) {

public void LookAt(Transform target) {
if (target != null) {
_newRotation = CalculateAngleToLookAt(target.position);
_newRotation = VectorUtils.CalculateMoveDirectionAngle(transform.position, target.position);
}
}

public void LookAt(Vector3 position) {
_newRotation = CalculateAngleToLookAt(position);
}

private float CalculateAngleToLookAt(Vector3 position) {
float angle = Mathf.Atan2(position.x - transform.position.x, position.z - transform.position.z) * Mathf.Rad2Deg;
angle = Mathf.Round(angle / 45f);
angle *= 45f;
return angle;
_newRotation = VectorUtils.CalculateMoveDirectionAngle(transform.position, position);
}

public bool IsPositionSynced() {
Expand Down
6 changes: 4 additions & 2 deletions l2-unity/Assets/Scripts/Networking/NetworkTransformShare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class NetworkTransformShare : MonoBehaviour {

[SerializeField] public Vector3 _serverPosition;
[SerializeField] public bool _shouldShareRotation;
public bool _rotationShareEnabled;

public bool ShouldShareRotation { get { return _shouldShareRotation; } set { _shouldShareRotation = value; } }

Expand All @@ -21,14 +22,15 @@ void Start() {
_lastPos = transform.position;
_lastRot = transform.forward;
_lastSharedPosTime = Time.time;
_rotationShareEnabled = false; //rotation is now calculated based on movedirection
}

void Update() {
if (ShouldSharePosition()) {
SharePosition();
}

if(ShouldShareRotation) {
if(ShouldShareRotation && _rotationShareEnabled) {
ShareRotation();
}
}
Expand All @@ -47,7 +49,7 @@ public void SharePosition() {
_lastSharedPosTime = Time.time;
_lastPos = transform.position;

ClientPacketHandler.Instance.UpdateRotation(transform.eulerAngles.y);
//ClientPacketHandler.Instance.UpdateRotation(transform.eulerAngles.y);
}

public void ShareRotation() {
Expand Down
2 changes: 1 addition & 1 deletion l2-unity/Assets/Scripts/Terrain/Geodata/GeodataImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static Vector3 LoadMapOrigin(string mapId, float nodeSize) {
throw new Exception($"Map {mapId} prefab not found at {mapFolder}.");
}

Vector3 origin = VectorUtils.floorToNearest(map.transform.position, nodeSize);
Vector3 origin = VectorUtils.FloorToNearest(map.transform.position, nodeSize);
return origin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ void Start() {
}

void GenerateGeodata() {
Vector3 roundedTerrainPos = VectorUtils.floorToNearest(terrainTransform.position, nodeSize);
Vector3 roundedTerrainPos = VectorUtils.FloorToNearest(terrainTransform.position, nodeSize);
if(enableCustomGeneratedTerrainOrigin) {
roundedTerrainPos = VectorUtils.floorToNearest(customGeneratedTerrainOrigin, nodeSize);
roundedTerrainPos = VectorUtils.FloorToNearest(customGeneratedTerrainOrigin, nodeSize);
}
Debug.Log(roundedTerrainPos);
for (int x = 0; x < scaledTerrainWidth; x++) {
Expand All @@ -79,7 +79,7 @@ void GenerateGeodata() {

float y = hits[i].point.y - roundedTerrainPos.y;
Vector3 nodePos = new Vector3 (x * nodeSize, y, z * nodeSize);
nodePos = VectorUtils.floorToNearest(nodePos, nodeSize);
nodePos = VectorUtils.FloorToNearest(nodePos, nodeSize);

if((oldHeight - hits[i].point.y) > characterHeight || i == 0) {
Vector3 scaledNodePos = new Vector3(x, nodePos.y / nodeSize, z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static GameObject BuildAmbientSoundGameObject(AmbientSound ambientSound)
collider.radius = soundRadius;
collider.isTrigger = true;

go.transform.position = VectorUtils.convertToUnity(ambientSound.position);
go.transform.position = VectorUtils.ConvertToUnity(ambientSound.position);
go.layer = LayerMask.NameToLayer("AmbientSound");

// event
Expand Down
10 changes: 5 additions & 5 deletions l2-unity/Assets/Scripts/Tools/Terrain/L2BrushBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void Build(string path) {

GameObject brush = new GameObject(b.name);
brush.transform.parent = brushContainer.transform;
brush.transform.position = VectorUtils.convertToUnity(b.position) - VectorUtils.convertToUnity(b.prePivot);
brush.transform.position = VectorUtils.ConvertToUnity(b.position) - VectorUtils.ConvertToUnity(b.prePivot);

Debug.Log(b.name);
Model model = b.model;
Expand All @@ -52,7 +52,7 @@ static void Build(string path) {

// Adjust verticles
for(int p = 0; p < polyData.vertices.Length; p++) {
polyData.vertices[p] = VectorUtils.convertToUnity(polyData.vertices[p]);
polyData.vertices[p] = VectorUtils.ConvertToUnity(polyData.vertices[p]);
}

List<string> pPolyFlags = new List<string>(polyData.polyFlags);
Expand Down Expand Up @@ -80,9 +80,9 @@ static GameObject createProbuilderMesh(string csgOper, PolyData polyData, int in

Material material = GetMaterialForTexture(polyData.texture);

Vector3 adjustedNormal = VectorUtils.convertToUnityUnscaled(polyData.normal);
Vector3 adjustedU = VectorUtils.convertToUnityUnscaled(polyData.textureU);
Vector3 adjustedV = VectorUtils.convertToUnityUnscaled(polyData.textureV);
Vector3 adjustedNormal = VectorUtils.ConvertToUnityUnscaled(polyData.normal);
Vector3 adjustedU = VectorUtils.ConvertToUnityUnscaled(polyData.textureU);
Vector3 adjustedV = VectorUtils.ConvertToUnityUnscaled(polyData.textureV);

Quaternion rt = Quaternion.FromToRotation(Vector3.forward, adjustedNormal);
Vector3 rotatedU = rt * adjustedU;
Expand Down
27 changes: 20 additions & 7 deletions l2-unity/Assets/Scripts/Utils/VectorUtils.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using UnityEngine;

public class VectorUtils : MonoBehaviour {
public static Vector3 convertToUnity(Vector3 ueVector) {
public static Vector3 ConvertToUnity(Vector3 ueVector) {
return new Vector3(ueVector.y, ueVector.z, ueVector.x) * (1f / 52.5f);
}

public static Vector3 convertToUnityUnscaled(Vector3 ueVector) {
public static Vector3 ConvertToUnityUnscaled(Vector3 ueVector) {
return new Vector3(ueVector.y, ueVector.z, ueVector.x);
}

public static Vector3 scaleToUnity(Vector3 ueVector) {
public static Vector3 ScaleToUnity(Vector3 ueVector) {
return ueVector * (1f / 52.5f);
}

public static Vector2 rotateVector2(Vector2 vector, float angle) {
public static Vector2 RotateVector2(Vector2 vector, float angle) {
float radians = angle * Mathf.Deg2Rad;
return rotateVector2Rad(vector, radians);
return RotateVector2Rad(vector, radians);
}

public static Vector2 rotateVector2Rad(Vector2 vector, float radians) {
public static Vector2 RotateVector2Rad(Vector2 vector, float radians) {
float sin = Mathf.Sin(radians);
float cos = Mathf.Cos(radians);

Expand All @@ -28,7 +28,7 @@ public static Vector2 rotateVector2Rad(Vector2 vector, float radians) {
return new Vector2(newX, newY);
}

public static Vector3 floorToNearest(Vector3 vector, float step) {
public static Vector3 FloorToNearest(Vector3 vector, float step) {
return new Vector3(NumberUtils.FloorToNearest(vector.x, step),
NumberUtils.FloorToNearest(vector.y, step),
NumberUtils.FloorToNearest(vector.z, step));
Expand All @@ -45,4 +45,17 @@ public static float Distance2D(Vector3 from, Vector3 to) {
public static bool IsVectorZero2D(Vector3 vector) {
return vector.x == 0 && vector.z == 0;
}

public static float CalculateMoveDirectionAngle(Vector3 from, Vector3 to) {
// Calculate the direction vector (destination - current position)
float directionX = to.x - from.x;
float directionZ = to.z - from.z;

return CalculateMoveDirectionAngle(directionX, directionZ);
}

public static float CalculateMoveDirectionAngle(float directionX, float directionZ) {
float angle = Mathf.Atan2(directionX, directionZ) * Mathf.Rad2Deg;
return angle;
}
}
Loading

0 comments on commit d013ee4

Please sign in to comment.