Made items throwable by the player, fixed issues with bullets and chunk

borders.
This commit is contained in:
josua 2019-09-02 14:36:18 +10:00
parent 121c6f10eb
commit e48d0cfc6c
23 changed files with 255 additions and 52 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 71 KiB

After

Width:  |  Height:  |  Size: 73 KiB

Binary file not shown.

View File

@ -35,6 +35,7 @@ public class Main
public static EntityPlayer player = new EntityPlayer();
public static World world;
public static AudioEngine audio;
public static Random rand = new Random();
public static void main(String[] args)
{

View File

@ -7,5 +7,5 @@ public interface ITransparentObject
{
public boolean isOpaqueTile();
public void render(Vec2d pos, Camera camera, short meta);
public Vec2d getRenderOffset();
public Vec2d getRenderOffset(short meta);
}

View File

@ -12,7 +12,7 @@ class TransparentObject
short meta;
TransparentObject(ITransparentObject object, Camera camera, Vec2d pos, short meta) {
Vec2d offset = object.getRenderOffset();
Vec2d offset = object.getRenderOffset(meta);
this.distance = camera.pos.distance(new Vec3d(offset.x + pos.x, offset.y + pos.y, 0));
this.object = object;
this.pos = pos;

View File

@ -1,38 +1,45 @@
package shootergame.display.transparent;
import java.util.ArrayList;
import shootergame.display.Camera;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec3d;
public class TransparentObjects
{
private static ArrayList<TransparentObject> objects = new ArrayList<TransparentObject>();
private static TransparentObject[] objects = new TransparentObject[0];
public static void clear() {
objects.clear();
objects = new TransparentObject[0];
}
public static void register(ITransparentObject object, Camera camera, Vec2d pos, short meta)
{
TransparentObject r_to = new TransparentObject(object, camera, pos, meta);
ArrayList<TransparentObject> objects_n = new ArrayList<TransparentObject>();
TransparentObject[] objects_n = new TransparentObject[objects.length + 1];
boolean added = false;
int upto = 0;
//System.out.println("objects length: "+objects.length);
//System.out.println("objects_n length: "+objects_n.length);
// Loop over the transparent object items
for(TransparentObject to : objects)
for(int i=0;i<objects.length;i++)
{
TransparentObject to = objects[i];
//System.out.println("UPTO: "+upto+", to: "+to);
if(r_to.distance > to.distance && !added) {
added = true;
objects_n.add(r_to);
objects_n[upto] = r_to;
upto += 1;
}
objects_n.add(to);
objects_n[upto] = to;
upto += 1;
}
if(!added) {
objects_n.add(r_to);
objects_n[upto] = r_to;
}
objects = objects_n;

View File

@ -51,8 +51,10 @@ public class Entity implements ITransparentObject
speed = 1;
angle = MathHelpers.floor(angle);
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
tile_back = layer.getBackTile(tpos);
tile_front = layer.getFrontTile(tpos);
if(chunk == null) chunk = layer.getChunk(pos);
this.chunk = chunk;
tile_back = chunk.getBackTile(tpos);
tile_front = chunk.getFrontTile(tpos);
if(this.isSolid)
{
@ -134,8 +136,8 @@ public class Entity implements ITransparentObject
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
// Activate both tiles
tile_front.tile.onActivated(chunk, layer, tpos, this, tile_front.meta);
tile_back.tile.onActivated(chunk, layer, tpos, this, tile_back.meta);
tile_front.tile.onActivated(layer, tpos, this, tile_front.meta);
tile_back.tile.onActivated(layer, tpos, this, tile_back.meta);
}
public void activateSteppedOnTile()
@ -153,8 +155,9 @@ public class Entity implements ITransparentObject
public boolean moveIsLegal(Vec2d pos)
{
Vec2i t_pos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
tile_back = Main.world.getLayer().getBackTile(t_pos);
tile_front = Main.world.getLayer().getFrontTile(t_pos);
Chunk chunk = Main.world.getLayer().getChunk(pos);
tile_back = chunk.getBackTile(t_pos);
tile_front = chunk.getFrontTile(t_pos);
// Is this entity solid
if(!goThroughSolid || !crossUnWalkable)
@ -211,7 +214,7 @@ public class Entity implements ITransparentObject
}
@Override
public Vec2d getRenderOffset() {
public Vec2d getRenderOffset(short meta) {
return new Vec2d(0, 0);
}
}

View File

@ -23,13 +23,18 @@ public class EntityBullet extends EntityParticle
private int time = 0;
private Entity parent;
public EntityBullet(Vec2d pos, Entity parent, double angle) {
private double damage;
private int breakchance;
public EntityBullet(Vec2d pos, Entity parent, double angle, double damage, int breakchance) {
super(0.2, 0.4);
// Store some specified values
this.pos = pos;
this.angle = angle;
this.parent = parent;
this.damage = damage;
this.breakchance = breakchance;
// Play the gun sound
Sounds.GUN.play(new Vec3d(pos.x, pos.y, 0.4), 2);
@ -41,33 +46,34 @@ public class EntityBullet extends EntityParticle
// Move forward in the bullets angle, very quickly
this.moveForward(0.2);
chunk = layer.getChunk(pos);
// Is the bullets new position intersecting a solid object
{
// Get the position of the tile the bullet is over
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x), MathHelpers.floor(pos.y));
// Get the foreground tile and the background tile
TileState tile_f = chunk.getFrontTile(tpos);
TileState tile_b = chunk.getBackTile(tpos);
Tile tile_f = chunk.getFrontTile(tpos).tile;
Tile tile_b = chunk.getBackTile(tpos).tile;
// Is the tile solid and has the bullet crashed into it
if(tile_f.tile.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile_f.tile.tileHitbox)
if(tile_f.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile_f.tileHitbox)
{
// Break the block
if(RandomHelpers.randrange(rand, 5) == 0) {
if(RandomHelpers.randrange(rand, breakchance) == 0) {
chunk.breakFrontTile(tpos);
}
// Delete the bullet
kill();
}
} if(tile_b.tile.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile_b.tile.tileHitbox)
} if(tile_b.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile_b.tileHitbox)
{
// Break the block
if(RandomHelpers.randrange(rand, 5) == 0) {
if(RandomHelpers.randrange(rand, breakchance) == 0) {
chunk.breakBackTile(tpos);
}
@ -95,7 +101,7 @@ public class EntityBullet extends EntityParticle
Sounds.HIT.play(new Vec3d(pos.x, pos.y, 0.4), 1);
// Harm the entity
ea.removeHealth(10);
ea.removeHealth(damage);
// Kill the bullet
chunk.killEntity(this);

View File

@ -55,15 +55,15 @@ public class EntityItem extends EntityVertical
{
// Pick the stack up if its an inventory
stack.item.onPickedUp(stack, layer, chunk, e);
// Kill this entity if the stack is empty
if(stack.isEmpty()) {
kill();
return;
}
}
}
}
// Kill this entity if the stack is empty
if(stack.isEmpty()) {
kill();
return;
}
}
@Override

View File

@ -5,6 +5,8 @@ import java.util.Random;
import shootergame.Main;
import shootergame.display.Camera;
import shootergame.entity.particle.ParticleBlood;
import shootergame.entity.particle.ParticleBreak;
import shootergame.entity.particle.ParticleSmoke;
import shootergame.entity.particle.ParticleSpark;
import shootergame.init.Sounds;
import shootergame.init.Textures;
@ -93,9 +95,15 @@ public class EntityTnt extends EntityVertical
}
// Set the tiles
if(!bts.tile.unbreakable) l.setBackTile(new TileState(ets.tile,
(short)blackened_gradient), tpos);
if(!fts.tile.unbreakable) l.setFrontTile(Tiles.VOID.getDefaultState(), tpos);
if(!bts.tile.unbreakable) {
l.setBackTile(new TileState(ets.tile, (short)blackened_gradient), tpos);
l.spawnEntity(new ParticleBreak(new Vec2d(tpos.x+rand.nextDouble(), tpos.y+rand.nextDouble()), bts));
}
if(!fts.tile.unbreakable) {
l.setFrontTile(Tiles.VOID.getDefaultState(), tpos);
l.spawnEntity(new ParticleBreak(new Vec2d(tpos.x+rand.nextDouble(), tpos.y+rand.nextDouble()), fts));
}
boolean killed_entities = false;
@ -114,6 +122,9 @@ public class EntityTnt extends EntityVertical
// Spawn some blood if entities were killed
if(killed_entities)
l.spawnEntity(new ParticleBlood(rand, new Vec2d(px, py), py));
// Spawn some smoke
l.spawnEntity(new ParticleSmoke(new Vec2d(px, py)));
}
}
}

View File

@ -59,7 +59,7 @@ public class EntityZombie extends EntityVertical implements EntityAlive
angle_gun += noise_gun_angle.eval(time, 0)*20;
// Fire the gun
layer.spawnEntity(new EntityBullet(pos.copy(), this, angle_gun));
layer.spawnEntity(new EntityBullet(pos.copy(), this, angle_gun, 20, 5));
}
}

View File

@ -0,0 +1,45 @@
package shootergame.entity.particle;
import shootergame.Main;
import shootergame.display.Camera;
import shootergame.entity.EntityVertical;
import shootergame.init.Textures;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class ParticleSmoke extends EntityVertical
{
double height = 0;
double opacity = 1;
public ParticleSmoke(Vec2d pos) {
super(Textures.PARTICLE_SMOKE_RANDOM.getTexture(), new Vec2d(1, 1));
this.pos = pos;
}
@Override
public void render(Vec2d pos, Camera camera) {
if(opacity <= 0) return;
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, height);
GlHelpers.color4(1, 1, 1, opacity);
super.render(pos, camera);
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.popMatrix();
}
@Override
public void tick(Chunk chunk, Layer layer) {
super.tick(chunk, layer);
if(pos.squareDistance(Main.player.pos) > 32 || opacity <= 0) {
kill();
}
height += 0.001;
opacity -= 0.001;
}
}

View File

@ -9,6 +9,7 @@ import shootergame.entity.Entity;
import shootergame.entity.EntityAlive;
import shootergame.entity.EntityBullet;
import shootergame.entity.EntityInventory;
import shootergame.entity.EntityItem;
import shootergame.entity.EntityTnt;
import shootergame.entity.EntityVertical;
import shootergame.init.Items;
@ -179,7 +180,7 @@ public class EntityPlayer extends EntityVertical implements EntityAlive, EntityI
ammo -= 1;
// Summon bullets at this angle relative to the player
Main.world.getLayer().spawnEntity(new EntityBullet(pos.copy(), this, angle + this.angle));
Main.world.getLayer().spawnEntity(new EntityBullet(pos.copy(), this, angle + this.angle, 20, 5));
}
}
@ -234,4 +235,17 @@ public class EntityPlayer extends EntityVertical implements EntityAlive, EntityI
public Inventory getInventory() {
return inventory;
}
public void dropItem()
{
ItemStack i = inventory.getItem(inventory_hand);
if(!i.isEmpty())
{
Entity e = new EntityItem(pos.copy(), new ItemStack(i.item, 1, i.meta));
e.angle = angle;
Main.world.getLayer().spawnEntity(e);
i.count -= 1;
}
}
}

View File

@ -6,6 +6,7 @@ import shootergame.items.ItemDefenceUpgrade;
import shootergame.items.ItemEmpty;
import shootergame.items.ItemGunUpgrade;
import shootergame.items.ItemHealthPotion;
import shootergame.items.ItemTnt;
public class Items
{
@ -14,4 +15,5 @@ public class Items
public static final Item GUN_UPGRADE = new ItemGunUpgrade("gun_upgrade");
public static final Item HEALTH_POTION = new ItemHealthPotion("health_potion");
public static final Item EMPTY = new ItemEmpty("empty");
public static final Item TNT = new ItemTnt("tnt");
}

View File

@ -7,6 +7,7 @@ import org.lwjgl.opengl.GL;
import shootergame.display.DisplayWindow;
import shootergame.util.gl.texture.TextureMap;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.gl.texture.TextureReferenceRandom;
import shootergame.util.gl.texture.AnimationReference;
public class Textures
@ -38,6 +39,11 @@ public class Textures
public static final TextureReference TILE_LADDER_UP = texmap.getTextureReference(16, 17, 0, 16);
public static final TextureReference TILE_CHEST = texmap.getTextureReference(2, 3, 4, 5);
public static final TextureReferenceRandom PARTICLE_SMOKE_RANDOM = new TextureReferenceRandom(
texmap.getTextureReference(14, 15, 13, 14), texmap.getTextureReference(15, 16, 13, 14),
texmap.getTextureReference(14, 15, 14, 15), texmap.getTextureReference(15, 16, 14, 15),
texmap.getTextureReference(14, 15, 15, 16), texmap.getTextureReference(15, 16, 15, 16));
public static final TextureReference UI_HEALTH_FG = texmap.getTextureReference(0, 16, 11, 12);
public static final TextureReference UI_HEALTH_BG = texmap.getTextureReference(0, 16, 12, 13);
public static final TextureReference UI_ITEM_SLOTS = texmap.getTextureReference(0, 12, 13, 15);

View File

@ -18,6 +18,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
private static boolean activateItem_last = false;
private static boolean activateTile_last = false;
private static boolean dropItem_last = false;
private static boolean hotbar_l = false;
private static boolean hotbar_r = false;
@ -219,6 +220,18 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
else if(activateTile_last) {
activateTile_last = false;
}
// Drop item
if(button_b) {
if(!dropItem_last) {
Main.player.dropItem();
dropItem_last = true;
}
}
else if(dropItem_last) {
dropItem_last = false;
}
}
}

View File

@ -0,0 +1,30 @@
package shootergame.items;
import shootergame.entity.Entity;
import shootergame.entity.EntityTnt;
import shootergame.init.Textures;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class ItemTnt extends Item
{
public ItemTnt(String id) {
super(id);
this.texture = Textures.ENTITY_TNT;
}
@Override
public String getName(short meta) {
return "TNT";
}
@Override
public void onAction(ItemStack stack, Layer layer, Chunk chunk, Entity entity) {
super.onAction(stack, layer, chunk, entity);
layer.spawnEntity(new EntityTnt(entity.pos.copy(), entity.angle, stack.meta));
}
}

View File

@ -54,7 +54,7 @@ public class Tile implements ITransparentObject
}
@Override
public Vec2d getRenderOffset() {
public Vec2d getRenderOffset(short meta) {
return new Vec2d(0.5, 0.5);
}
@ -62,6 +62,6 @@ public class Tile implements ITransparentObject
return new TileState(this, (short)0);
}
public void onActivated(Chunk chunk, Layer layer, Vec2i tpos, Entity entity, short meta) {
public void onActivated(Layer layer, Vec2i tpos, Entity entity, short meta) {
}
}

View File

@ -1,11 +1,16 @@
package shootergame.tiles;
import java.util.Random;
import shootergame.Main;
import shootergame.entity.Entity;
import shootergame.entity.EntityItem;
import shootergame.init.Items;
import shootergame.init.Textures;
import shootergame.items.Item;
import shootergame.items.ItemStack;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.world.chunk.Chunk;
@ -13,6 +18,7 @@ import shootergame.world.layer.Layer;
public class TileChest extends TileVertical
{
public static final short CHEST_CAVES = 0;
public TileChest(String id) {
super(id, Textures.TILE_CHEST, new Vec2d(1, 1));
@ -23,11 +29,45 @@ public class TileChest extends TileVertical
}
@Override
public void onActivated(Chunk chunk, Layer layer, Vec2i tpos, Entity entity, short meta) {
super.onActivated(chunk, layer, tpos, entity, meta);
private void spawnItem(Chunk chunk, Vec2i pos, ItemStack stack) {
chunk.spawnEntity(new EntityItem(new Vec2d(pos.x+0.5, pos.y+0.5), stack));
}
@Override
public void onActivated(Layer layer, Vec2i tpos, Entity entity, short meta) {
super.onActivated(layer, tpos, entity, meta);
Chunk chunk = layer.getChunk(tpos);
Random rand = Main.rand;
if(meta == CHEST_CAVES)
{
// Ammo
spawnItem(chunk, tpos, new ItemStack(Items.AMMO, RandomHelpers.randrange(rand, 100), (short)1));
// Tnt
spawnItem(chunk, tpos, new ItemStack(Items.TNT, RandomHelpers.randrange(rand, 2), (short)10));
// Health potions
spawnItem(chunk, tpos, new ItemStack(Items.HEALTH_POTION, RandomHelpers.randrange(rand, 10), (short)20));
// Gun upgrade
if(RandomHelpers.randrange(rand, 5) == 0) {
spawnItem(chunk, tpos, new ItemStack(Items.GUN_UPGRADE, 1, (short)1));
}
if(RandomHelpers.randrange(rand, 20) == 0) {
spawnItem(chunk, tpos, new ItemStack(Items.GUN_UPGRADE, 1, (short)2));
}
// Defence upgrade
if(RandomHelpers.randrange(rand, 20) == 0) {
spawnItem(chunk, tpos, new ItemStack(Items.DEFENCE_UPGRADE, 1, (short)2));
}
if(RandomHelpers.randrange(rand, 5) == 0) {
spawnItem(chunk, tpos, new ItemStack(Items.DEFENCE_UPGRADE, 1, (short)1));
}
}
layer.spawnEntity(new EntityItem(new Vec2d(tpos.x+0.5, tpos.y+0.5), new ItemStack(Items.HEALTH_POTION, 1, (short)20)));
layer.breakFrontTile(tpos);
}

View File

@ -23,10 +23,10 @@ public class TileLadderUp extends TileVertical
}
@Override
public void onActivated(Chunk chunk, Layer layer, Vec2i pos, Entity entity, short meta)
public void onActivated(Layer layer, Vec2i pos, Entity entity, short meta)
{
// Call super
super.onWalkedOn(chunk, layer, pos, entity, meta);
super.onActivated(layer, pos, entity, meta);
// Is the entity the player
if(entity == Main.player)

View File

@ -24,10 +24,10 @@ public class TilePortalDown extends TileFlat
}
@Override
public void onActivated(Chunk chunk, Layer layer, Vec2i pos, Entity entity, short meta)
public void onActivated(Layer layer, Vec2i pos, Entity entity, short meta)
{
// Call super
super.onWalkedOn(chunk, layer, pos, entity, meta);
super.onActivated(layer, pos, entity, meta);
// Is the entity the player
if(entity == Main.player)

View File

@ -0,0 +1,21 @@
package shootergame.util.gl.texture;
import java.util.ArrayList;
import java.util.Random;
import shootergame.util.math.random.RandomHelpers;
public class TextureReferenceRandom
{
private static final Random rand = new Random();
private TextureReference[] textures;
public TextureReferenceRandom(TextureReference ... textures) {
this.textures = textures;
}
public TextureReference getTexture() {
return textures[RandomHelpers.randrange(rand, textures.length)];
}
}

View File

@ -46,8 +46,10 @@ public class LayerGenEarth extends LayerGen
Vec2i pos = new Vec2i(x, y);
// Tree and rock generation
if(rand.nextDouble() > 0.9) chunk.setFrontTile(Tiles.TREE.getDefaultState(), pos);
else if(rand.nextDouble() > 0.99) chunk.setFrontTile(Tiles.ROCK.getDefaultState(), pos);
if(rand.nextDouble() > 0.9) chunk.setFrontTile(new TileState(Tiles.TREE,
(short)RandomHelpers.randrange(rand, Short.MAX_VALUE)), pos);
else if(rand.nextDouble() > 0.99) chunk.setFrontTile(new TileState(Tiles.ROCK,
(short)RandomHelpers.randrange(rand, Short.MAX_VALUE)), pos);
else chunk.setFrontTile(Tiles.VOID.getDefaultState(), pos);
// Terrain generation
@ -86,4 +88,6 @@ public class LayerGenEarth extends LayerGen
public TileState getTileDestroyed() {
return Tiles.DIRT.getDefaultState();
}
}