Added a proper audio engine, added tnt, added another dimention.

This commit is contained in:
josua 2019-08-28 13:59:21 +10:00
parent 6e17063619
commit 2f9e19d072
58 changed files with 1005 additions and 93 deletions

BIN
resources/sound/gun0.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun1.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun2.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun3.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun5.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun6.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun7.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun8.ogg Normal file

Binary file not shown.

BIN
resources/sound/gun9.ogg Normal file

Binary file not shown.

BIN
resources/sound/hit0.ogg Normal file

Binary file not shown.

BIN
resources/sound/hit1.ogg Normal file

Binary file not shown.

BIN
resources/sound/hit2.ogg Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 48 KiB

BIN
resources/texmap.xcf Normal file

Binary file not shown.

View File

@ -25,6 +25,7 @@ import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.World;
import shootergame.world.chunk.ChunkEventHandler;
import shootergame.world.layer.layergen.LayerGenCaves;
import shootergame.world.layer.layergen.LayerGenEarth;
public class Main
@ -69,7 +70,7 @@ public class Main
JoystickCallback.JOYSTICK_CALLBACK.init();
// Create the world
world = new World(new Random(), new LayerGenEarth());
world = new World(new Random(), new LayerGenEarth(), new LayerGenCaves());
// Initialise the entities
mainloop.register(EntityEventHandler.ENTITY_EVENT_HANDLER);

View File

@ -57,7 +57,7 @@ public class Test
alBufferData(bufferPointer, format, rawAudioBuffer, sampleRate);
//Free the memory allocated by STB
free(rawAudioBuffer);
//free(rawAudioBuffer);
while(true)
{

View File

@ -69,8 +69,8 @@ public class AudioObject
alBufferData(bufferPointer, format, audio, sample_rate);
// Free some c buffers
LibCStdlib.free(audio);
LibCStdlib.free(resource_buffer);
//LibCStdlib.free(audio);
//LibCStdlib.free(resource_buffer);
}
public void play(Vec3d pos, double volume)
@ -78,11 +78,19 @@ public class AudioObject
// Get the player pos
Vec2d p_pos = Main.player.pos;
// Calculate the position
double x = ( pos.x - p_pos.x ) * Math.sin(Math.toRadians(Main.player.angle));
double y = ( pos.y - p_pos.y ) * Math.cos(Math.toRadians(Main.player.angle));
// Convert the angle to radians
double angle_r = Math.toRadians(Main.player.angle + 90);
// Calculate the translation
double x = ( pos.x - p_pos.x );
double y = ( pos.y - p_pos.y );
double z = pos.z;
// Calculate the rotation
Vec2d rotated = MathHelpers.rotate2(new Vec2d(x, y), angle_r);
x = rotated.x;
y = rotated.y;
// Play the sound with a new source
int source = AudioSources.getSource();
alSourcei(source, AL_BUFFER, bufferPointer);

View File

@ -0,0 +1,29 @@
package shootergame.audio;
import java.util.Random;
import shootergame.resources.Resource;
import shootergame.util.math.vec.Vec3d;
public class AudioRandom extends AudioObject
{
private AudioObject[] audioObjects;
private static Random rand = new Random();
public AudioRandom(AudioObject ... audioObjects) {
super(null);
// Set the specified parameters
this.audioObjects = audioObjects;
}
@Override
public void init() {
}
@Override
public void play(Vec3d pos, double volume) {
audioObjects[(int)(rand.nextDouble()*audioObjects.length)].play(pos, volume);
}
}

View File

@ -11,6 +11,8 @@ public class Camera
public int renderDistance;
public double cameraDistance;
public static Camera camera;
public Camera(Vec3d pos, Vec2d angle, double cameraDistance, int renderDistance)
{
this.angle = new Vec2d(angle.x, -angle.y);

View File

@ -69,6 +69,7 @@ public class DisplayRender
EntityPlayer player = Main.player;
Camera camera = new Camera(new Vec3d(player.pos.x, player.pos.y, 0), new Vec2d(player.angle, 45), 10, 2);
Camera.camera = camera;
//GlHelpers.translate(0, 0, -5);
GlHelpers.rotate(camera.angle.y, 1, 0, 0);

View File

@ -6,6 +6,6 @@ import shootergame.util.math.vec.Vec2d;
public interface ITransparentObject
{
public boolean isOpaqueTile();
public void render(Vec2d pos, Camera camera);
public void render(Vec2d pos, Camera camera, short meta);
public Vec2d getRenderOffset();
}

View File

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

View File

@ -3,6 +3,7 @@ package shootergame.display.transparent;
import java.util.ArrayList;
import shootergame.display.Camera;
import shootergame.tiles.TileBlackened;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec3d;
@ -14,9 +15,9 @@ public class TransparentObjects
objects.clear();
}
public static void register(ITransparentObject object, Camera camera, Vec2d pos)
public static void register(ITransparentObject object, Camera camera, Vec2d pos, short meta)
{
TransparentObject r_to = new TransparentObject(object, camera, pos);
TransparentObject r_to = new TransparentObject(object, camera, pos, meta);
ArrayList<TransparentObject> objects_n = new ArrayList<TransparentObject>();
boolean added = false;
@ -42,7 +43,7 @@ public class TransparentObjects
{
// Loop over the objects and render all of them
for(TransparentObject to : objects) {
to.object.render(to.pos, camera);
to.object.render(to.pos, camera, to.meta);
}
}
}

View File

@ -9,6 +9,7 @@ import shootergame.display.transparent.TransparentObjects;
import shootergame.init.Entities;
import shootergame.tiles.Tile;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.TileState;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
@ -24,6 +25,9 @@ public class Entity implements ITransparentObject
public double hitbox = 1;
public boolean isSolid = false;
public Chunk chunk;
private double speed = 1;
private TileState tile_front;
private TileState tile_back;
public Entity(Vec2d pos, double angle)
{
@ -40,16 +44,35 @@ public class Entity implements ITransparentObject
}
public void tick(Chunk chunk, Layer layer) {
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(this.isSolid)
{
this.addSlowness(tile_back.tile.slowness);
this.addSlowness(tile_front.tile.slowness);
}
}
public void addSlowness(double amount) {
speed *= (1 - amount);
}
public void render(Vec2d pos, Camera camera) {
}
@Override
public void render(Vec2d pos, Camera camera) {
public void render(Vec2d pos, Camera camera, short meta) {
this.render(pos, camera);
}
public void doRender(Vec2d pos, Camera camera)
{
if(this.opaqueTile) {
TransparentObjects.register(this, camera, pos);
TransparentObjects.register(this, camera, pos, (short)0);
}
else {
@ -63,15 +86,25 @@ public class Entity implements ITransparentObject
}
public void moveForward(double speed) {
Vec2d pos = this.pos.add(MathHelpers.moveTowards2(speed, Math.toRadians(this.angle)));
if(this.moveIsLegal(new Vec2d(this.pos.x, pos.y))) this.pos.y = pos.y;
if(this.moveIsLegal(new Vec2d(pos.x, this.pos.y))) this.pos.x = pos.x;
this.moveTowards(0, speed);
}
public void moveBackward(double speed) {
Vec2d pos = this.pos.add(MathHelpers.moveTowards2(-speed, Math.toRadians(this.angle)));
this.moveTowards(0, -speed);
}
public void moveTowards(double angle, double speed)
{
// Calculate the new position
speed *= this.speed;
Vec2d pos = this.pos.add(MathHelpers.moveTowards2(speed, Math.toRadians(this.angle + angle)));
// Check if the new position is legal
if(this.moveIsLegal(new Vec2d(this.pos.x, pos.y))) this.pos.y = pos.y;
if(this.moveIsLegal(new Vec2d(pos.x, this.pos.y))) this.pos.x = pos.x;
// Activate stepped on tiles if the entity is "solid"
if(this.isSolid) activateSteppedOnTile();
}
public void moveForward() {
@ -82,17 +115,43 @@ public class Entity implements ITransparentObject
this.moveBackward(0.1);
}
public void moveTowards(double angle) {
moveTowards(angle, 0.1);
}
public void kill() {
chunk.killEntity(this);
}
public void activateSteppedOnBlocks()
public void activateTile()
{
// Get the tile position and the layer
Layer layer = Main.world.getLayer();
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);
}
public void activateSteppedOnTile()
{
// Get the tile position and the layer
Layer layer = Main.world.getLayer();
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
// Activate both tiles
tile_front.tile.onWalkedOn(chunk, layer, tpos, this, tile_front.meta);
tile_back.tile.onWalkedOn(chunk, layer, tpos, this, tile_back.meta);
}
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);
// Is this entity solid
if(isSolid)
{
@ -104,17 +163,17 @@ public class Entity implements ITransparentObject
{
// Get the tile
Tile t = l.getBackTile(tpos);
Tile t = tile_back.tile;
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
return false;
}
}
}
{
// Get the front tile
Tile t = l.getFrontTile(tpos);
Tile t = tile_front.tile;
// Send false if the tile isn't walkable
if(!t.tileWalkable) {

View File

@ -5,9 +5,11 @@ import java.util.Random;
import shootergame.display.Camera;
import shootergame.entity.particle.ParticleBlood;
import shootergame.init.Sounds;
import shootergame.init.Tiles;
import shootergame.tiles.Tile;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.TileState;
import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
@ -46,12 +48,19 @@ public class EntityBullet extends EntityParticle
// Get the position of the tile the bullet is over
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
// Get the foreground tile
Tile tile = chunk.getFrontTile(tpos);
// Get the foreground tile and the background tile
TileState tile_f = chunk.getFrontTile(tpos);
TileState tile_b = chunk.getBackTile(tpos);
// Is the tile solid and has the bullet crashed into it
if(tile.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile.tileHitbox)
if(tile_f.tile.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile_f.tile.tileHitbox)
{
// 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)
{
// Delete the bullet
kill();
@ -73,6 +82,9 @@ public class EntityBullet extends EntityParticle
chunk.spawnEntity(new ParticleBlood(rand, pos.copy(), angle));
}
// Play the hit noise
Sounds.HIT.play(new Vec3d(pos.x, pos.y, 0.4), 1);
// Harm the entity
ea.removeHealth(10);

View File

@ -1,8 +1,11 @@
package shootergame.entity;
import shootergame.Main;
import shootergame.display.Camera;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class EntityParticle extends Entity
{
@ -21,6 +24,14 @@ public class EntityParticle extends Entity
this.size = size;
}
@Override
public void tick(Chunk chunk, Layer layer) {
super.tick(chunk, layer);
// Kill the particle if the player can't see it to reduce lag
if(Main.player.pos.squareDistance(pos) > 32) this.kill();
}
@Override
public void render(Vec2d pos, Camera camera)
{

View File

@ -1,6 +1,167 @@
package shootergame.entity;
import java.util.Random;
import shootergame.Main;
import shootergame.display.Camera;
import shootergame.entity.particle.ParticleBlood;
import shootergame.entity.particle.ParticleSpark;
import shootergame.init.Textures;
import shootergame.init.Tiles;
import shootergame.tiles.TileBlackened;
import shootergame.tiles.TileStone;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.TileState;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class EntityTnt extends EntityVertical
{
private double height = 0.4;
private Vec3d velocity;
private int explode_time;
private int explode_radius;
private Random rand = new Random();
public EntityTnt(Vec2d pos, double angle, int explode_radius) {
super(Textures.ENTITY_TNT, 0.5);
velocity = MathHelpers.moveTowards3(0.1, new Vec2d(Math.toRadians(angle), Math.toRadians(20)));
this.pos = pos;
this.explode_radius = explode_radius;
// Set to 2.5 seconds
this.explode_time = 250;
}
@Override
public void tick(Chunk chunk, Layer layer) {
super.tick(chunk, layer);
double nx = pos.x + velocity.x;
double ny = pos.y + velocity.y;
height += velocity.z;
// Create a downward gravitational pull
velocity.z -= 0.001;
// Reduce the velocty
velocity.x /= 1.01;
velocity.y /= 1.01;
velocity.z /= 1.01;
// Make the tnt bounce on the ground
if(height < 0) {
height = 0;
velocity.z = -velocity.z;
}
// Make the tnt bounce off obsticles
if(moveIsLegal(new Vec2d(nx, pos.y)))
pos.x = nx;
else velocity.x *= -1;
if(moveIsLegal(new Vec2d(pos.y, ny)))
pos.y = ny;
else velocity.y *= -1;
// Is it time for the tnt to blow up
explode_time -= 1;
if(explode_time < 0)
{
// Loop over the tiles around the tnt
for(int ex=-explode_radius;ex<explode_radius;ex++) {
for(int ey=-explode_radius;ey<explode_radius;ey++)
{
// Calculate the distance from the explosion centre
double distance = Math.sqrt(ex*ex + ey*ey);
// Is the point within the distance from the explosion centre
if(distance < explode_radius)
{
// Get the actual tile position
double px = ex + pos.x;
double py = ey + pos.y;
// Get the layer
Layer l = Main.world.getLayer();
// Calculate the blackened gradient
short blackened_gradient = (short)( Short.MAX_VALUE - distance/explode_radius*Short.MAX_VALUE );
// Get the front tile
Vec2i tpos = new Vec2i(MathHelpers.floor(px), MathHelpers.floor(py));
TileState bts = l.getBackTile(tpos);
TileState fts = l.getFrontTile(tpos);
if(bts.tile instanceof TileStone) {
if(bts.meta > blackened_gradient) blackened_gradient = bts.meta;
}
// Set the tiles
if(!bts.tile.unbreakable) l.setBackTile(new TileState(Tiles.STONE,
(short)blackened_gradient), tpos);
if(!fts.tile.unbreakable) l.setFrontTile(Tiles.VOID.getDefaultState(), tpos);
// Summon some blood
l.spawnEntity(new ParticleBlood(rand, new Vec2d(px, py), py));
}
}
}
// Delete the entity
kill();
return;
}
// Create sparks
chunk.spawnEntity(new ParticleSpark(pos.copy(), height));
}
@Override
public void render(Vec2d pos, Camera camera)
{
// Render the tnt with the height
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, height);
super.render(pos, camera);
GlHelpers.popMatrix();
}
@Override
public boolean moveIsLegal(Vec2d pos)
{
// Get the position of the tile the tnt is over
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
// Get the foreground tile and the background tile
Layer l = Main.world.getLayer();
TileState tile_f = l.getFrontTile(tpos);
TileState tile_b = l.getBackTile(tpos);
// Is the tile solid and has the tnt 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)
{
// Send back false
return false;
}
}
if(tile_b.tile.tileSolid) {
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < tile_b.tile.tileHitbox)
{
// Send back false
return false;
}
}
// Send back true by default
return true;
}
}

View File

@ -44,7 +44,7 @@ public class EntityZombie extends EntityVertical implements EntityAlive
// Move forward towards the player
this.angle = Math.toDegrees(angle) + 180;
this.angle += noise_movement.eval(time, 0)*60;
this.moveForward(0.05);
this.moveForward();
if(noise_gun_fire.eval(time, 0) > 0)
{
@ -64,7 +64,16 @@ public class EntityZombie extends EntityVertical implements EntityAlive
// Increase time
time += 0.001;
}
@Override
public void moveForward() {
this.moveForward(0.06);
}
@Override
public void moveBackward(double speed) {
super.moveBackward(0.06);
}
@Override

View File

@ -9,10 +9,12 @@ import shootergame.world.layer.Layer;
public class ParticleSpark extends EntityParticle
{
private double size = 1;
private double size = 0.1;
public ParticleSpark(int height) {
super(1, height);
public ParticleSpark(Vec2d pos, double height) {
super(1, height+0.4);
this.pos = pos;
this.opaqueTile = false;
}
@Override
@ -20,7 +22,7 @@ public class ParticleSpark extends EntityParticle
super.tick(chunk, layer);
// Reduce the size
size -= 0.01;
size -= 0.004;
setSize(size);
// Is the size zero
@ -35,10 +37,12 @@ public class ParticleSpark extends EntityParticle
public void render(Vec2d pos, Camera camera)
{
// Set some settings
GlHelpers.pushMatrix();
GlHelpers.color3(1, 1, 0);
// Call super
super.render(pos, camera);
GlHelpers.popMatrix();
}
}

View File

@ -0,0 +1,57 @@
package shootergame.entity.particle;
import java.util.Random;
import shootergame.display.Camera;
import shootergame.entity.EntityParticle;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class ParticleWater extends EntityParticle
{
private static Random rand = new Random();
private Vec3d velocity;
private double height = 0;
public ParticleWater(Vec2d pos) {
super(rand.nextDouble()/5, 0);
// Set the velocity
velocity = MathHelpers.moveTowards3(0.1, new Vec2d(Math.toRadians(
RandomHelpers.randrange(rand, 360)), Math.toRadians(RandomHelpers.randrange(rand, 0, 45))));
this.pos = pos;
}
@Override
public void tick(Chunk chunk, Layer layer) {
super.tick(chunk, layer);
// Add the velocity
velocity.z -= 0.005;
pos.x += velocity.x;
pos.y += velocity.y;
height += velocity.z;
// Is the height below 0; destroy this particle
if(height < 0) {
kill();
}
}
@Override
public void render(Vec2d pos, Camera camera) {
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, height);
GlHelpers.color4(0, 0, 1, 0.4);
super.render(pos, camera);
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.popMatrix();
}
}

View File

@ -8,9 +8,11 @@ import shootergame.display.Camera;
import shootergame.entity.Entity;
import shootergame.entity.EntityAlive;
import shootergame.entity.EntityBullet;
import shootergame.entity.EntityTnt;
import shootergame.entity.EntityVertical;
import shootergame.init.Sounds;
import shootergame.init.Textures;
import shootergame.util.gl.GlHelpers;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.vec.Vec2d;
@ -28,6 +30,8 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
public boolean GUN = false;
public boolean moving = false;
public double height = 0;
private int bullet_frequency = 0;
private Random rand;
private double health_max = 1000;
@ -84,18 +88,35 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
}
}
@Override
public void moveForward() {
this.moveForward(0.08);
}
@Override
public void moveBackward(double speed) {
super.moveBackward(0.08);
}
@Override
public void render(Vec2d pos, Camera camera)
{
// Don't render if the player is dead
if(this.getHealth() < 0) return;
// Translation
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, height);
// Moving
if(MOVE_BACKWARD || MOVE_FORWARD || moving)
super.render(pos, camera, Textures.ENTITY_PLAYER_MOVING, 1);
// Standing still
else super.render(pos, camera, Textures.ENTITY_PLAYER_STILL, 1);
// Pop the matrix
GlHelpers.popMatrix();
}
public void fireBullet(double angle)
@ -107,8 +128,16 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
{
// Summon bullets at this angle relative to the player
Main.world.getLayer().spawnEntity(new EntityBullet(rand, pos.copy(), this, angle + this.angle));
//throwTnt(angle);
}
}
private boolean throwTnt_last = false;
public void throwTnt(double angle)
{
Main.world.getLayer().spawnEntity(new EntityTnt(pos.copy(), angle + this.angle, 10));
}
@Override
public void addHealth(double amount) {

View File

@ -7,9 +7,37 @@ public class Resources
public static void loadResources()
{
TEXMAP_PNG.load();
GUN_OGG.load();
GUN_OGG_0.load();
GUN_OGG_1.load();
GUN_OGG_2.load();
GUN_OGG_3.load();
GUN_OGG_4.load();
GUN_OGG_5.load();
GUN_OGG_6.load();
GUN_OGG_7.load();
GUN_OGG_8.load();
GUN_OGG_9.load();
HIT_OGG_0.load();
HIT_OGG_1.load();
HIT_OGG_2.load();
}
public static final Resource TEXMAP_PNG = new Resource("texmap.png");
public static final Resource GUN_OGG = new Resource("sound/gun.ogg");
public static final Resource GUN_OGG_0 = new Resource("sound/gun0.ogg");
public static final Resource GUN_OGG_1 = new Resource("sound/gun1.ogg");
public static final Resource GUN_OGG_2 = new Resource("sound/gun2.ogg");
public static final Resource GUN_OGG_3 = new Resource("sound/gun3.ogg");
public static final Resource GUN_OGG_4 = new Resource("sound/gun4.ogg");
public static final Resource GUN_OGG_5 = new Resource("sound/gun5.ogg");
public static final Resource GUN_OGG_6 = new Resource("sound/gun6.ogg");
public static final Resource GUN_OGG_7 = new Resource("sound/gun7.ogg");
public static final Resource GUN_OGG_8 = new Resource("sound/gun8.ogg");
public static final Resource GUN_OGG_9 = new Resource("sound/gun9.ogg");
public static final Resource HIT_OGG_0 = new Resource("sound/hit0.ogg");
public static final Resource HIT_OGG_1 = new Resource("sound/hit1.ogg");
public static final Resource HIT_OGG_2 = new Resource("sound/hit2.ogg");
}

View File

@ -1,13 +1,47 @@
package shootergame.init;
import shootergame.audio.AudioObject;
import shootergame.audio.AudioRandom;
public class Sounds
{
public static void init()
{
GUN.init();
GUN_0.init();
GUN_1.init();
GUN_2.init();
GUN_3.init();
GUN_4.init();
GUN_5.init();
GUN_6.init();
GUN_7.init();
GUN_8.init();
GUN_9.init();
HIT_0.init();
HIT_1.init();
HIT_2.init();
}
public static final AudioObject GUN = new AudioObject(Resources.GUN_OGG);
public static final AudioObject GUN_0 = new AudioObject(Resources.GUN_OGG_0);
public static final AudioObject GUN_1 = new AudioObject(Resources.GUN_OGG_1);
public static final AudioObject GUN_2 = new AudioObject(Resources.GUN_OGG_2);
public static final AudioObject GUN_3 = new AudioObject(Resources.GUN_OGG_3);
public static final AudioObject GUN_4 = new AudioObject(Resources.GUN_OGG_4);
public static final AudioObject GUN_5 = new AudioObject(Resources.GUN_OGG_5);
public static final AudioObject GUN_6 = new AudioObject(Resources.GUN_OGG_6);
public static final AudioObject GUN_7 = new AudioObject(Resources.GUN_OGG_7);
public static final AudioObject GUN_8 = new AudioObject(Resources.GUN_OGG_8);
public static final AudioObject GUN_9 = new AudioObject(Resources.GUN_OGG_9);
public static final AudioObject GUN = new AudioRandom(
GUN_0,GUN_1,GUN_2,GUN_3,GUN_4,
GUN_5,GUN_6,GUN_7,GUN_8,GUN_9);
public static final AudioObject HIT_0 = new AudioObject(Resources.HIT_OGG_0);
public static final AudioObject HIT_1 = new AudioObject(Resources.HIT_OGG_1);
public static final AudioObject HIT_2 = new AudioObject(Resources.HIT_OGG_2);
public static final AudioObject HIT = new AudioRandom(
HIT_0, HIT_1, HIT_2);
}

View File

@ -31,6 +31,11 @@ public class Textures
public static final TextureReference TILE_DIRT = texmap.getTextureReference(3, 4, 0, 1);
public static final TextureReference TILE_TREE = texmap.getTextureReference(4, 5, 0, 4);
public static final TextureReference TILE_ROCK = texmap.getTextureReference(4, 5, 4, 5);
public static final TextureReference ENTITY_TNT = texmap.getTextureReference(4, 5, 5, 6);
public static final TextureReference TILE_LADDER = texmap.getTextureReference(3, 4, 4, 5);
public static final TextureReference TILE_PORTAL = texmap.getTextureReference(3, 4, 5, 6);
public static final TextureReference TILE_WALL = texmap.getTextureReference(2, 3, 5, 6);
public static final TextureReference TILE_LADDER_UP = texmap.getTextureReference(16, 17, 0, 16);
// Fire
public static final TextureReference TILE_FIRE_0 = texmap.getTextureReference(0, 1, 1, 2);
@ -58,7 +63,7 @@ public class Textures
);
// Water
public static final TextureReference TILE_WATER = new AnimationReference(20,
public static final TextureReference TILE_WATER = new AnimationReference(10,
texmap.getTextureReference(0, 1, 8, 9),
texmap.getTextureReference(1, 2, 8, 9),
texmap.getTextureReference(2, 3, 8, 9),
@ -78,7 +83,7 @@ public class Textures
);
// Lava
public static final TextureReference TILE_LAVA = new AnimationReference(10,
public static final TextureReference TILE_LAVA = new AnimationReference(20,
texmap.getTextureReference(0, 1, 6, 7),
texmap.getTextureReference(1, 2, 6, 7),
texmap.getTextureReference(2, 3, 6, 7),

View File

@ -1,16 +1,21 @@
package shootergame.init;
import shootergame.tiles.Tile;
import shootergame.tiles.TileBlackened;
import shootergame.tiles.TileDirt;
import shootergame.tiles.TileFire;
import shootergame.tiles.TileGrass;
import shootergame.tiles.TileLadder;
import shootergame.tiles.TileLadderUp;
import shootergame.tiles.TileLava;
import shootergame.tiles.TileLavaFlow;
import shootergame.tiles.TilePortalDown;
import shootergame.tiles.TileRock;
import shootergame.tiles.TileSand;
import shootergame.tiles.TileStone;
import shootergame.tiles.TileTree;
import shootergame.tiles.TileVoid;
import shootergame.tiles.TileWall;
import shootergame.tiles.TileWater;
import shootergame.tiles.TileWaterFlow;
@ -28,4 +33,9 @@ public class Tiles
public static final Tile WATER = new TileWater("water");
public static final Tile LAVA_FLOW = new TileLavaFlow("lava_flow");
public static final Tile WATER_FLOW = new TileWaterFlow("water_flow");
public static final Tile BLACKENED = new TileBlackened("blackened");
public static final Tile LADDER = new TileLadder("ladder");
public static final Tile PORTAL_DOWN = new TilePortalDown("portal_down");
public static final Tile WALL = new TileWall("wall");
public static final Tile LADDER_UP = new TileLadderUp("ladder_up");
}

View File

@ -14,6 +14,9 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
{
public static final JoystickCallback JOYSTICK_CALLBACK = new JoystickCallback();
private ArrayList<Integer> connections = new ArrayList<Integer>();
private static boolean throwTnt_last = false;
private static boolean activate_last = false;
@Override
public void invoke(int jid, int event)
@ -73,15 +76,13 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
private float combineJoystickAxis(float a, float b)
{
if(b > 0.2 || b < -0.2) a += b;
if(b > 0.3 || b < -0.3) a += b;
return a;
}
@Override
public void MainLoopUpdate()
{
// Gamepad properties
float left_x = 0;
float left_y = 0;
@ -147,9 +148,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
double angle = Math.toDegrees(Math.atan2(left_y, left_x)) + 90;
// Move the player in the left sticks angle
Main.player.angle += angle;
Main.player.moveForward();
Main.player.angle -= angle;
Main.player.moveTowards(angle);
// Set the players moving to true
Main.player.moving = true;
@ -160,6 +159,7 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
// Is the right x axis stick moved into a position (camera stick)
if(right_x > 0.3 || right_x < -0.3) {
System.out.println("Angle changed");
Main.player.angle += right_x;
}
@ -168,6 +168,19 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
Main.player.fireBullet(0);
}
// Tnt trigger
if(left_trigger > 0.3) {
if(!throwTnt_last)
{
throwTnt_last = true;
Main.player.throwTnt(0);
}
}
else {
throwTnt_last = false;
}
if(dpad_up) {
Main.player.fireBullet(0);
}
@ -183,6 +196,18 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
if(dpad_right) {
Main.player.fireBullet(90);
}
// Activate block
if(button_x) {
if(!activate_last) {
Main.player.activateTile();
activate_last = true;
}
}
else if(activate_last) {
activate_last = false;
}
}
}

View File

@ -5,6 +5,7 @@ import shootergame.display.transparent.ITransparentObject;
import shootergame.display.transparent.TransparentObjects;
import shootergame.entity.Entity;
import shootergame.entity.player.EntityPlayer;
import shootergame.util.math.TileState;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
@ -18,6 +19,8 @@ public class Tile implements ITransparentObject
public boolean tileSolid = false;
public boolean tileWalkable = true;
public double tileHitbox = 0;
public double slowness = 0;
public boolean unbreakable = false;
public Tile(String id) {
this.id = id;
@ -28,17 +31,17 @@ public class Tile implements ITransparentObject
}
@Override
public void render(Vec2d pos, Camera camera) {
public void render(Vec2d pos, Camera camera, short meta) {
}
public void doRender(Vec2d pos, Camera camera)
public void doRender(Vec2d pos, Camera camera, short meta)
{
if(this.opaqueTile) {
TransparentObjects.register(this, camera, pos);
TransparentObjects.register(this, camera, pos, meta);
}
else {
this.render(pos, camera);
this.render(pos, camera, meta);
}
}
@ -47,11 +50,18 @@ public class Tile implements ITransparentObject
return this.opaqueTile;
}
public void onWalkedOn(Chunk chunk, Layer layer, Vec2i pos, Entity entity) {
public void onWalkedOn(Chunk chunk, Layer layer, Vec2i pos, Entity entity, short meta) {
}
@Override
public Vec2d getRenderOffset() {
return new Vec2d(0.5, 0.5);
}
public TileState getDefaultState() {
return new TileState(this, (short)0);
}
public void onActivated(Chunk chunk, Layer layer, Vec2i tpos, Entity entity, short meta) {
}
}

View File

@ -0,0 +1,39 @@
package shootergame.tiles;
import shootergame.display.Camera;
import shootergame.util.gl.GlHelpers;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.vec.Vec2d;
public class TileBlackened extends Tile
{
public TileBlackened(String id) {
super(id);
//this.opaqueTile = true;
}
@Override
public void render(Vec2d pos, Camera camera, short meta)
{
//System.out.println("META: "+ (int)meta);
// Call super
super.render(pos, camera, meta);
// Set the colour
GlHelpers.color4(0, 0, 0, ((double)meta) / Short.MAX_VALUE);
// Draw the tile
GlHelpers.begin();
GlHelpers.vertex3(pos.x+0, pos.y+0, 0.001);
GlHelpers.vertex3(pos.x+1, pos.y+0, 0.001);
GlHelpers.vertex3(pos.x+1, pos.y+1, 0.001);
GlHelpers.vertex3(pos.x+0, pos.y+1, 0.001);
GlHelpers.end();
// Reset the colour
GlHelpers.color4(1, 1, 1, 1);
}
}

View File

@ -18,10 +18,10 @@ public class TileFlat extends Tile
}
@Override
public void render(Vec2d pos, Camera camera)
public void render(Vec2d pos, Camera camera, short meta)
{
// Call super
super.render(pos, camera);
super.render(pos, camera, meta);
// Render the tile
GlHelpers.begin();

View File

@ -0,0 +1,18 @@
package shootergame.tiles;
import shootergame.init.Textures;
import shootergame.util.gl.texture.TextureReference;
public class TileLadder extends TileVertical
{
public TileLadder(String id) {
super(id, Textures.TILE_LADDER, 1);
this.opaqueTile = true;
this.tileSolid = true;
this.tileHitbox = 0.3;
this.unbreakable = true;
}
}

View File

@ -0,0 +1,88 @@
package shootergame.tiles;
import mainloop.task.IMainloopTask;
import shootergame.Main;
import shootergame.entity.Entity;
import shootergame.entity.player.EntityPlayer;
import shootergame.init.Textures;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class TileLadderUp extends TileVertical
{
public TileLadderUp(String id) {
super(id, Textures.TILE_LADDER_UP, 16);
this.opaqueTile = true;
this.tileSolid = true;
this.tileHitbox = 0.3;
this.unbreakable = true;
}
@Override
public void onActivated(Chunk chunk, Layer layer, Vec2i pos, Entity entity, short meta)
{
// Call super
super.onWalkedOn(chunk, layer, pos, entity, meta);
// Is the entity the player
if(entity == Main.player)
{
// Cast to player
EntityPlayer player = (EntityPlayer)entity;
double playerAngle = player.angle;
Vec2d playerPos = player.pos.copy();
player.height = 0;
// Register the animation
Main.mainloop.register(new IMainloopTask() {
int movingPlayer = 0;
@Override
public void MainLoopUpdate()
{
player.angle = playerAngle;
player.pos = playerPos.copy();
player.moving = true;
if(movingPlayer == 0) {
player.height += 0.04;
}
if(movingPlayer == 1) {
player.height += 0.02;
}
if(player.height >= 6 && movingPlayer == 0)
{
movingPlayer = 1;
Main.world.setLayerID(meta);
player.height = -1;
}
if(player.height >= 0 && movingPlayer == 1)
{
movingPlayer = 2;
player.height = 0;
player.moving = false;
}
}
@Override
public boolean MainLoopRepeat() {
return movingPlayer != 2;
}
@Override
public boolean MainLoopDelay(long arg0) {
return arg0 > 10;
}
});
}
}
}

View File

@ -15,10 +15,10 @@ public class TileLava extends TileFlat
}
@Override
public void render(Vec2d pos, Camera camera) {
public void render(Vec2d pos, Camera camera, short meta) {
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, 0.001);
super.render(pos, camera);
super.render(pos, camera, meta);
GlHelpers.popMatrix();
}

View File

@ -13,10 +13,10 @@ public class TileLavaFlow extends TileFlat
}
@Override
public void render(Vec2d pos, Camera camera) {
public void render(Vec2d pos, Camera camera, short meta) {
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, 0.001);
super.render(pos, camera);
super.render(pos, camera, meta);
GlHelpers.popMatrix();
}

View File

@ -0,0 +1,99 @@
package shootergame.tiles;
import mainloop.task.IMainloopTask;
import shootergame.Main;
import shootergame.entity.Entity;
import shootergame.entity.player.EntityPlayer;
import shootergame.init.Textures;
import shootergame.init.Tiles;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class TilePortalDown extends TileFlat
{
public TilePortalDown(String id) {
super(id, Textures.TILE_PORTAL);
this.unbreakable = true;
}
@Override
public void onActivated(Chunk chunk, Layer layer, Vec2i pos, Entity entity, short meta)
{
// Call super
super.onWalkedOn(chunk, layer, pos, entity, meta);
// Is the entity the player
if(entity == Main.player)
{
// Cast to player
EntityPlayer player = (EntityPlayer)entity;
double playerAngle = player.angle;
Vec2d playerPos = player.pos.copy();
player.height = 0;
// Register the animation
Main.mainloop.register(new IMainloopTask() {
int movingPlayer = 0;
@Override
public void MainLoopUpdate()
{
player.angle = playerAngle;
player.pos = playerPos.copy();
player.moving = true;
if(movingPlayer == 0) {
player.height -= 0.02;
}
if(movingPlayer == 1) {
player.height -= 0.04;
}
if(player.height < -1 && movingPlayer == 0)
{
movingPlayer = 1;
Main.world.setLayerID(meta);
player.height = 6;
}
if(player.height < 0 && movingPlayer == 1)
{
movingPlayer = 2;
player.height = 0;
player.moving = false;
Layer layer = Main.world.getLayer();
for(int i=0;i<=16;i++) {
if(layer.getBackTile(new Vec2i(pos.x+i, pos.y)).tile != Tiles.STONE)
layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x+i, pos.y));
if(layer.getBackTile(new Vec2i(pos.x-i, pos.y)).tile != Tiles.STONE)
layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x-i, pos.y));
if(layer.getBackTile(new Vec2i(pos.x, pos.y+i)).tile != Tiles.STONE)
layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x, pos.y+i));
if(layer.getBackTile(new Vec2i(pos.x, pos.y-i)).tile != Tiles.STONE)
layer.setBackTile(Tiles.STONE.getDefaultState(), new Vec2i(pos.x, pos.y-i));
}
}
}
@Override
public boolean MainLoopRepeat() {
return movingPlayer != 2;
}
@Override
public boolean MainLoopDelay(long arg0) {
return arg0 > 10;
}
});
}
}
}

View File

@ -1,6 +1,9 @@
package shootergame.tiles;
import shootergame.display.Camera;
import shootergame.init.Textures;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.vec.Vec2d;
public class TileStone extends TileFlat
{
@ -8,5 +11,12 @@ public class TileStone extends TileFlat
public TileStone(String id) {
super(id, Textures.TILE_STONE);
}
@Override
public void render(Vec2d pos, Camera camera, short meta) {
GlHelpers.color4(1, 1, 1, (Short.MAX_VALUE - (double)meta) / Short.MAX_VALUE);
super.render(pos, camera, meta);
GlHelpers.color4(1, 1, 1, 1);
}
}

View File

@ -23,8 +23,8 @@ public class TileVertical extends Tile
}
@Override
public void render(Vec2d pos, Camera camera) {
super.render(pos, camera);
public void render(Vec2d pos, Camera camera, short meta) {
super.render(pos, camera, meta);
VerticalRender.render(pos, camera, tex, h);
}

View File

@ -0,0 +1,17 @@
package shootergame.tiles;
import shootergame.init.Textures;
import shootergame.util.gl.texture.TextureReference;
public class TileWall extends TileFlat
{
public TileWall(String id) {
super(id, Textures.TILE_WALL);
this.tileWalkable = false;
this.tileSolid = true;
this.tileHitbox = 1;
}
}

View File

@ -1,25 +1,41 @@
package shootergame.tiles;
import shootergame.display.Camera;
import shootergame.entity.Entity;
import shootergame.entity.particle.ParticleWater;
import shootergame.init.Textures;
import shootergame.util.gl.GlHelpers;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class TileWater extends TileFlat
{
public TileWater(String id) {
super(id, Textures.TILE_WATER);
this.tileWalkable = false;
this.slowness = 0.5;
}
@Override
public void render(Vec2d pos, Camera camera) {
public void render(Vec2d pos, Camera camera, short meta) {
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, 0.001);
super.render(pos, camera);
super.render(pos, camera, meta);
GlHelpers.popMatrix();
}
@Override
public void onWalkedOn(Chunk chunk, Layer layer, Vec2i pos, Entity entity, short meta)
{
// Call super
super.onWalkedOn(chunk, layer, pos, entity, meta);
// Spawn some water particles
for(int i=0;i<4;i++) {
layer.spawnEntity(new ParticleWater(entity.pos.copy()));
}
}
}

View File

@ -13,10 +13,10 @@ public class TileWaterFlow extends TileFlat
}
@Override
public void render(Vec2d pos, Camera camera) {
public void render(Vec2d pos, Camera camera, short meta) {
GlHelpers.pushMatrix();
GlHelpers.translate(0, 0, 0.001);
super.render(pos, camera);
super.render(pos, camera, meta);
GlHelpers.popMatrix();
}

View File

@ -83,4 +83,16 @@ public class MathHelpers
if(a < 0) return -a;
else return a;
}
public static Vec2d rotate2(Vec2d pos, double angle)
{
// Get the angle and the distance from the centre to the point
double p_angle = Math.atan2(pos.y, pos.x);
double p_distance = Math.sqrt(pos.y*pos.y + pos.x*pos.x);
// Return and calculate the new positions
return new Vec2d(
p_distance * Math.sin(angle - p_angle),
p_distance * Math.cos(angle - p_angle));
}
}

View File

@ -0,0 +1,21 @@
package shootergame.util.math;
import shootergame.init.Tiles;
import shootergame.tiles.Tile;
public class TileState
{
public static final TileState EMPTY = new TileState(Tiles.VOID, (short)0);
public Tile tile;
public short meta;
public TileState(Tile tile, short meta) {
this.tile = tile;
this.meta = meta;
}
public TileState withMeta(short meta) {
return new TileState(tile, meta);
}
}

View File

@ -15,11 +15,13 @@ public class World
public World(Random rand, LayerGen ... layergen)
{
long seed = rand.nextLong();
// Loop over the layer generators
for(LayerGen lg : layergen)
{
// Create new layers
layers.add(new Layer(rand, lg));
layers.add(new Layer(new Random(seed), lg));
}
// Set the current layer
@ -44,6 +46,7 @@ public class World
public void setLayerID(int id) {
layer_id = id;
layer = layers.get(layer_id);
}
public Layer getLayer() {

View File

@ -9,6 +9,7 @@ import shootergame.entity.EntityAlive;
import shootergame.init.Tiles;
import shootergame.tiles.Tile;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.TileState;
import shootergame.util.math.range.Range2i;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
@ -23,6 +24,8 @@ public class Chunk
private Tile tiles_back[] = new Tile[CHUNK_INDEX];
private Tile tiles_front[] = new Tile[CHUNK_INDEX];
private short tiles_front_meta[] = new short[CHUNK_INDEX];
private short tiles_back_meta[] = new short[CHUNK_INDEX];
public ArrayList<Entity> entities = new ArrayList<Entity>();
private Random rand;
private Layer layer;
@ -53,8 +56,8 @@ public class Chunk
Vec2i t_pos = Vec2i.fromId(CHUNK_SIZE, i);
// Render the tiles
tiles_back[i].doRender(new Vec2d(t_pos.x + c_pos.x * 16, t_pos.y + c_pos.y * 16), camera);
tiles_front[i].doRender(new Vec2d(t_pos.x + c_pos.x * 16, t_pos.y + c_pos.y * 16), camera);
tiles_back[i].doRender(new Vec2d(t_pos.x + c_pos.x * 16, t_pos.y + c_pos.y * 16), camera, tiles_back_meta[i]);
tiles_front[i].doRender(new Vec2d(t_pos.x + c_pos.x * 16, t_pos.y + c_pos.y * 16), camera, tiles_front_meta[i]);
}
// Render all the entities
@ -116,7 +119,7 @@ public class Chunk
}
}
public void setBackTile(Tile tile, Vec2i pos)
public void setBackTile(TileState tile, Vec2i pos)
{
// Get the id
Vec2i cpos = new Vec2i(0, 0);
@ -125,10 +128,11 @@ public class Chunk
int id = cpos.getId(CHUNK_SIZE);
// Set the back tile
this.tiles_back[id] = tile;
this.tiles_back[id] = tile.tile;
this.tiles_back_meta[id] = tile.meta;
}
public void setFrontTile(Tile tile, Vec2i pos)
public void setFrontTile(TileState tile, Vec2i pos)
{
// Get the id
Vec2i cpos = new Vec2i(0, 0);
@ -137,10 +141,11 @@ public class Chunk
int id = cpos.getId(CHUNK_SIZE);
// Set the front tile
this.tiles_front[id] = tile;
this.tiles_front[id] = tile.tile;
this.tiles_front_meta[id] = tile.meta;
}
public Tile getBackTile(Vec2i pos)
public TileState getBackTile(Vec2i pos)
{
// Get the id
Vec2i cpos = new Vec2i(0, 0);
@ -149,10 +154,10 @@ public class Chunk
int id = cpos.getId(CHUNK_SIZE);
// Send back the back tile
return this.tiles_back[id];
return new TileState(this.tiles_back[id], this.tiles_back_meta[id]);
}
public Tile getFrontTile(Vec2i pos)
public TileState getFrontTile(Vec2i pos)
{
// Get the id
Vec2i cpos = new Vec2i(0, 0);
@ -161,7 +166,7 @@ public class Chunk
int id = cpos.getId(CHUNK_SIZE);
// Send back the front tile
return this.tiles_front[id];
return new TileState(this.tiles_front[id], this.tiles_front_meta[id]);
}
public void killEntity(Entity e) {

View File

@ -6,6 +6,7 @@ import shootergame.display.Camera;
import shootergame.entity.Entity;
import shootergame.init.Tiles;
import shootergame.tiles.Tile;
import shootergame.util.math.TileState;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
@ -27,20 +28,20 @@ public class ChunkEmpty extends Chunk
public void spawnEntity(Entity e) {}
@Override
public Tile getBackTile(Vec2i pos) {
return Tiles.VOID;
public TileState getBackTile(Vec2i pos) {
return TileState.EMPTY;
}
@Override
public Tile getFrontTile(Vec2i pos) {
return Tiles.VOID;
public TileState getFrontTile(Vec2i pos) {
return TileState.EMPTY;
}
@Override
public void setBackTile(Tile tile, Vec2i pos) {}
public void setBackTile(TileState tile, Vec2i pos) {}
@Override
public void setFrontTile(Tile tile, Vec2i pos) {}
public void setFrontTile(TileState tile, Vec2i pos) {}
@Override
public void checkEntities() {}

View File

@ -8,6 +8,7 @@ import shootergame.display.Camera;
import shootergame.entity.Entity;
import shootergame.tiles.Tile;
import shootergame.util.math.MathHelpers;
import shootergame.util.math.TileState;
import shootergame.util.math.map.Map2D;
import shootergame.util.math.map.Map2DElement;
import shootergame.util.math.vec.Vec2d;
@ -65,7 +66,7 @@ public class Layer
this.layergen.spawnEntities(this, rand);
}
public void setBackTile(Tile tile, Vec2i pos)
public void setBackTile(TileState tile, Vec2i pos)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
@ -74,7 +75,7 @@ public class Layer
chunks.get(c_pos).setBackTile(tile, pos);
}
public void setFrontTile(Tile tile, Vec2i pos)
public void setFrontTile(TileState tile, Vec2i pos)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
@ -83,7 +84,7 @@ public class Layer
chunks.get(c_pos).setFrontTile(tile, pos);
}
public Tile getBackTile(Vec2i pos)
public TileState getBackTile(Vec2i pos)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);
@ -102,7 +103,7 @@ public class Layer
MathHelpers.floor(pos.y / (double)Chunk.CHUNK_SIZE.my));
}
public Tile getFrontTile(Vec2i pos)
public TileState getFrontTile(Vec2i pos)
{
// Get the chunk pos
Vec2i c_pos = getChunkPosFromPos(pos);

View File

@ -0,0 +1,71 @@
package shootergame.world.layer.layergen;
import java.util.Random;
import org.lwjgl.stb.STBPerlin;
import shootergame.init.Tiles;
import shootergame.util.math.TileState;
import shootergame.util.math.random.OpenSimplexNoise;
import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.vec.Vec2i;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
public class LayerGenCaves extends LayerGen
{
@Override
public Chunk generateChunk(Layer layer, long seed, Random rand, Vec2i c_pos)
{
// Create a new chunk
Chunk chunk = new Chunk(layer, c_pos, rand);
// Is there going to be a portal in this chunk
boolean portal = RandomHelpers.randrange(rand, 10) == 0;
Vec2i portal_pos = null;
if(portal) portal_pos = new Vec2i(
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx),
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my));
// Get some noise generators
OpenSimplexNoise noisegen_n = new OpenSimplexNoise(seed);
// Loop over the chunk
for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
for(int y=0;y<Chunk.CHUNK_SIZE.my;y++)
{
// Get the x and y in the world
int cx = x + c_pos.x * Chunk.CHUNK_SIZE.mx;
int cy = y + c_pos.y * Chunk.CHUNK_SIZE.my;
// Get the noise value and the position vector
double noise_n = (noisegen_n.eval(cx/10.0, cy/10.0) + 1) * 50;
Vec2i pos = new Vec2i(x, y);
if(noise_n > 60) {
chunk.setBackTile(Tiles.STONE.getDefaultState(), pos);
}
else {
chunk.setBackTile(Tiles.WALL.getDefaultState(), pos);
}
if(portal) {
chunk.setFrontTile(new TileState(Tiles.LADDER_UP, (short)0), portal_pos);
}
}
}
// Send the chunk back
return chunk;
}
@Override
public void spawnEntities(Layer layer, Random rand)
{
// TODO Auto-generated method stub
}
}

View File

@ -8,6 +8,7 @@ import shootergame.entity.EntityBullet;
import shootergame.entity.EntityZombie;
import shootergame.init.Tiles;
import shootergame.tiles.Tile;
import shootergame.util.math.TileState;
import shootergame.util.math.random.OpenSimplexNoise;
import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.range.Range2i;
@ -25,6 +26,13 @@ public class LayerGenEarth extends LayerGen
// Create the new chunk
Chunk chunk = new Chunk(layer, c_pos, rand);
// Is there going to be a portal in this chunk
boolean portal = RandomHelpers.randrange(rand, 10) == 0;
Vec2i portal_pos = null;
if(portal) portal_pos = new Vec2i(
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.mx),
RandomHelpers.randrange(rand, Chunk.CHUNK_SIZE.my));
// Get the noise generator
OpenSimplexNoise noisegen_n = new OpenSimplexNoise(seed);
@ -41,18 +49,23 @@ public class LayerGenEarth extends LayerGen
Vec2i pos = new Vec2i(x, y);
// Tree and rock generation
if(rand.nextDouble() > 0.9) chunk.setFrontTile(Tiles.TREE, pos);
else if(rand.nextDouble() > 0.99) chunk.setFrontTile(Tiles.ROCK, pos);
else chunk.setFrontTile(Tiles.VOID, pos);
if(rand.nextDouble() > 0.9) chunk.setFrontTile(Tiles.TREE.getDefaultState(), pos);
else if(rand.nextDouble() > 0.99) chunk.setFrontTile(Tiles.ROCK.getDefaultState(), pos);
else chunk.setFrontTile(Tiles.VOID.getDefaultState(), pos);
// Terrain generation
if(noise_n < 40) {
chunk.setFrontTile(Tiles.WATER, pos);
chunk.setBackTile(Tiles.DIRT, pos);
chunk.setFrontTile(Tiles.WATER.getDefaultState(), pos);
chunk.setBackTile(Tiles.DIRT.getDefaultState(), pos);
}
else if(noise_n < 70) chunk.setBackTile(Tiles.GRASS.getDefaultState(), pos);
else if(noise_n < 90) chunk.setBackTile(Tiles.DIRT.getDefaultState(), pos);
else chunk.setBackTile(Tiles.STONE.getDefaultState(), pos);
if(portal) {
chunk.setBackTile(new TileState(Tiles.PORTAL_DOWN, (short)1), portal_pos);
chunk.setFrontTile(Tiles.LADDER.getDefaultState(), portal_pos);
}
else if(noise_n < 70) chunk.setBackTile(Tiles.GRASS, pos);
else if(noise_n < 90) chunk.setBackTile(Tiles.DIRT, pos);
else chunk.setBackTile(Tiles.STONE, pos);
}
}