Implement Animated Sprites

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent 2ae7c9dc19
commit 8ba9307465
  1. BIN
      maps/world1.map
  2. BIN
      sprites/erinoah.gif
  3. 23
      src/sig/DrawLoop.java
  4. 2
      src/sig/RabiClone.java
  5. 44
      src/sig/engine/AnimatedObject.java
  6. 35
      src/sig/engine/AnimatedSprite.java
  7. 12
      src/sig/engine/Object.java
  8. 8
      src/sig/engine/Rectangle.java
  9. 1
      src/sig/engine/Sprite.java
  10. 4
      src/sig/objects/EditorRenderer.java
  11. 23
      src/sig/objects/Erinoah.java
  12. 2
      src/sig/objects/LevelRenderer.java

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -3,9 +3,11 @@ package sig;
import java.util.regex.Pattern;
import sig.engine.Alpha;
import sig.engine.AnimatedSprite;
import sig.engine.Font;
import sig.engine.PaletteColor;
import sig.engine.Panel;
import sig.engine.Rectangle;
import sig.engine.Sprite;
public class DrawLoop {
@ -59,17 +61,22 @@ public class DrawLoop {
yOffset+=f.getGlyphHeight();
charCount=0;
} else {
Draw_Sprite_Partial_Ext(x+i*f.getGlyphWidth()-xOffset, y+yOffset, f.getCharInfo(finalS.charAt(i)).getX(), f.getCharInfo(finalS.charAt(i)).getY(), f.getCharInfo(finalS.charAt(i)).getWidth(), f.getCharInfo(finalS.charAt(i)).getHeight(), f.getSprite(),alpha,currentCol);
Draw_Sprite_Partial_Ext(x+i*f.getGlyphWidth()-xOffset, y+yOffset, f.getCharInfo(finalS.charAt(i)).getX(), f.getCharInfo(finalS.charAt(i)).getY(), f.getCharInfo(finalS.charAt(i)).getWidth(), f.getCharInfo(finalS.charAt(i)).getHeight(), f.getSprite(), 0,alpha,currentCol);
charCount++;
}
}
}
public static void Draw_Sprite(double x, double y, Sprite sprite){
Draw_Sprite_Partial(x,y,0,0,sprite.getWidth(),sprite.getHeight(),sprite);
Draw_Sprite_Partial(x,y,0,0,sprite.getWidth(),sprite.getHeight(),sprite,0);
}
public static void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite){
public static void Draw_Animated_Sprite(double x, double y, AnimatedSprite sprite, double frameIndex){
Rectangle frameRectangle=sprite.getFrame((int)frameIndex);
Draw_Sprite_Partial(x,y,frameRectangle.getX(),frameRectangle.getY(),frameRectangle.getWidth(),frameRectangle.getHeight(),sprite,frameIndex);
}
public static void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, double frame_index){
byte[] p = panel.pixel;
for(int X=(int)xOffset;X<(int)(w+xOffset);X++){
for(int Y=(int)yOffset;Y<(int)(h+yOffset);Y++){
@ -81,7 +88,6 @@ public class DrawLoop {
continue;
} else {
Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X],Alpha.ALPHA0);
//Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X],false);
}
}
}
@ -89,10 +95,15 @@ public class DrawLoop {
}
public static void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, Alpha alpha){
Draw_Sprite_Partial_Ext(x, y, xOffset, yOffset, w, h, sprite, alpha, PaletteColor.NORMAL);
Draw_Sprite_Partial_Ext(x, y, xOffset, yOffset, w, h, sprite, 0, alpha, PaletteColor.NORMAL);
}
public static void Draw_Animated_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, AnimatedSprite sprite, double frameIndex, Alpha alpha){
Rectangle frameRectangle=sprite.getFrame((int)frameIndex);
Draw_Sprite_Partial_Ext(x, y, frameRectangle.getX(), frameRectangle.getY(), frameRectangle.getWidth(), frameRectangle.getHeight(), sprite, 0, alpha, PaletteColor.NORMAL);
}
public static void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, Alpha alpha, PaletteColor col){
public static void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, double frame_index, Alpha alpha, PaletteColor col){
byte[] p = panel.pixel;
for(int X=(int)xOffset;X<(int)(w+xOffset);X++){
for(int Y=(int)yOffset;Y<(int)(h+yOffset);Y++){

@ -9,6 +9,7 @@ import sig.engine.Panel;
import sig.engine.Point;
import sig.map.Maps;
import sig.objects.EditorRenderer;
import sig.objects.Erinoah;
import sig.objects.LevelRenderer;
import sig.objects.Player;
import sig.engine.Object;
@ -94,6 +95,7 @@ public class RabiClone{
private static void StartGame() {
OBJ.add(player = new Player(p));
OBJ.add(new Erinoah(p));
}
private static void ChooseBestRatio() {

@ -0,0 +1,44 @@
package sig.engine;
public abstract class AnimatedObject extends Object{
double currentFrame;
double animationSpd;
AnimatedSprite animatedSpr;
protected AnimatedObject(AnimatedSprite spr, double animationSpd, Panel panel) {
super(panel);
this.spr=this.animatedSpr=spr;
this.animationSpd=animationSpd;
this.currentFrame=0;
}
public void update(double updateMult) {
this.currentFrame+=this.animationSpd*updateMult;
}
public double getCurrentFrame() {
return currentFrame;
}
public void setCurrentFrame(double currentFrame) {
this.currentFrame = currentFrame;
}
public double getAnimationSpd() {
return animationSpd;
}
public void setAnimationSpd(double animationSpd) {
this.animationSpd = animationSpd;
}
public AnimatedSprite getAnimatedSpr() {
return animatedSpr;
}
public void setAnimatedSpr(AnimatedSprite animatedSpr) {
this.animatedSpr = animatedSpr;
}
}

@ -0,0 +1,35 @@
package sig.engine;
import java.io.File;
public class AnimatedSprite extends Sprite{
int frameWidth;
int frameHeight;
int frames;
Rectangle[] frames_to_Rectangle;
AnimatedSprite(File filename, int width, int height){
super(filename);
this.frameWidth = width;
this.frameHeight = height;
frames = (this.width/width)*(this.height/height);
frames_to_Rectangle = new Rectangle[frames];
for(int i=0;i<frames;i++){
int x_tile = i%(this.width/width);
int y_tile = i/(this.width/width);
frames_to_Rectangle[i] = new Rectangle(x_tile*width, y_tile*height, width, height);
}
}
public Rectangle getFrame(int index){
return frames_to_Rectangle[index%frames];
}
public int getFrame_count(){
return frames;
}
public int get_original_width(){
return this.width;
}
public int get_original_height(){
return this.height;
}
}

@ -61,6 +61,10 @@ public abstract class Object implements GameEntity{
DrawLoop.Draw_Sprite(x,y,sprite);
}
protected void Draw_Animated_Sprite(double x, double y, AnimatedSprite sprite, double frameIndex){
DrawLoop.Draw_Animated_Sprite(x,y,sprite,frameIndex);
}
protected void Draw_Text(double x, double y, StringBuilder string, Font font){
DrawLoop.Draw_Text(x,y,string,font);
}
@ -69,16 +73,16 @@ public abstract class Object implements GameEntity{
DrawLoop.Draw_Text_Ext(x,y,string,font,alpha,col);
}
protected void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite){
DrawLoop.Draw_Sprite_Partial(x,y,xOffset,yOffset,w,h,sprite);
protected void Draw_Sprite_Partial(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, double frame_index){
DrawLoop.Draw_Sprite_Partial(x,y,xOffset,yOffset,w,h,sprite,frame_index);
}
protected void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, Alpha alpha){
DrawLoop.Draw_Sprite_Partial_Ext(x,y,xOffset,yOffset,w,h,sprite,alpha);
}
protected void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, Alpha alpha, PaletteColor col){
DrawLoop.Draw_Sprite_Partial_Ext(x,y,xOffset,yOffset,w,h,sprite,alpha,col);
protected void Draw_Sprite_Partial_Ext(double x, double y, double xOffset, double yOffset, double w, double h, Sprite sprite, double frame_index, Alpha alpha, PaletteColor col){
DrawLoop.Draw_Sprite_Partial_Ext(x,y,xOffset,yOffset,w,h,sprite,frame_index,alpha,col);
}
protected boolean KeyHeld(int key) {

@ -42,4 +42,12 @@ public class Rectangle {
this.h = h;
}
@Override
public String toString() {
return new StringBuilder("Rectangle(x=").append(x).append(",")
.append("y=").append(y).append(",")
.append("w=").append(w).append(",")
.append("h=").append(h).append(")").toString();
}
}

@ -17,6 +17,7 @@ public class Sprite{
public static Sprite BACKGROUND1 = new Sprite(new File(new File("..","backgrounds"),"back1.gif"));
public static Sprite BACKGROUND2 = new Sprite(new File(new File("..","backgrounds"),"back2.gif"));
public static Sprite BACKGROUND3 = new Sprite(new File(new File("..","backgrounds"),"back3.gif"));
public static AnimatedSprite ERINOAH = new AnimatedSprite(new File(new File("..","sprites"),"erinoah.gif"),48,48);

@ -55,10 +55,10 @@ public class EditorRenderer extends LevelRenderer{
int up = KeyHeld(KeyEvent.VK_UP)||KeyHeld(KeyEvent.VK_W)?1:0;
int down = KeyHeld(KeyEvent.VK_DOWN)||KeyHeld(KeyEvent.VK_S)?1:0;
if (right-left!=0) {
setX(getX()+(right-left)*CAMERA_SPD*updateMult);
setX(Math.max(0,getX()+(right-left)*CAMERA_SPD*updateMult));
}
if (up-down!=0) {
setY(getY()+(down-up)*CAMERA_SPD*updateMult);
setY(Math.max(0,getY()+(down-up)*CAMERA_SPD*updateMult));
}
boolean left_mb = MouseHeld(MouseEvent.BUTTON1);
boolean middle_mb = MouseHeld(MouseEvent.BUTTON2);

@ -0,0 +1,23 @@
package sig.objects;
import sig.engine.AnimatedObject;
import sig.engine.Panel;
import sig.engine.Sprite;
public class Erinoah extends AnimatedObject{
public Erinoah(Panel panel) {
super(Sprite.ERINOAH, 4, panel);
}
@Override
public void update(double updateMult) {
super.update(updateMult);
}
@Override
public void draw(byte[] p) {
Draw_Animated_Sprite(16, 16, getAnimatedSpr(), getCurrentFrame());
}
}

@ -66,7 +66,7 @@ public class LevelRenderer extends Object{
}
private void DrawTile(double x, double y, Tile tile) {
Draw_Sprite_Partial(x,y, tile.getSpriteSheetX()*tile.getTileWidth(), tile.getSpriteSheetY()*tile.getTileHeight(), tile.getTileWidth(), tile.getTileHeight(), getSprite());
Draw_Sprite_Partial(x,y, tile.getSpriteSheetX()*tile.getTileWidth(), tile.getSpriteSheetY()*tile.getTileHeight(), tile.getTileWidth(), tile.getTileHeight(), getSprite(), 0);
}
protected void DrawTransparentTile(double x, double y, Tile tile, Alpha alpha) {

Loading…
Cancel
Save