rimworld-animations/1.3/Source/Comps/CompThingAnimator.cs

191 lines
4.3 KiB
C#
Raw Normal View History

2022-01-25 02:17:49 +00:00
using rjw;
using System;
2020-04-09 00:43:01 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2022-01-25 02:17:49 +00:00
using UnityEngine;
using Verse;
2020-04-09 00:43:01 +00:00
namespace Rimworld_Animations {
2022-01-25 02:17:49 +00:00
public class CompThingAnimator : ThingComp
2020-04-09 00:43:01 +00:00
{
2022-01-25 02:17:49 +00:00
Vector3 anchor;
Pawn pawn;
public bool isAnimating = false;
int animTicks = 0, stageTicks = 0, clipTicks = 0, curStage = 0;
float rotation = 0;
float clipPercent = 0;
public Vector3 deltaPos;
AnimationDef anim;
private ThingAnimationClip clip => (ThingAnimationClip)stage.animationClips[1];
private AnimationStage stage
{
get
{
return anim.animationStages[curStage];
}
}
public void StartAnimation(AnimationDef anim, Pawn pawn)
{
isAnimating = true;
this.anim = anim;
this.pawn = pawn;
animTicks = 0;
stageTicks = 0;
clipTicks = 0;
curStage = 0;
clipPercent = 0;
tickAnim();
}
public void setAnchor(IntVec3 position)
{
anchor = position.ToVector3();
}
public override void CompTick()
{
base.CompTick();
if(isAnimating)
{
if (pawn.Dead || pawn?.jobs?.curDriver == null || (pawn?.jobs?.curDriver != null && !(pawn?.jobs?.curDriver is rjw.JobDriver_Sex)))
{
isAnimating = false;
}
else
{
tickAnim();
}
}
}
public void tickAnim()
{
if (!isAnimating) return;
animTicks++;
if (animTicks < anim.animationTimeTicks)
{
tickStage();
}
else
{
if (LoopNeverending())
{
ResetOnLoop();
}
else
{
isAnimating = false;
}
}
}
public void tickStage()
{
if (stage == null)
{
isAnimating = false;
return;
}
stageTicks++;
if (stageTicks >= stage.playTimeTicks)
{
curStage++;
stageTicks = 0;
clipTicks = 0;
clipPercent = 0;
}
if (curStage >= anim.animationStages.Count)
{
if (LoopNeverending())
{
ResetOnLoop();
}
else
{
isAnimating = false;
}
}
else
{
tickClip();
}
}
public void tickClip()
{
clipTicks++;
if (clipPercent >= 1 && stage.isLooping)
{
clipTicks = 1;//warning: don't set to zero or else calculations go wrong
}
clipPercent = (float)clipTicks / (float)clip.duration;
calculateDrawValues();
}
private void calculateDrawValues()
{
//shift up and right 0.5f to align center
deltaPos = new Vector3(clip.PositionX.Evaluate(clipPercent) + 0.5f/* todo * (mirror ? -1 : 1) */, AltitudeLayer.Item.AltitudeFor(), clip.PositionZ.Evaluate(clipPercent) + 0.5f);
Log.Message("Clip percent: " + clipPercent + " deltaPos: " + deltaPos);
rotation = clip.Rotation.Evaluate(clipPercent);
}
public void AnimateThing(Thing thing)
{
thing.Graphic.Draw(deltaPos + anchor, Rot4.North, thing, rotation);
}
public bool LoopNeverending()
{
if (pawn?.jobs?.curDriver != null &&
(pawn.jobs.curDriver is JobDriver_Sex) && (pawn.jobs.curDriver as JobDriver_Sex).neverendingsex)
{
return true;
}
return false;
}
public void ResetOnLoop()
{
curStage = 1;
animTicks = 0;
stageTicks = 0;
clipTicks = 0;
tickAnim();
}
2020-04-09 00:43:01 +00:00
}
}