Files
PacManUdon/Assets/Scripts/GridMover.cs

65 lines
1.9 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)
{
if (eventType != NetworkEventType.PacManTurn)
{
return;
}
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)
{
if (eventType != NetworkEventType.PacManTurn)
{
return true;
}
SetPosition(new Vector2(BitConverter.ToSingle(data, offset), BitConverter.ToSingle(data, offset + 4)));
SetDirection(new Vector2(BitConverter.ToSingle(data, offset + 8), BitConverter.ToSingle(data, offset + 12)));
offset += 16;
return true;
}
}
}