Skip to content

Commit

Permalink
Finishing sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCheung0422 committed Aug 17, 2022
1 parent 171872e commit 34c4381
Show file tree
Hide file tree
Showing 87 changed files with 1,014 additions and 951 deletions.
19 changes: 9 additions & 10 deletions src/main/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import minicraft.gfx.Ellipsis;
import minicraft.gfx.Font;
import minicraft.gfx.FontStyle;
import minicraft.gfx.MinicraftImage;
import minicraft.gfx.Point;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker;
import minicraft.gfx.SpriteSheet;
import minicraft.item.Items;
import minicraft.item.PotionType;
import minicraft.item.ToolItem;
Expand Down Expand Up @@ -76,11 +76,11 @@ private Renderer() {}

private static LinkedSprite hudSheet;

public static SpriteSheet loadDefaultSkinSheet() {
SpriteSheet skinsSheet;
public static MinicraftImage loadDefaultSkinSheet() {
MinicraftImage skinsSheet;
try {
// These set the sprites to be used.
skinsSheet = new SpriteSheet(ImageIO.read(Objects.requireNonNull(Game.class.getResourceAsStream("/resources/textures/skins.png"))));
skinsSheet = new MinicraftImage(ImageIO.read(Objects.requireNonNull(Game.class.getResourceAsStream("/resources/textures/skins.png"))));
} catch (NullPointerException e) {
// If a provided InputStream has no name. (in practice meaning it cannot be found.)
CrashHandler.crashHandle(e, new ErrorInfo("Sprite Sheet Not Found", ErrorInfo.ErrorType.UNEXPECTED, true, "A sprite sheet was not found."));
Expand All @@ -97,9 +97,8 @@ public static SpriteSheet loadDefaultSkinSheet() {
public static void initScreen() {
ResourcePackDisplay.initPacks();
ResourcePackDisplay.reloadResources();
SpriteSheet sheet = loadDefaultSkinSheet();
screen = new Screen(sheet);
lightScreen = new Screen(sheet);
screen = new Screen();
lightScreen = new Screen();

image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
screen.pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Expand Down Expand Up @@ -194,11 +193,11 @@ private static void renderLevel() {
if (xScroll > level.w * 16 - Screen.w) xScroll = level.w * 16 - Screen.w; // ...Right border.
if (yScroll > level.h * 16 - Screen.h) yScroll = level.h * 16 - Screen.h; // ...Bottom border.
if (currentLevel > 3) { // If the current level is higher than 3 (which only the sky level (and dungeon) is)
SpriteSheet cloud = spriteLinker.getSpriteSheet(SpriteType.Tile, "cloud");
MinicraftImage cloud = spriteLinker.getSheet(SpriteType.Tile, "cloud_background");
for (int y = 0; y < 28; y++)
for (int x = 0; x < 48; x++) {
// Creates the background for the sky (and dungeon) level:
screen.render(x * 8 - ((xScroll / 4) & 7), y * 8 - ((yScroll / 4) & 7), 2, 3, 0, cloud);
screen.render(x * 8 - ((xScroll / 4) & 7), y * 8 - ((yScroll / 4) & 7), 0, 0, 0, cloud);
}
}

Expand Down Expand Up @@ -351,7 +350,7 @@ private static void renderGui() {
// Renders armor
int armor = player.armor * Player.maxStat / Player.maxArmor;
if (i <= armor && player.curArmor != null) {
player.curArmor.sprite.getSprite().render(screen, i * 8, Screen.h - 24);
screen.render(i * 8, Screen.h - 24, player.curArmor.sprite);
}

// Renders your current red hearts, or black hearts for damaged health.
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/minicraft/core/io/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

import org.jetbrains.annotations.NotNull;

import minicraft.core.CrashHandler;
import minicraft.core.Game;
import minicraft.util.Logging;

import org.json.JSONException;
import org.json.JSONObject;
import org.tinylog.Logger;

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/minicraft/entity/Arrow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import java.util.List;

import minicraft.core.Renderer;
import javax.security.auth.DestroyFailedException;

import minicraft.entity.mob.Mob;
import minicraft.entity.mob.Player;
import minicraft.gfx.Color;
import minicraft.gfx.Rectangle;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.util.Logging;

public class Arrow extends Entity implements ClientTickable {
private Direction dir;
private int damage;
public Mob owner;
private int speed;
private LinkedSprite sprite = new LinkedSprite(SpriteType.Entity, "arrow").setSpriteSize(1, 1);

public Arrow(Mob owner, Direction dir, int dmg) {
this(owner, owner.x, owner.y, dir, dmg);
Expand All @@ -29,6 +33,12 @@ public Arrow(Mob owner, int x, int y, Direction dir, int dmg) {
damage = dmg;
col = Color.get(-1, 111, 222, 430);

int xt = 0;
if(dir == Direction.LEFT) xt = 1;
if(dir == Direction.UP) xt = 2;
if(dir == Direction.DOWN) xt = 3;
sprite.setSpritePos(xt, 0);

if (damage > 3) speed = 8;
else if (damage >= 0) speed = 7;
else speed = 6;
Expand Down Expand Up @@ -67,6 +77,11 @@ public void tick() {
&& !level.getTile(x / 16, y / 16).connectsToFluid
&& level.getTile(x / 16, y / 16).id != 16) {
this.remove();
try {
sprite.destroy();
} catch (DestroyFailedException e) {
Logging.SPRITE.trace(e);
}
}
}
}
Expand All @@ -77,12 +92,6 @@ public boolean isSolid() {

@Override
public void render(Screen screen) {
int xt = 0;

if(dir == Direction.LEFT) xt = 1;
if(dir == Direction.UP) xt = 2;
if(dir == Direction.DOWN) xt = 3;

screen.render(x - 4, y - 4, xt, 0, 0, Renderer.spriteLinker.getSpriteSheet(SpriteType.Entity, "arrow"));
screen.render(x - 4, y - 4, sprite);
}
}
7 changes: 4 additions & 3 deletions src/main/java/minicraft/entity/ItemEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ItemEntity extends Entity implements ClientTickable {
// Solely for multiplayer use.
private boolean pickedUp = false;
private long pickupTimestamp;

/**
* Creates an item entity of the item item at position (x,y) with size 2*2.
* @param item Item to add as item entity
Expand Down Expand Up @@ -127,8 +127,9 @@ public void render(Screen screen) {
if (time >= lifeTime - 6 * 20) {
if (time / 6 % 2 == 0) return;
}
item.sprite.getSprite().render(screen, x-4, y - 4, 4, -1, Color.get(0, 31)); // Item shadow
item.sprite.getSprite().render(screen, x - 4, y - 4 - (int)(zz)); // Item

screen.render(x-4, y - 4, item.sprite.getSprite(), 0, false, Color.get(0, 31)); // Item shadow
screen.render(x - 4, y - 4 - (int) zz, item.sprite); // Item
}

@Override
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/minicraft/entity/Spark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import java.util.List;

import minicraft.core.Game;
import minicraft.core.Renderer;
import minicraft.entity.mob.AirWizard;
import minicraft.entity.mob.Mob;
import minicraft.gfx.Color;
import minicraft.gfx.Rectangle;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;

public class Spark extends Entity {
Expand All @@ -17,6 +17,7 @@ public class Spark extends Entity {
private double xx, yy; // The x and y positions
private int time; // The amount of time that has passed
private final AirWizard owner; // The AirWizard that created this spark
private LinkedSprite sprite = new LinkedSprite(SpriteType.Entity, "spark");

/**
* Creates a new spark. Owner is the AirWizard which is spawning this spark.
Expand Down Expand Up @@ -75,9 +76,10 @@ public void render(Screen screen) {

randmirror = random.nextInt(4);
}

screen.render(x - 4, y - 4 + 2, 0, 0, randmirror, Renderer.spriteLinker.getSpriteSheet(SpriteType.Entity, "spark"), -1, false, Color.BLACK); // renders the shadow on the ground
screen.render(x - 4, y - 4 - 2, 0, 0, randmirror, Renderer.spriteLinker.getSpriteSheet(SpriteType.Entity, "spark")); // Renders the spark

sprite.setMirror(randmirror);
screen.render(x - 4, y - 4 + 2, sprite.getSprite(), 0, false, Color.BLACK); // renders the shadow on the ground
screen.render(x - 4, y - 4 - 2, sprite); // Renders the spark
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/minicraft/entity/furniture/Furniture.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void tick() {
}

/** Draws the furniture on the screen. */
public void render(Screen screen) { sprite.getSprite().render(screen, x-8, y-8); }
public void render(Screen screen) { screen.render(x-8, y-8, sprite); }

/** Called when the player presses the MENU key in front of this. */
public boolean use(Player player) { return false; }
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/minicraft/entity/mob/AirWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
import minicraft.gfx.Font;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.network.Analytics;
import minicraft.screen.AchievementsDisplay;

public class AirWizard extends EnemyMob {
private static final LinkedSprite[] sprites = new LinkedSprite[] {
new LinkedSprite(SpriteType.Entity, "air_wizard").setSpritePos(0, 0),
new LinkedSprite(SpriteType.Entity, "air_wizard").setSpritePos(0, 2)
private static final LinkedSprite[][][] sprites = new LinkedSprite[][][] {
Mob.compileMobSpriteAnimations(0, 0, "air_wizard"),
Mob.compileMobSpriteAnimations(0, 2, "air_wizard")
};

public static boolean beaten = false;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/minicraft/entity/mob/Cow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import minicraft.core.io.Settings;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.Items;

public class Cow extends PassiveMob {
private static LinkedSprite sprites = new LinkedSprite(SpriteType.Entity, "cow");
private static LinkedSprite[][] sprites = Mob.compileMobSpriteAnimations(0, 0, "cow");

/**
* Creates the cow with the right sprites and color.
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/minicraft/entity/mob/Creeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
import minicraft.gfx.Point;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.Items;
import minicraft.level.tile.Tiles;

import java.util.ArrayList;
import java.util.List;

public class Creeper extends EnemyMob {
private static LinkedSprite[] sprites = new LinkedSprite[] {
new LinkedSprite(SpriteType.Entity, "creeper").setSpriteDim(0, 0, 2, 2).setSpriteList(2),
new LinkedSprite(SpriteType.Entity, "creeper").setSpriteDim(0, 2, 2, 2).setSpriteList(2),
new LinkedSprite(SpriteType.Entity, "creeper").setSpriteDim(0, 4, 2, 2).setSpriteList(2),
new LinkedSprite(SpriteType.Entity, "creeper").setSpriteDim(0, 6, 2, 2).setSpriteList(2)
private static LinkedSprite[][][] sprites = new LinkedSprite[][][] {
new LinkedSprite[][] {Mob.compileSpriteList(0, 0, 2, 2, 0, 2, "creeper")},
new LinkedSprite[][] {Mob.compileSpriteList(0, 2, 2, 2, 0, 2, "creeper")},
new LinkedSprite[][] {Mob.compileSpriteList(0, 4, 2, 2, 0, 2, "creeper")},
new LinkedSprite[][] {Mob.compileSpriteList(0, 6, 2, 2, 0, 2, "creeper")}
};

private static final int MAX_FUSE_TIME = 60;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/minicraft/entity/mob/EnemyMob.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class EnemyMob extends MobAi {

public int lvl;
protected LinkedSprite[] lvlSprites;
protected LinkedSprite[][][] lvlSprites;
public int detectDist;

/**
Expand All @@ -30,7 +30,7 @@ public class EnemyMob extends MobAi {
* @param rwTime How long the mob will walk in a random direction. (random walk duration)
* @param rwChance The chance of this mob will walk in a random direction (random walk chance)
*/
public EnemyMob(int lvl, LinkedSprite[] lvlSprites, int health, boolean isFactor, int detectDist, int lifetime, int rwTime, int rwChance) {
public EnemyMob(int lvl, LinkedSprite[][][] lvlSprites, int health, boolean isFactor, int detectDist, int lifetime, int rwTime, int rwChance) {
super(lvlSprites[0], isFactor ? (lvl == 0 ? 1 : lvl * lvl) * health * ((Double)(Math.pow(2, Settings.getIdx("diff")))).intValue() : health, lifetime, rwTime, rwChance);
this.lvl = lvl == 0 ? 1 : lvl;
this.lvlSprites = java.util.Arrays.copyOf(lvlSprites, lvlSprites.length);
Expand All @@ -48,7 +48,7 @@ public EnemyMob(int lvl, LinkedSprite[] lvlSprites, int health, boolean isFactor
* @param rwTime How long the mob will walk in a random direction. (random walk duration)
* @param rwChance The chance of this mob will walk in a random direction (random walk chance)
*/
public EnemyMob(int lvl, LinkedSprite[] lvlSprites, int health, boolean isFactor, int detectDist, int rwTime, int rwChance) {
public EnemyMob(int lvl, LinkedSprite[][][] lvlSprites, int health, boolean isFactor, int detectDist, int rwTime, int rwChance) {
this(lvl, lvlSprites, health, isFactor, detectDist, 60 * Updater.normSpeed, rwTime, rwChance);
}

Expand All @@ -63,7 +63,7 @@ public EnemyMob(int lvl, LinkedSprite[] lvlSprites, int health, boolean isFactor
* @param health How much health the mob has.
* @param detectDist The distance where the mob will detect the player and start moving towards him/her.
*/
public EnemyMob(int lvl, LinkedSprite[] lvlSprites, int health, int detectDist) {
public EnemyMob(int lvl, LinkedSprite[][][] lvlSprites, int health, int detectDist) {
this(lvl, lvlSprites, health, true, detectDist, 60, 200);
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/minicraft/entity/mob/Knight.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import minicraft.core.io.Settings;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.Items;

public class Knight extends EnemyMob {
private static LinkedSprite[] sprites = new LinkedSprite[] {
new LinkedSprite(SpriteType.Entity, "creeper").setSpritePos(0, 0),
new LinkedSprite(SpriteType.Entity, "creeper").setSpritePos(0, 2),
new LinkedSprite(SpriteType.Entity, "creeper").setSpritePos(0, 4),
new LinkedSprite(SpriteType.Entity, "creeper").setSpritePos(0, 6)
private static LinkedSprite[][][] sprites = new LinkedSprite[][][] {
Mob.compileMobSpriteAnimations(0, 0, "knight"),
Mob.compileMobSpriteAnimations(0, 2, "knight"),
Mob.compileMobSpriteAnimations(0, 4, "knight"),
Mob.compileMobSpriteAnimations(0, 6, "knight")
};

/**
Expand Down
47 changes: 45 additions & 2 deletions src/main/java/minicraft/entity/mob/Mob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import minicraft.entity.particle.TextParticle;
import minicraft.gfx.Color;
import minicraft.gfx.SpriteLinker.LinkedSprite;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.PotionType;
import minicraft.level.tile.Tile;
import minicraft.level.tile.Tiles;

public abstract class Mob extends Entity {

protected LinkedSprite sprites; // This contains all the mob's sprites, sorted first by direction (index corresponding to the dir variable), and then by walk animation state.
protected LinkedSprite[][] sprites; // This contains all the mob's sprites, sorted first by direction (index corresponding to the dir variable), and then by walk animation state.
public int walkDist = 0; // How far we've walked currently, incremented after each movement. This is used to change the sprite; "(walkDist >> 3) & 1" switches between a value of 0 and 1 every 8 increments of walkDist.

public Direction dir = Direction.DOWN; // The direction the mob is facing, used in attacking and rendering. 0 is down, 1 is up, 2 is left, 3 is right
Expand All @@ -31,7 +32,7 @@ public abstract class Mob extends Entity {
* @param sprites All of this mob's sprites.
* @param health The mob's max health.
*/
public Mob(LinkedSprite sprites, int health) {
public Mob(LinkedSprite[][] sprites, int health) {
super(4, 3);
this.sprites = sprites;
this.health = this.maxHealth = health;
Expand Down Expand Up @@ -73,7 +74,9 @@ public void tick() {
private boolean move(int xd, int yd, boolean changeDir) { // Knockback shouldn't change mob direction
if (level == null) return false; // Stopped b/c there's no level to move in!

@SuppressWarnings("unused")
int oldxt = x >> 4;
@SuppressWarnings("unused")
int oldyt = y >> 4;

// These should return true b/c the mob is still technically moving; these are just to make it move *slower*.
Expand Down Expand Up @@ -103,6 +106,46 @@ private boolean move(int xd, int yd, boolean changeDir) { // Knockback shouldn't
return moved;
}

/** This is an easy way to make a list of sprites that are all part of the same "Sprite", so they have similar parameters, but they're just at different locations on the spreadsheet. */
public static LinkedSprite[] compileSpriteList(int sheetX, int sheetY, int width, int height, int mirror, int number, String key) {
LinkedSprite[] sprites = new LinkedSprite[number];
for (int i = 0; i < sprites.length; i++)
sprites[i] = new LinkedSprite(SpriteType.Entity, key).setSpriteDim(sheetX + width * i, sheetY, width, height)
.setMirror(mirror).setFlip(mirror);

return sprites;
}

public static LinkedSprite[][] compileMobSpriteAnimations(int sheetX, int sheetY, String key) {
LinkedSprite[][] sprites = new LinkedSprite[4][2];
// dir numbers: 0=down, 1=up, 2=left, 3=right.
/// On the spritesheet, most mobs have 4 sprites there, first facing down, then up, then right 1, then right 2. The first two get flipped to animate them, but the last two get flipped to change direction.

// Contents: down 1, up 1, right 1, right 2
LinkedSprite[] set1 = compileSpriteList(sheetX, sheetY, 2, 2, 0, 4, key);

// Contents: down 2, up 2, left 1, left 2
LinkedSprite[] set2 = compileSpriteList(sheetX, sheetY, 2, 2, 1, 4, key);

// Down
sprites[0][0] = set1[0];
sprites[0][1] = set2[0];

// Up
sprites[1][0] = set1[1];
sprites[1][1] = set2[1];

// Left
sprites[2][0] = set2[2];
sprites[2][1] = set2[3];

// Right
sprites[3][0] = set1[2];
sprites[3][1] = set1[3];

return sprites;
}

private boolean isWooling() { // supposed to walk at half speed on wool
if (level == null) return false;
Tile tile = level.getTile(x >> 4, y >> 4);
Expand Down
Loading

0 comments on commit 34c4381

Please sign in to comment.