rimworld-animation-studio/Assets/Scripts/GUI/ActorBody.cs

98 lines
3.4 KiB
C#
Raw Normal View History

2022-09-13 05:36:34 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2022-09-13 05:36:34 +00:00
using UnityEngine;
using UnityEngine.EventSystems;
2022-09-13 05:36:34 +00:00
namespace RimWorldAnimationStudio
{
public class ActorBody : MonoBehaviour, IPointerClickHandler, IDragHandler, IEndDragHandler
2022-09-13 05:36:34 +00:00
{
public int actorID;
public bool isSelected = false;
2022-09-13 05:36:34 +00:00
public SpriteRenderer bodyRenderer;
public SpriteRenderer headRenderer;
public SpriteRenderer appendageRenderer;
2022-09-26 04:10:41 +00:00
private Vector3 delta = new Vector3();
public bool actorBodyPartSelected { get { return GetComponentsInChildren<ActorBodyPart>().Any(x => x.isSelected); } }
public void Initialize(int actorID)
{
this.actorID = actorID;
}
public void Update()
{
if (Workspace.actorID == actorID && Workspace.selectedBodyPart == null)
{ bodyRenderer.color = Constants.ColorGreen; }
else
{ bodyRenderer.color = Constants.ColorWhite; }
2022-09-21 21:15:25 +00:00
appendageRenderer.gameObject.SetActive(Workspace.animationDef.actors[actorID].requiredGenitals.Any(x => x == "Penis") || Workspace.animationDef.actors[actorID].isFucking);
}
public void OnPointerClick(PointerEventData eventData)
{
2022-09-24 07:17:40 +00:00
if (eventData.pointerCurrentRaycast.gameObject.GetComponent<ActorBodyPart>())
{ return; }
Activate();
}
public void OnDrag(PointerEventData eventData)
{
Activate();
PawnKeyframe keyframe = Workspace.Instance.GetCurrentPawnKeyframe(true);
if (keyframe == null)
{ Debug.LogWarning("Cannot alter actor - no keyframe data available"); return; }
2022-09-26 04:10:41 +00:00
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (delta == Vector3.zero)
{ delta = mousePosition - transform.position; }
if (Workspace.actorManipulationMode == ActorManipulationMode.Pan)
{
keyframe.bodyOffsetX = mousePosition.x - delta.x - Workspace.animationDef.actors[actorID].GetFinalTransformOffset().x;
keyframe.bodyOffsetZ = mousePosition.y - delta.y - Workspace.animationDef.actors[actorID].GetFinalTransformOffset().y;
}
else if (Workspace.actorManipulationMode == ActorManipulationMode.Rotate)
{
2022-09-24 07:17:40 +00:00
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.Instance.GetPawnAnimationClip(actorID);
clip.BuildSimpleCurves();
}
public void OnEndDrag(PointerEventData eventData)
{
Workspace.Instance.RecordEvent("Actor position / orientation");
2022-09-26 04:10:41 +00:00
delta = Vector3.zero;
}
public void Activate()
{
Workspace.actorID = actorID;
Workspace.selectedBodyPart = null;
}
2022-09-13 05:36:34 +00:00
}
}