-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from fastjengine/images-sprites-animations
Images, Sprites, Animations, and a lot more Additions - (#104) added Linux's X11 to list of supported hardware accelerations - (#108) Added Sprite2D and simple animation system - Manages an array of sprites, as well as an image resource instance - has default animations for "continuous", "play to end", and "static" (default is "continuous") - (#113) Added centralized, extensible resource manager - (#108, #113) Added image resource manager implementation - (#108) Added `ImageUtil` to load and manage buffered images (makes use of resource manager where needed) - (#113) Added default resource management, loaded on `FastJEngine` static initialization (_before_ `FastJEngine.init`!) - (#10, #110) added `TexturePaint` builder, and support for `TexturePaint` in `.psdf` files - (#10) Added support for writing/parsing `Model2D` to the `.obj`/`.mtl` file format Bug Fixes - (Fixes #106) Fixed translation doubling by removing translation from `setMetrics` method calls - (Fixes #114) Added missing call to `Scene.reset` for each scene in `SceneManager` during a call to `SceneManager.reset`. - (Fixes #115) Added calls to destroy `Drawable`s in `Scene`/`SimpleManager` - (Fixes #86) Fixed issue where occasional `ConcurrentModificationException`s woulc crop up thanks to slight issues with consistent multithreaded/event-based inputs (keyboard, mouse, window, etc). Breaking Changes - Moved `tech.fastj.systems.fio.FileUtil` to `tech.fastj.resources.files.FileUtil` - Moved `tech.fastj.graphics.util.ModelUtil` to `tech.fastj.resources.models.ModelUtil` - Moved `tech.fastj.graphics.io.PsdfUtil` to `tech.fastj.resources.models.PsdfUtil` - Moved `tech.fastj.graphics.io.SupportedModelFormats` to `tech.fastj.resources.models.SupportedModelFormats`
- Loading branch information
Showing
39 changed files
with
1,517 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package tech.fastj.graphics.game; | ||
|
||
public enum AnimationStyle { | ||
ContinuousLoop, | ||
Static, | ||
PlayUntilEnd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package tech.fastj.graphics.game; | ||
|
||
import tech.fastj.graphics.util.DrawUtil; | ||
|
||
import tech.fastj.resources.images.ImageResource; | ||
import tech.fastj.resources.images.ImageUtil; | ||
import tech.fastj.systems.control.Scene; | ||
import tech.fastj.systems.control.SimpleManager; | ||
|
||
import java.awt.Graphics2D; | ||
import java.awt.geom.AffineTransform; | ||
import java.awt.image.BufferedImage; | ||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class Sprite2D extends GameObject { | ||
|
||
public static final int DefaultStartingFrame = 0; | ||
public static final int DefaultAnimationFPS = 12; | ||
public static final AnimationStyle DefaultAnimationStyle = AnimationStyle.ContinuousLoop; | ||
public static final int DefaultHorizontalImageCount = 1; | ||
public static final int DefaultVerticalImageCount = 1; | ||
|
||
private ImageResource spritesResource; | ||
private BufferedImage[] sprites; | ||
private int currentFrame; | ||
private int animationFPS = DefaultAnimationFPS; | ||
private AnimationStyle animationStyle; | ||
|
||
private ScheduledExecutorService spriteAnimator; | ||
|
||
Sprite2D(ImageResource spritesResource, int horizontalImageCount, int verticalImageCount) { | ||
this.spritesResource = spritesResource; | ||
setCollisionPath(DrawUtil.createPath(DrawUtil.createBoxFromImage(sprites[0]))); | ||
resetSpriteSheet(horizontalImageCount, verticalImageCount); | ||
resetSpriteAnimator(); | ||
} | ||
|
||
public static Sprite2DBuilder create(ImageResource spritesResource) { | ||
return new Sprite2DBuilder(spritesResource); | ||
} | ||
|
||
public static Sprite2D fromImageResource(ImageResource spritesResource) { | ||
return new Sprite2DBuilder(spritesResource).build(); | ||
} | ||
|
||
public void changeSpriteResource(ImageResource spritesResource, int horizontalImageCount, int verticalImageCount) { | ||
this.spritesResource = spritesResource; | ||
resetSpriteSheet(horizontalImageCount, verticalImageCount); | ||
} | ||
|
||
public int getCurrentFrame() { | ||
return currentFrame; | ||
} | ||
|
||
public int getAnimationFPS() { | ||
return animationFPS; | ||
} | ||
|
||
public AnimationStyle getAnimationStyle() { | ||
return animationStyle; | ||
} | ||
|
||
public Sprite2D setCurrentFrame(int currentFrame) { | ||
this.currentFrame = currentFrame; | ||
return this; | ||
} | ||
|
||
public Sprite2D setAnimationFPS(int animationFPS) { | ||
this.animationFPS = animationFPS; | ||
resetSpriteAnimator(); | ||
return this; | ||
} | ||
|
||
public Sprite2D setAnimationStyle(AnimationStyle animationStyle) { | ||
this.animationStyle = animationStyle; | ||
return this; | ||
} | ||
|
||
@Override | ||
public void render(Graphics2D g) { | ||
if (!shouldRender()) { | ||
return; | ||
} | ||
|
||
AffineTransform oldTransform = (AffineTransform) g.getTransform().clone(); | ||
g.transform(getTransformation()); | ||
|
||
g.drawImage(sprites[currentFrame], null, null); | ||
|
||
g.setTransform(oldTransform); | ||
} | ||
|
||
@Override | ||
public void destroy(Scene origin) { | ||
spriteAnimator.shutdownNow(); | ||
spriteAnimator = null; | ||
sprites = null; | ||
currentFrame = -1; | ||
animationFPS = -1; | ||
animationStyle = null; | ||
|
||
super.destroyTheRest(origin); | ||
} | ||
|
||
@Override | ||
public void destroy(SimpleManager origin) { | ||
spriteAnimator.shutdownNow(); | ||
sprites = null; | ||
currentFrame = -1; | ||
animationFPS = -1; | ||
animationStyle = null; | ||
|
||
super.destroyTheRest(origin); | ||
} | ||
|
||
private void resetSpriteSheet(int horizontalImageCount, int verticalImageCount) { | ||
sprites = ImageUtil.createSpriteSheet(this.spritesResource.get(), horizontalImageCount, verticalImageCount); | ||
} | ||
|
||
private void resetSpriteAnimator() { | ||
if (spriteAnimator != null) { | ||
spriteAnimator.shutdownNow(); | ||
spriteAnimator = null; | ||
} | ||
|
||
spriteAnimator = Executors.newSingleThreadScheduledExecutor(); | ||
spriteAnimator.scheduleAtFixedRate( | ||
() -> { | ||
switch (animationStyle) { | ||
case Static: { | ||
break; | ||
} | ||
case ContinuousLoop: { | ||
currentFrame++; | ||
if (currentFrame == sprites.length) { | ||
currentFrame = 0; | ||
} | ||
break; | ||
} | ||
case PlayUntilEnd: { | ||
if (currentFrame < sprites.length - 1) { | ||
currentFrame++; | ||
} | ||
break; | ||
} | ||
} | ||
}, | ||
1000 / animationFPS, | ||
1000 / animationFPS, | ||
TimeUnit.MILLISECONDS | ||
); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
if (other == null || getClass() != other.getClass()) { | ||
return false; | ||
} | ||
if (!super.equals(other)) { | ||
return false; | ||
} | ||
Sprite2D sprite2D = (Sprite2D) other; | ||
return currentFrame == sprite2D.currentFrame | ||
&& animationFPS == sprite2D.animationFPS | ||
&& animationStyle == sprite2D.animationStyle | ||
&& Arrays.equals(sprites, sprite2D.sprites); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = Objects.hash(currentFrame, animationFPS, animationStyle); | ||
result = 31 * result + Arrays.hashCode(sprites); | ||
return result; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Sprite2D{" + | ||
"sprites=" + Arrays.toString(sprites) + | ||
", currentFrame=" + currentFrame + | ||
", animationFPS=" + animationFPS + | ||
", animationStyle=" + animationStyle + | ||
'}'; | ||
} | ||
} |
Oops, something went wrong.