This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFunscript.cs
More file actions
79 lines (70 loc) · 1.93 KB
/
Funscript.cs
File metadata and controls
79 lines (70 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using Godot;
using System.Collections.Generic;
using System.Linq;
public enum ScriptType
{
MainStroke,
Sway,
Surge,
Roll,
Pitch,
Twist,
TypeCount
}
public struct Action
{
public float timestamp;
public float pos;
}
public class Funscript
{
public string Name { get; private set; }
public List<Action> Actions = new List<Action>();
public Funscript(Godot.Collections.Dictionary changeEvent)
{
Name = changeEvent["name"] as string;
UpdateFromEvent(changeEvent);
}
public void UpdateFromEvent(Godot.Collections.Dictionary changeEvent)
{
Actions.Clear();
var script = changeEvent["funscript"] as Godot.Collections.Dictionary;
var actions = script["actions"] as Godot.Collections.Array;
foreach(Godot.Collections.Dictionary action in actions)
{
Actions.Add(new Action() {
timestamp = (action["at"] as float? ?? 0.0f) / 1000.0f,
pos = action["pos"] as float? / 100.0f ?? 0.0f
});
}
}
public float GetPositionAt(float time)
{
var idx = Actions.BinarySearch(0, Actions.Count, new Action()
{
timestamp = time,
pos = 0
}, Comparer<Action>.Create((a,b) => Comparer<float>.Default.Compare(a.timestamp, b.timestamp)));
if(idx < 0)
{
idx = ~idx;
if(idx - 1 >= 0 && idx < Actions.Count)
{
var current = Actions[idx-1];
var next = Actions[idx];
float t = (time - current.timestamp) / (next.timestamp - current.timestamp);
return Mathf.Lerp(current.pos, next.pos, t);
}
else if(Actions.Count > 0)
{
return Actions.Last().pos;
}
}
else
{
return Actions[idx].pos;
}
return 0.5f;
}
}