55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
namespace Marro.PacManUdon
|
|
{
|
|
using System;
|
|
using UdonSharp;
|
|
using UnityEngine;
|
|
using VRC.Udon.Serialization.OdinSerializer;
|
|
|
|
public abstract class GridMover : SyncedObject
|
|
{
|
|
protected Vector2 direction;
|
|
|
|
public virtual Vector2 GetPosition()
|
|
{
|
|
return (Vector2)transform.localPosition;
|
|
}
|
|
|
|
public virtual void SetPosition(Vector2 position)
|
|
{
|
|
transform.localPosition = new Vector3(position.x, position.y, transform.localPosition.z);
|
|
}
|
|
|
|
public virtual Vector2 GetDirection()
|
|
{
|
|
return direction;
|
|
}
|
|
|
|
public void SetDirection(Vector2 direction)
|
|
{
|
|
this.direction = direction;
|
|
UpdateAnimator();
|
|
}
|
|
|
|
protected abstract void UpdateAnimator();
|
|
|
|
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType)
|
|
{
|
|
var position = GetPosition();
|
|
data[offset++] = BitConverter.GetBytes(position.x);
|
|
data[offset++] = BitConverter.GetBytes(position.y);
|
|
|
|
var direction = GetDirection();
|
|
data[offset++] = BitConverter.GetBytes(direction.x);
|
|
data[offset++] = BitConverter.GetBytes(direction.y);
|
|
}
|
|
|
|
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
|
|
{
|
|
SetPosition(new Vector2(BitConverter.ToSingle(data, offset), BitConverter.ToSingle(data, offset + 2)));
|
|
SetDirection(new Vector2(BitConverter.ToSingle(data, offset + 4), BitConverter.ToSingle(data, offset + 6)));
|
|
offset += 8;
|
|
|
|
return true;
|
|
}
|
|
}
|
|
} |