Skip to content

Commit

Permalink
begin working on Animation
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 8, 2024
1 parent 7bdef07 commit 9a5d01e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions animate/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.hsgamer</groupId>
<artifactId>hscore</artifactId>
<version>4.4.4-SNAPSHOT</version>
</parent>

<artifactId>hscore-animate</artifactId>
</project>
43 changes: 43 additions & 0 deletions animate/src/main/java/me/hsgamer/hscore/animate/Animation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package me.hsgamer.hscore.animate;

import java.util.List;

public class Animation<T> {
private final List<T> frames;
private final long periodMillis;
private Long startMillis;

public Animation(List<T> frames, long periodMillis) {
if (frames.isEmpty()) {
throw new IllegalArgumentException("Frames cannot be empty");
}
if (periodMillis <= 0) {
throw new IllegalArgumentException("Period must be positive");
}

this.frames = frames;
this.periodMillis = periodMillis;
}

public List<T> getFrames() {
return frames;
}

public T getFrame(int index) {
return frames.get(index);
}

public T getCurrentFrame() {
long currentMillis = System.currentTimeMillis();
if (startMillis == null) {
startMillis = currentMillis;
}
long diff = currentMillis - startMillis;
int index = (int) (diff / periodMillis) % frames.size();
return frames.get(index);
}

public void reset() {
startMillis = null;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<module>minestom</module>
<module>license</module>
<module>action</module>
<module>animate</module>
</modules>

<properties>
Expand Down

0 comments on commit 9a5d01e

Please sign in to comment.