Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tall Grass #636

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/client/java/minicraft/level/LevelGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import minicraft.core.io.Settings;
import minicraft.gfx.Rectangle;
import minicraft.level.tile.FlowerTile;
import minicraft.level.tile.TallGrassTile;
import minicraft.level.tile.Tiles;
import minicraft.screen.RelPos;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -423,14 +424,24 @@ private static short[][] createTopMap(int w, int h) { // Create surface map
for (int i = 0; i < w * h / 400; i++) {
int x = random.nextInt(w);
int y = random.nextInt(h);
int col = random.nextInt(4) * random.nextInt(4);
for (int j = 0; j < 30; j++) {
int xx = x + random.nextInt(5) - random.nextInt(5);
int yy = y + random.nextInt(5) - random.nextInt(5);
int col0 = random.nextInt(4);
int col1 = random.nextInt(4);
int col2 = random.nextInt(4) * random.nextInt(4);
int r = 25 + random.nextInt(5) * 5;
for (int j = 0; j < r; j++) {
int xx = x + random.nextInt(6) - random.nextInt(6);
int yy = y + random.nextInt(6) - random.nextInt(6);
int colRandom = random.nextInt(8);
int rCol = (colRandom & 0b100) != 0 ? col2 : (colRandom & 0b10) != 0 ? col1 : col0; // chances different
if (xx >= 0 && yy >= 0 && xx < w && yy < h) {
if (map[xx + yy * w] == Tiles.get("grass").id) {
map[xx + yy * w] = Tiles.get("flower").id;
data[xx + yy * w] = (short) (col + random.nextInt(3)); // Data determines what the flower is
if (random.nextInt(4) == 0) { // Smaller chances for flowers
map[xx + yy * w] = Tiles.get("flower").id;
data[xx + yy * w] = (short) (col2 + random.nextInt(3)); // Data determines what the flower is
} else {
map[xx + yy * w] = Tiles.get("Tall Grass").id;
data[xx + yy * w] = TallGrassTile.getRandomData(random);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/client/java/minicraft/level/tile/FlowerTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public boolean connectsToGrass(Level level, int x, int y) {

public boolean tick(Level level, int xt, int yt) {
// TODO revise this method.
//noinspection DuplicatedCode
if (random.nextInt(30) != 0) return false; // Skips every 31 tick.

int xn = xt;
Expand Down
44 changes: 36 additions & 8 deletions src/client/java/minicraft/level/tile/GrassTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import minicraft.core.io.Sound;
import minicraft.entity.Direction;
import minicraft.entity.mob.Player;
import minicraft.entity.particle.Particle;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteAnimation;
import minicraft.gfx.SpriteLinker;
import minicraft.gfx.SpriteLinker.SpriteType;
import minicraft.item.Item;
import minicraft.item.Items;
import minicraft.item.StackableItem;
import minicraft.item.ToolItem;
import minicraft.item.ToolType;
import minicraft.level.Level;
import minicraft.util.AdvancementElement;

import java.util.Random;

public class GrassTile extends Tile {
private static final SpriteAnimation sprite = new SpriteAnimation(SpriteType.Tile, "grass")
.setConnectionChecker((level, x, y, tile, side) -> !side || tile.connectsToGrass(level, x, y))
Expand All @@ -30,17 +35,24 @@ public boolean connectsToGrass(Level level, int x, int y) {

public boolean tick(Level level, int xt, int yt) {
// TODO revise this method.
if (random.nextInt(40) != 0) return false;
if (random.nextInt(35) != 0) return false;

int xn = xt;
int yn = yt;
if (random.nextInt(20) == 0) { // "Generate" tall grass
level.setTile(xt, yt, Tiles.get("Tall Grass"), TallGrassTile.getRandomData(random));
} else { // "Extend" grass
//noinspection DuplicatedCode
int xn = xt;
int yn = yt;

if (random.nextBoolean()) xn += random.nextInt(2) * 2 - 1;
else yn += random.nextInt(2) * 2 - 1;
if (random.nextBoolean()) xn += random.nextInt(2) * 2 - 1;
else yn += random.nextInt(2) * 2 - 1;

if (level.getTile(xn, yn) == Tiles.get("Dirt")) {
level.setTile(xn, yn, this);
if (level.getTile(xn, yn) == Tiles.get("Dirt")) {
level.setTile(xn, yn, this);
return true;
}
}

return false;
}

Expand All @@ -50,6 +62,8 @@ public void render(Screen screen, Level level, int x, int y) {
sprite.render(screen, level, x, y);
}

private static final SpriteLinker.LinkedSprite particleSprite = new SpriteLinker.LinkedSprite(SpriteLinker.SpriteType.Entity, "glint");

public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
Expand All @@ -72,7 +86,7 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
int data = level.getData(xt, yt);
level.setTile(xt, yt, Tiles.get("Farmland"));
Sound.play("monsterhurt");
if (random.nextInt(5) != 0) { // 80% chance to drop Wheat seeds
if (random.nextInt(8) < 5) { // 62.5% chance to drop Wheat seeds (some is covered by tall grass)
level.dropItem((xt << 4) + 8, (yt << 4) + 8, Items.get("Wheat Seeds"));
}
AdvancementElement.AdvancementTrigger.ItemUsedOnTileTrigger.INSTANCE.trigger(
Expand All @@ -87,6 +101,20 @@ public boolean interact(Level level, int xt, int yt, Player player, Item item, D
Sound.play("monsterhurt");
}
}
} else if (item instanceof StackableItem && item.getName().equalsIgnoreCase("Fertilizer")) {
//noinspection DuplicatedCode
((StackableItem) item).count--;
Random random = new Random();
for (int i = 0; i < 2; ++i) {
double x = (double) xt * 16 + 8 + (random.nextGaussian() * 0.5) * 8;
double y = (double) yt * 16 + 8 + (random.nextGaussian() * 0.5) * 8;
level.add(new Particle((int) x, (int) y, 120 + random.nextInt(21) - 40, particleSprite));
}

// Change to forcedly generate tall grass
if (random.nextInt(4) == 0) // 25%
level.setTile(xt, yt, Tiles.get("Tall Grass"), TallGrassTile.getRandomData(random));
return true;
}
return false;
}
Expand Down
88 changes: 88 additions & 0 deletions src/client/java/minicraft/level/tile/TallGrassTile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package minicraft.level.tile;

import minicraft.core.io.Sound;
import minicraft.entity.Direction;
import minicraft.entity.mob.Player;
import minicraft.gfx.Screen;
import minicraft.gfx.SpriteAnimation;
import minicraft.gfx.SpriteLinker;
import minicraft.item.Item;
import minicraft.item.Items;
import minicraft.item.ToolItem;
import minicraft.item.ToolType;
import minicraft.level.Level;
import minicraft.util.AdvancementElement;
import org.intellij.lang.annotations.MagicConstant;

import java.util.Random;

public class TallGrassTile extends Tile {
private static final int NUM_VARIANTS = 2;
private static final SpriteAnimation[] SPRITES = new SpriteAnimation[] {
new SpriteAnimation(SpriteLinker.SpriteType.Tile, "tall_grass_variant0")
.setConnectionChecker((level, x, y, tile, side) -> tile instanceof TallGrassTile),
new SpriteAnimation(SpriteLinker.SpriteType.Tile, "tall_grass_variant1")
.setConnectionChecker((level, x, t, tile, side) -> tile instanceof TallGrassTile)
};

protected TallGrassTile() {
super("Tall Grass", null);
}

@Override
public boolean connectsToGrass(Level level, int x, int y) {
return true;
}

public static short getRandomData(Random random) {
return (short) random.nextInt(NUM_VARIANTS);
}

@Override
public void render(Screen screen, Level level, int x, int y) {
Tiles.get("Grass").render(screen, level, x, y);
int variant = level.getData(x, y) & 0x1; // Ensures that it is in range.
SPRITES[variant].render(screen, level, x, y);
}

@Override
public boolean tick(Level level, int xt, int yt) {
//noinspection DuplicatedCode
if (random.nextInt(30) != 0) return false;

int xn = xt;
int yn = yt;

if (random.nextBoolean()) xn += random.nextInt(2) * 2 - 1;
else yn += random.nextInt(2) * 2 - 1;

if (level.getTile(xn, yn) == Tiles.get("Dirt")) {
level.setTile(xn, yn, this);
return true;
}

return false;
}

@Override
public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if (item instanceof ToolItem) {
ToolItem tool = (ToolItem) item;
if (tool.type == ToolType.Hoe) {
if (player.payStamina(4 - tool.level) && tool.payDurability()) {
int data = level.getData(xt, yt);
level.setTile(xt, yt, Tiles.get("Grass"));
Sound.play("monsterhurt");
if (random.nextInt(2) == 0) { // 50% chance to drop Wheat seeds
level.dropItem(xt * 16 + 8, yt * 16 + 8, Items.get("Wheat Seeds"));
}
AdvancementElement.AdvancementTrigger.ItemUsedOnTileTrigger.INSTANCE.trigger(
new AdvancementElement.AdvancementTrigger.ItemUsedOnTileTrigger.ItemUsedOnTileTriggerConditionHandler.ItemUsedOnTileTriggerConditions(
item, this, data, xt, yt, level.depth));
return true;
}
}
}
return false;
}
}
1 change: 1 addition & 0 deletions src/client/java/minicraft/level/tile/Tiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static void initTileList() {
tiles.put((short) 57, new TorchTile());
tiles.put((short) 58, new SignTile());
tiles.put((short) 59, new DecorTile(DecorTile.decorType.ORNATE_WOOD));
tiles.put((short) 60, new TallGrassTile());

// WARNING: don't use this tile for anything!
tiles.put((short) 255, new ConnectTile());
Expand Down
3 changes: 2 additions & 1 deletion src/client/java/minicraft/level/tile/farming/CropTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Arrays;
import java.util.Random;

public class CropTile extends FarmTile {
public abstract class CropTile extends FarmTile {
protected final @Nullable String seed;

protected int maxAge = 0b111; // Must be a bit mask.
Expand Down Expand Up @@ -104,6 +104,7 @@ public boolean tick(Level level, int xt, int yt) {
@Override
public boolean interact(Level level, int xt, int yt, Player player, Item item, Direction attackDir) {
if (item instanceof StackableItem && item.getName().equalsIgnoreCase("Fertilizer")) {
//noinspection DuplicatedCode
((StackableItem) item).count--;
Random random = new Random();
for (int i = 0; i < 2; ++i) {
Expand Down
1 change: 1 addition & 0 deletions src/client/resources/assets/localization/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"String": "String",
"Swim Potion": "Swim Potion",
"Sword": "Sword",
"Tall Grass": "Tall Grass",
"Time Potion": "Time Potion",
"Tnt": "Tnt",
"Torch": "Torch",
Expand Down
Binary file modified src/client/resources/assets/textures/tile/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/client/resources/assets/textures/tile/grass_border.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"border": {
"key": "tall_grass_border_variant0"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"border": {
"key": "tall_grass_border_variant1"
}
}
Loading