From bfc591e7b66e620096b4f3ce72ec280f7fca29d1 Mon Sep 17 00:00:00 2001 From: CrushedPixel Date: Thu, 10 Sep 2015 13:01:56 +0200 Subject: [PATCH] Finished the AdvancedPositionKeyframeList and SpectatorDataInterpolation --- .../AdvancedPositionKeyframeList.java | 44 ++++++++++++------- .../SpectatorDataInterpolation.java | 30 +++++++------ 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/src/main/java/eu/crushedpixel/replaymod/interpolation/AdvancedPositionKeyframeList.java b/src/main/java/eu/crushedpixel/replaymod/interpolation/AdvancedPositionKeyframeList.java index 77c2c02f..79251311 100644 --- a/src/main/java/eu/crushedpixel/replaymod/interpolation/AdvancedPositionKeyframeList.java +++ b/src/main/java/eu/crushedpixel/replaymod/interpolation/AdvancedPositionKeyframeList.java @@ -3,16 +3,17 @@ package eu.crushedpixel.replaymod.interpolation; import eu.crushedpixel.replaymod.holders.AdvancedPosition; import eu.crushedpixel.replaymod.holders.Keyframe; import eu.crushedpixel.replaymod.holders.SpectatorData; +import eu.crushedpixel.replaymod.holders.TimestampValue; +import eu.crushedpixel.replaymod.replay.ReplayHandler; import lombok.NoArgsConstructor; -import java.util.HashSet; -import java.util.ListIterator; -import java.util.Set; +import java.util.*; +import java.util.Map.Entry; @NoArgsConstructor public class AdvancedPositionKeyframeList extends KeyframeList { - private Set spectatorDataInterpolators; + private Map, SpectatorDataInterpolation> spectatorDataInterpolators; @Override public AdvancedPosition getInterpolatedValueForPathPosition(float pathPosition, boolean linear) { @@ -26,17 +27,16 @@ public class AdvancedPositionKeyframeList extends KeyframeList recalculate(linear); } - int keyframeIndex = (int)(pathPosition/size()); - float percentage = (pathPosition/size() - keyframeIndex)/(1f/size()); + int keyframeIndex = (int)Math.min(size()-1, pathPosition*(size()-1)); + float remainder = (pathPosition - ((float)keyframeIndex/(size()-1))); + float partial = remainder / (1f/(size()-1)); + AdvancedPosition current = get(keyframeIndex).getValue(); SpectatorDataInterpolation dataInterpolation = null; if(current instanceof SpectatorData) { - for(SpectatorDataInterpolation interpolation : spectatorDataInterpolators) { - if(interpolation.contains((SpectatorData)current)) { - dataInterpolation = interpolation; - break; - } + for(Entry, SpectatorDataInterpolation> entry : spectatorDataInterpolators.entrySet()) { + if(entry.getKey().contains(keyframeIndex)) dataInterpolation = entry.getValue(); } } @@ -45,9 +45,10 @@ public class AdvancedPositionKeyframeList extends KeyframeList } else { SpectatorData spectatorData = (SpectatorData)current; int index = dataInterpolation.indexOf(spectatorData); - int size = dataInterpolation.size(); + int size = dataInterpolation.size()-1; float one = 1f/size; - dataInterpolation.applyPoint((index/size)+(percentage*one), toApply); + float pathPos = (index/(float)size)+(partial*one); + dataInterpolation.applyPoint(pathPos, toApply); } return toApply; @@ -68,7 +69,7 @@ public class AdvancedPositionKeyframeList extends KeyframeList interpolation.prepare(); - spectatorDataInterpolators = new HashSet(); + spectatorDataInterpolators = new HashMap, SpectatorDataInterpolation>(); //find subsequent Spectator Keyframes with the same Entity ID ListIterator> iterator = this.listIterator(); @@ -77,13 +78,18 @@ public class AdvancedPositionKeyframeList extends KeyframeList SpectatorDataInterpolation spectatorInterpolation = null; boolean found = keyframe.getValue() instanceof SpectatorData; + int first = iterator.nextIndex()-1; + int firstTimestamp = -1; while(found) { if(spectatorInterpolation == null) { spectatorInterpolation = new SpectatorDataInterpolation(linear); } - spectatorInterpolation.addPoint((SpectatorData)keyframe.getValue(), keyframe.getRealTimestamp()); - + int keyframeTimestamp = keyframe.getRealTimestamp(); + TimestampValue timestampValue = ReplayHandler.getTimeKeyframes().getInterpolatedValueForTimestamp(keyframeTimestamp, true); + int replayTimestamp = timestampValue == null ? 0 : timestampValue.asInt(); + if(firstTimestamp == -1) firstTimestamp = replayTimestamp; + spectatorInterpolation.addPoint((SpectatorData)keyframe.getValue(), replayTimestamp); if(iterator.hasNext()) { keyframe = iterator.next(); found = keyframe.getValue() instanceof SpectatorData; @@ -94,7 +100,11 @@ public class AdvancedPositionKeyframeList extends KeyframeList if(spectatorInterpolation != null && spectatorInterpolation.size() > 1) { spectatorInterpolation.prepare(); - spectatorDataInterpolators.add(spectatorInterpolation); + List keys = new ArrayList(); + for(int i=first; i(); int i = 0; - int size = points.size(); + int firstTimestamp = -1; + int size = points.size()-1; for(Pair pair : points) { int timestamp = pair.getKey(); + if(firstTimestamp == -1) firstTimestamp = timestamp; + int currentTimestamp = timestamp; int nextTimestamp = timestamp; @@ -79,12 +84,13 @@ public class SpectatorDataInterpolation { int difference = nextTimestamp - timestamp; int smoothness = 0; - while(currentTimestamp + smoothness < nextTimestamp) { + while(currentTimestamp + smoothness <= nextTimestamp) { currentTimestamp += smoothness; SpectatorDataThirdPersonInfo thirdPersonInfo = new SpectatorDataThirdPersonInfo(); float percentage = (float)i/size; percentage += ((currentTimestamp-timestamp)/(float)difference) * 1f/size; + if(Float.isNaN(percentage)) percentage = 1; thirdPersonInfoInterpolation.applyPoint(percentage, thirdPersonInfo); @@ -100,20 +106,17 @@ public class SpectatorDataInterpolation { //next, move the camera point to fulfill the specified distance to the entity entityPosition = entityPosition.getDestination(-1 * thirdPersonInfo.shoulderCamDistance); - underlyingKeyframes.add(new Keyframe(currentTimestamp, entityPosition)); + underlyingKeyframes.add(new Keyframe(currentTimestamp-firstTimestamp, entityPosition)); } i++; } - - underlyingKeyframes.recalculate(linear); } public void applyPoint(float position, AdvancedPosition toEdit) { - int keyframeIndex = (int)position*points.size(); - - //the progress between this and the next keyframe (between 0 and 1) - float partial = (position - ((float)keyframeIndex/points.size())) / (1f/points.size()); + int keyframeIndex = (int)Math.min(size()-1, position*(size()-1)); + float remainder = (position - ((float)keyframeIndex/(size()-1))); + float partial = remainder / (1f/(size()-1)); Pair pair = points.get(keyframeIndex); Pair next = keyframeIndex < points.size()-1 ? points.get(keyframeIndex+1) : null; @@ -140,8 +143,7 @@ public class SpectatorDataInterpolation { int interpolatedTimestamp = firstTimestamp+(int)(partial*diff); AdvancedPosition pos = ReplayHandler.getEntityPositionTracker().getEntityPositionAtTimestamp(entityID, interpolatedTimestamp); - if(pos != null) - toEdit.apply(pos); + if(pos != null) toEdit.apply(pos); } else { toEdit.apply(underlyingKeyframes.getInterpolatedValueForPathPosition(position, linear)); }