rimworld-animation-studio/Assets/Scripts/GUI/Actors/ActorBody.cs
AbstractConcept a55ba7b95b Code refactor
2022-10-28 00:28:51 -05:00

112 lines
3.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
namespace RimWorldAnimationStudio
{
public class ActorBody : MonoBehaviour, IPointerClickHandler, IDragHandler, IEndDragHandler
{
public int actorID;
public SpriteRenderer bodyRenderer;
private Vector3 dragDelta = new Vector3();
private void Start()
{
EventsManager.onActorBodyPartSelected.AddListener(delegate(ActorBodyPart bodyPart) { OnActorBodyPartSelected(bodyPart); });
EventsManager.onActorBodySelected.AddListener(delegate(ActorBody actorBody) { OnActorBodySelected(actorBody); });
}
public void OnActorBodySelected(ActorBody actorBody)
{
if (actorBody == this)
{ bodyRenderer.color = Constants.ColorGreen; }
else
{ bodyRenderer.color = Constants.ColorWhite; }
}
public void OnActorBodyPartSelected(ActorBodyPart bodyPart)
{
if (bodyPart.parent == this)
{ bodyRenderer.color = Constants.ColorLightGreen; }
else
{ bodyRenderer.color = Constants.ColorWhite; }
}
public void Initialize(int actorID)
{
this.actorID = actorID;
if (actorID == Workspace.ActorID)
{ Activate(); }
}
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.pointerCurrentRaycast.gameObject.GetComponent<ActorBodyPart>())
{ return; }
Activate();
}
public void OnDrag(PointerEventData eventData)
{
Activate();
PawnKeyframe keyframe = Workspace.GetCurrentPawnKeyframe(true);
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (dragDelta == Vector3.zero)
{ dragDelta = mousePosition - transform.position; }
if (Workspace.actorManipulationMode == ActorManipulationMode.Pan)
{
keyframe.BodyOffsetX = mousePosition.x - dragDelta.x - Workspace.GetActor(actorID).GetFinalTransformOffset().x;
keyframe.BodyOffsetZ = mousePosition.y - dragDelta.y - Workspace.GetActor(actorID).GetFinalTransformOffset().y;
}
else if (Workspace.actorManipulationMode == ActorManipulationMode.Rotate)
{
float angle = -Vector2.SignedAngle(Vector2.down, (Vector2)mousePosition - (Vector2)transform.position);
keyframe.BodyAngle = angle;
}
else if (Workspace.actorManipulationMode == ActorManipulationMode.Face)
{
float angle = Vector2.SignedAngle(Vector2.up, (Vector2)mousePosition - (Vector2)transform.position);
int facing = -Mathf.RoundToInt(angle / 90f );
facing = facing < 0 ? facing + 4 : facing;
keyframe.BodyFacing = facing;
}
PawnAnimationClip clip = Workspace.GetPawnAnimationClip(actorID);
clip.BuildSimpleCurves();
EventsManager.OnPawnKeyframeChanged(keyframe);
}
public void OnEndDrag(PointerEventData eventData)
{
Workspace.RecordEvent("Actor position / orientation");
dragDelta = Vector3.zero;
}
public ActorBodyPart GetBodyPart(string bodyPart)
{
return GetComponentsInChildren<ActorBodyPart>(true)?.FirstOrDefault(x => x.bodyPart.ToLower() == bodyPart);
}
public void Activate()
{
Workspace.ActorID = actorID;
Workspace.selectedBodyPart = null;
EventsManager.OnActorBodySelected(this);
}
}
}