Added sounds, resource downloading, and fixed collision detection.
This commit is contained in:
parent
28be74ec0d
commit
6e17063619
Binary file not shown.
|
Before Width: | Height: | Size: 56 KiB After Width: | Height: | Size: 56 KiB |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 56 KiB |
|
|
@ -5,6 +5,8 @@ import java.util.Random;
|
||||||
import javax.swing.text.html.parser.Entity;
|
import javax.swing.text.html.parser.Entity;
|
||||||
|
|
||||||
import mainloop.manager.MainloopManager;
|
import mainloop.manager.MainloopManager;
|
||||||
|
import shootergame.audio.AudioEngine;
|
||||||
|
import shootergame.audio.AudioSources;
|
||||||
import shootergame.cheats.Cheats;
|
import shootergame.cheats.Cheats;
|
||||||
import shootergame.display.DisplayStatsEventHandler;
|
import shootergame.display.DisplayStatsEventHandler;
|
||||||
import shootergame.display.DisplayWindow;
|
import shootergame.display.DisplayWindow;
|
||||||
|
|
@ -12,11 +14,15 @@ import shootergame.entity.EntityEventHandler;
|
||||||
import shootergame.entity.EntityZombie;
|
import shootergame.entity.EntityZombie;
|
||||||
import shootergame.entity.player.EntityPlayer;
|
import shootergame.entity.player.EntityPlayer;
|
||||||
import shootergame.init.Entities;
|
import shootergame.init.Entities;
|
||||||
|
import shootergame.init.Resources;
|
||||||
|
import shootergame.init.Sounds;
|
||||||
import shootergame.init.Textures;
|
import shootergame.init.Textures;
|
||||||
import shootergame.input.JoystickCallback;
|
import shootergame.input.JoystickCallback;
|
||||||
import shootergame.mainloop.MainloopEventHandler;
|
import shootergame.mainloop.MainloopEventHandler;
|
||||||
|
import shootergame.resources.Resource;
|
||||||
import shootergame.util.math.MathHelpers;
|
import shootergame.util.math.MathHelpers;
|
||||||
import shootergame.util.math.vec.Vec2i;
|
import shootergame.util.math.vec.Vec2i;
|
||||||
|
import shootergame.util.math.vec.Vec3d;
|
||||||
import shootergame.world.World;
|
import shootergame.world.World;
|
||||||
import shootergame.world.chunk.ChunkEventHandler;
|
import shootergame.world.chunk.ChunkEventHandler;
|
||||||
import shootergame.world.layer.layergen.LayerGenEarth;
|
import shootergame.world.layer.layergen.LayerGenEarth;
|
||||||
|
|
@ -27,12 +33,24 @@ public class Main
|
||||||
public static DisplayWindow window;
|
public static DisplayWindow window;
|
||||||
public static EntityPlayer player = new EntityPlayer();
|
public static EntityPlayer player = new EntityPlayer();
|
||||||
public static World world;
|
public static World world;
|
||||||
|
public static AudioEngine audio;
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
// Initialize the cheats
|
// Initialize the cheats
|
||||||
Cheats.init(args);
|
Cheats.init(args);
|
||||||
|
|
||||||
|
// Load the resources
|
||||||
|
Resources.loadResources();
|
||||||
|
|
||||||
|
// Initialize the sound engine
|
||||||
|
audio = new AudioEngine();
|
||||||
|
audio.init();
|
||||||
|
|
||||||
|
// Initialise the sounds
|
||||||
|
AudioSources.init();
|
||||||
|
Sounds.init();
|
||||||
|
|
||||||
// Create the mainloop
|
// Create the mainloop
|
||||||
mainloop = new MainloopManager(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
|
mainloop = new MainloopManager(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
|
||||||
mainloop.register(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
|
mainloop.register(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
package shootergame;
|
||||||
|
|
||||||
|
import org.lwjgl.openal.*;
|
||||||
|
import org.lwjgl.system.*;
|
||||||
|
|
||||||
|
import java.nio.*;
|
||||||
|
|
||||||
|
import static org.lwjgl.openal.AL10.*;
|
||||||
|
import static org.lwjgl.openal.ALC10.*;
|
||||||
|
import static org.lwjgl.stb.STBVorbis.*;
|
||||||
|
import static org.lwjgl.system.MemoryStack.*;
|
||||||
|
import static org.lwjgl.system.libc.LibCStdlib.*;
|
||||||
|
|
||||||
|
public class Test
|
||||||
|
{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//Initialization
|
||||||
|
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
|
||||||
|
long device = alcOpenDevice(defaultDeviceName);
|
||||||
|
|
||||||
|
int[] attributes = {0};
|
||||||
|
long context = alcCreateContext(device, attributes);
|
||||||
|
alcMakeContextCurrent(context);
|
||||||
|
|
||||||
|
ALCCapabilities alcCapabilities = ALC.createCapabilities(device);
|
||||||
|
ALCapabilities alCapabilities = AL.createCapabilities(alcCapabilities);
|
||||||
|
|
||||||
|
ShortBuffer rawAudioBuffer;
|
||||||
|
|
||||||
|
int channels;
|
||||||
|
int sampleRate;
|
||||||
|
|
||||||
|
try (MemoryStack stack = stackPush()) {
|
||||||
|
//Allocate space to store return information from the function
|
||||||
|
IntBuffer channelsBuffer = stack.mallocInt(1);
|
||||||
|
IntBuffer sampleRateBuffer = stack.mallocInt(1);
|
||||||
|
|
||||||
|
rawAudioBuffer = stb_vorbis_decode_filename("resources/sound/gun.ogg", channelsBuffer, sampleRateBuffer);
|
||||||
|
|
||||||
|
//Retreive the extra information that was stored in the buffers by the function
|
||||||
|
channels = channelsBuffer.get(0);
|
||||||
|
sampleRate = sampleRateBuffer.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Find the correct OpenAL format
|
||||||
|
int format = -1;
|
||||||
|
if (channels == 1) {
|
||||||
|
format = AL_FORMAT_MONO16;
|
||||||
|
} else if (channels == 2) {
|
||||||
|
format = AL_FORMAT_STEREO16;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Request space for the buffer
|
||||||
|
int bufferPointer = alGenBuffers();
|
||||||
|
|
||||||
|
//Send the data to OpenAL
|
||||||
|
alBufferData(bufferPointer, format, rawAudioBuffer, sampleRate);
|
||||||
|
|
||||||
|
//Free the memory allocated by STB
|
||||||
|
free(rawAudioBuffer);
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
//Request a source
|
||||||
|
int sourcePointer = alGenSources();
|
||||||
|
|
||||||
|
//Assign the sound we just loaded to the source
|
||||||
|
alSourcei(sourcePointer, AL_BUFFER, bufferPointer);
|
||||||
|
|
||||||
|
//Play the sound
|
||||||
|
alSourcePlay(sourcePointer);
|
||||||
|
|
||||||
|
System.out.println(sourcePointer);
|
||||||
|
|
||||||
|
try {
|
||||||
|
//Wait for a second
|
||||||
|
Thread.sleep(10);
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
//alDeleteSources(sourcePointer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*//Terminate OpenAL
|
||||||
|
alDeleteSources(sourcePointer);
|
||||||
|
alDeleteBuffers(bufferPointer);
|
||||||
|
alcDestroyContext(context);
|
||||||
|
alcCloseDevice(device);*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
||||||
|
package shootergame.audio;
|
||||||
|
|
||||||
|
import static org.lwjgl.openal.ALC11.*;
|
||||||
|
import static org.lwjgl.openal.AL11.*;
|
||||||
|
|
||||||
|
import org.lwjgl.openal.AL;
|
||||||
|
import org.lwjgl.openal.ALC;
|
||||||
|
import org.lwjgl.openal.ALCCapabilities;
|
||||||
|
import org.lwjgl.openal.ALCapabilities;
|
||||||
|
|
||||||
|
import shootergame.util.math.vec.Vec3d;
|
||||||
|
|
||||||
|
public class AudioEngine
|
||||||
|
{
|
||||||
|
String deviceName;
|
||||||
|
long device, context;
|
||||||
|
ALCCapabilities alcCapabilities;
|
||||||
|
ALCapabilities alCapabilities;
|
||||||
|
|
||||||
|
public void init()
|
||||||
|
{
|
||||||
|
// Get the device name
|
||||||
|
deviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
|
||||||
|
device = alcOpenDevice(deviceName);
|
||||||
|
|
||||||
|
if(device == 0) {
|
||||||
|
System.err.println("OpenAL device is null");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the context
|
||||||
|
int[] attributes = {0};
|
||||||
|
context = alcCreateContext(device, attributes);
|
||||||
|
alcMakeContextCurrent(context);
|
||||||
|
|
||||||
|
// Get the capabilities
|
||||||
|
alcCapabilities = ALC.createCapabilities(device);
|
||||||
|
alCapabilities = AL.createCapabilities(alcCapabilities);
|
||||||
|
|
||||||
|
// Display some info
|
||||||
|
if(alCapabilities.OpenAL11) {
|
||||||
|
System.out.println("OpenAL 1.1 supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(alCapabilities.OpenAL10) {
|
||||||
|
System.out.println("OpenAL 1.0 supported");
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
System.err.println("No OpenAL capability found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void destroy()
|
||||||
|
{
|
||||||
|
// Close the context and the device
|
||||||
|
alcDestroyContext(context);
|
||||||
|
alcCloseDevice(device);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
package shootergame.audio;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
import java.nio.ShortBuffer;
|
||||||
|
|
||||||
|
import org.lwjgl.openal.AL10;
|
||||||
|
import org.lwjgl.openal.AL11;
|
||||||
|
import org.lwjgl.openal.ALC10;
|
||||||
|
import org.lwjgl.stb.STBVorbis;
|
||||||
|
import org.lwjgl.system.MemoryStack;
|
||||||
|
import org.lwjgl.system.libc.LibCStdlib;
|
||||||
|
|
||||||
|
import static org.lwjgl.openal.AL10.AL_BUFFER;
|
||||||
|
import static org.lwjgl.openal.AL10.AL_GAIN;
|
||||||
|
import static org.lwjgl.openal.AL10.AL_POSITION;
|
||||||
|
import static org.lwjgl.openal.AL10.alSource3f;
|
||||||
|
import static org.lwjgl.openal.AL10.alSourcePlay;
|
||||||
|
import static org.lwjgl.openal.AL10.alSourcef;
|
||||||
|
import static org.lwjgl.openal.AL10.alSourcei;
|
||||||
|
import static org.lwjgl.openal.AL11.*;
|
||||||
|
|
||||||
|
import shootergame.Main;
|
||||||
|
import shootergame.display.Camera;
|
||||||
|
import shootergame.resources.Resource;
|
||||||
|
import shootergame.util.math.MathHelpers;
|
||||||
|
import shootergame.util.math.vec.Vec2d;
|
||||||
|
import shootergame.util.math.vec.Vec3d;
|
||||||
|
|
||||||
|
public class AudioObject
|
||||||
|
{
|
||||||
|
int bufferPointer;
|
||||||
|
Resource resource;
|
||||||
|
int channels;
|
||||||
|
int sample_rate;
|
||||||
|
int output;
|
||||||
|
|
||||||
|
public AudioObject(Resource resource)
|
||||||
|
{
|
||||||
|
// Store the argument values
|
||||||
|
this.resource = resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init()
|
||||||
|
{
|
||||||
|
// Create some buffers
|
||||||
|
MemoryStack stack = MemoryStack.stackPush();
|
||||||
|
IntBuffer channels_buf = stack.mallocInt(1);
|
||||||
|
IntBuffer sample_rate_buf = stack.mallocInt(1);
|
||||||
|
|
||||||
|
// Get the audio
|
||||||
|
ByteBuffer resource_buffer = resource.getByteBuffer();
|
||||||
|
ShortBuffer audio = STBVorbis.stb_vorbis_decode_memory(resource_buffer, channels_buf, sample_rate_buf);
|
||||||
|
|
||||||
|
// Get the channels and the sample rate
|
||||||
|
channels = channels_buf.get();
|
||||||
|
sample_rate = sample_rate_buf.get();
|
||||||
|
|
||||||
|
//Find the correct OpenAL format
|
||||||
|
int format = -1;
|
||||||
|
if(channels == 1) {
|
||||||
|
format = AL_FORMAT_MONO16;
|
||||||
|
} else if(channels == 2) {
|
||||||
|
format = AL_FORMAT_STEREO16;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the data to OpenAL
|
||||||
|
bufferPointer = alGenBuffers();
|
||||||
|
alBufferData(bufferPointer, format, audio, sample_rate);
|
||||||
|
|
||||||
|
// Free some c buffers
|
||||||
|
LibCStdlib.free(audio);
|
||||||
|
LibCStdlib.free(resource_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void play(Vec3d pos, double volume)
|
||||||
|
{
|
||||||
|
// 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));
|
||||||
|
double z = pos.z;
|
||||||
|
|
||||||
|
// Play the sound with a new source
|
||||||
|
int source = AudioSources.getSource();
|
||||||
|
alSourcei(source, AL_BUFFER, bufferPointer);
|
||||||
|
alSourcef(source, AL_GAIN, (float)volume);
|
||||||
|
alSource3f(source, AL_POSITION, (float)x, (float)y, (float)z);
|
||||||
|
alSourcePlay(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
package shootergame.audio;
|
||||||
|
|
||||||
|
import java.nio.IntBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static org.lwjgl.openal.AL11.*;
|
||||||
|
|
||||||
|
public class AudioSources
|
||||||
|
{
|
||||||
|
static ArrayList<Integer> sources = new ArrayList<Integer>();
|
||||||
|
private static int upto = 0;
|
||||||
|
private static int max = 0;
|
||||||
|
|
||||||
|
public static void init()
|
||||||
|
{
|
||||||
|
// Generate the first source
|
||||||
|
int source = alGenSources();
|
||||||
|
boolean first = true;
|
||||||
|
|
||||||
|
// Generate sources until OpenAL errors out
|
||||||
|
while(source != 0 || first) {
|
||||||
|
max = source;
|
||||||
|
first = false;
|
||||||
|
sources.add(source);
|
||||||
|
source = alGenSources();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getSource()
|
||||||
|
{
|
||||||
|
// Get the next source
|
||||||
|
int source = sources.get(upto);
|
||||||
|
|
||||||
|
// Increase the upto iterator
|
||||||
|
upto += 1;
|
||||||
|
upto %= max;
|
||||||
|
|
||||||
|
// Send back the source
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -99,46 +99,36 @@ public class Entity implements ITransparentObject
|
||||||
// Get the layer
|
// Get the layer
|
||||||
Layer l = Main.world.getLayer();
|
Layer l = Main.world.getLayer();
|
||||||
|
|
||||||
// Get some tiles
|
// Check the tile the player is standing on
|
||||||
Vec2i[] tiles = {
|
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
|
||||||
new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0)
|
|
||||||
};
|
|
||||||
|
|
||||||
for(Vec2i tpos : tiles)
|
|
||||||
{
|
{
|
||||||
{
|
// Get the tile
|
||||||
// Get the tile
|
Tile t = l.getBackTile(tpos);
|
||||||
Tile t = l.getBackTile(tpos);
|
|
||||||
|
// Send false if the tile isn't walkable
|
||||||
// Send false if the tile isn't walkable
|
if(!t.tileWalkable) {
|
||||||
if(!t.tileWalkable) {
|
return false;
|
||||||
System.out.println("1");
|
}
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
{
|
||||||
|
// Get the front tile
|
||||||
|
Tile t = l.getFrontTile(tpos);
|
||||||
|
|
||||||
|
// Send false if the tile isn't walkable
|
||||||
|
if(!t.tileWalkable) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check the tiles hitbox if the tile is solid
|
||||||
|
if(t.tileSolid)
|
||||||
{
|
{
|
||||||
// Get the front tile
|
// Is the entity in the tiles hitbox
|
||||||
Tile t = l.getFrontTile(tpos);
|
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < t.tileHitbox)
|
||||||
|
|
||||||
// Send false if the tile isn't walkable
|
|
||||||
if(!t.tileWalkable) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the tiles hitbox if the tile is solid
|
|
||||||
if(t.tileSolid)
|
|
||||||
{
|
{
|
||||||
// Is the entity in the tiles hitbox
|
// Send back false
|
||||||
if(
|
return false;
|
||||||
tpos.x + t.tileHitbox < pos.x ||
|
|
||||||
tpos.x - t.tileHitbox > pos.x ||
|
|
||||||
tpos.y + t.tileHitbox < pos.y ||
|
|
||||||
tpos.y - t.tileHitbox > pos.y
|
|
||||||
) {
|
|
||||||
// Send back false
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,14 @@ import java.util.Random;
|
||||||
|
|
||||||
import shootergame.display.Camera;
|
import shootergame.display.Camera;
|
||||||
import shootergame.entity.particle.ParticleBlood;
|
import shootergame.entity.particle.ParticleBlood;
|
||||||
|
import shootergame.init.Sounds;
|
||||||
|
import shootergame.tiles.Tile;
|
||||||
import shootergame.util.gl.GlHelpers;
|
import shootergame.util.gl.GlHelpers;
|
||||||
|
import shootergame.util.math.MathHelpers;
|
||||||
import shootergame.util.math.random.RandomHelpers;
|
import shootergame.util.math.random.RandomHelpers;
|
||||||
import shootergame.util.math.vec.Vec2d;
|
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.chunk.Chunk;
|
||||||
import shootergame.world.layer.Layer;
|
import shootergame.world.layer.Layer;
|
||||||
|
|
||||||
|
|
@ -24,6 +29,9 @@ public class EntityBullet extends EntityParticle
|
||||||
this.pos = pos;
|
this.pos = pos;
|
||||||
this.angle = angle;
|
this.angle = angle;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
|
||||||
|
// Play the gun sound
|
||||||
|
Sounds.GUN.play(new Vec3d(pos.x, pos.y, 0.4), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -33,6 +41,24 @@ public class EntityBullet extends EntityParticle
|
||||||
// Move forward in the bullets angle, very quickly
|
// Move forward in the bullets angle, very quickly
|
||||||
this.moveForward(0.2);
|
this.moveForward(0.2);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Get the foreground tile
|
||||||
|
Tile tile = chunk.getFrontTile(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)
|
||||||
|
{
|
||||||
|
// Delete the bullet
|
||||||
|
kill();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Loop over the nearby entities
|
// Loop over the nearby entities
|
||||||
for(Entity e : layer.getNearbyEntities(pos, 0.5))
|
for(Entity e : layer.getNearbyEntities(pos, 0.5))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@ package shootergame.entity;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import shootergame.Main;
|
import shootergame.Main;
|
||||||
|
import shootergame.init.Sounds;
|
||||||
import shootergame.init.Textures;
|
import shootergame.init.Textures;
|
||||||
import shootergame.util.math.random.OpenSimplexNoise;
|
import shootergame.util.math.random.OpenSimplexNoise;
|
||||||
|
import shootergame.util.math.vec.Vec3d;
|
||||||
import shootergame.world.chunk.Chunk;
|
import shootergame.world.chunk.Chunk;
|
||||||
import shootergame.world.layer.Layer;
|
import shootergame.world.layer.Layer;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,13 @@ import shootergame.entity.Entity;
|
||||||
import shootergame.entity.EntityAlive;
|
import shootergame.entity.EntityAlive;
|
||||||
import shootergame.entity.EntityBullet;
|
import shootergame.entity.EntityBullet;
|
||||||
import shootergame.entity.EntityVertical;
|
import shootergame.entity.EntityVertical;
|
||||||
|
import shootergame.init.Sounds;
|
||||||
import shootergame.init.Textures;
|
import shootergame.init.Textures;
|
||||||
import shootergame.util.gl.texture.TextureReference;
|
import shootergame.util.gl.texture.TextureReference;
|
||||||
import shootergame.util.math.MathHelpers;
|
import shootergame.util.math.MathHelpers;
|
||||||
import shootergame.util.math.vec.Vec2d;
|
import shootergame.util.math.vec.Vec2d;
|
||||||
import shootergame.util.math.vec.Vec2i;
|
import shootergame.util.math.vec.Vec2i;
|
||||||
|
import shootergame.util.math.vec.Vec3d;
|
||||||
import shootergame.world.chunk.Chunk;
|
import shootergame.world.chunk.Chunk;
|
||||||
import shootergame.world.layer.Layer;
|
import shootergame.world.layer.Layer;
|
||||||
|
|
||||||
|
|
@ -23,6 +25,7 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
|
||||||
public boolean MOVE_BACKWARD = false;
|
public boolean MOVE_BACKWARD = false;
|
||||||
public boolean MOVE_LEFT = false;
|
public boolean MOVE_LEFT = false;
|
||||||
public boolean MOVE_RIGHT = false;
|
public boolean MOVE_RIGHT = false;
|
||||||
|
public boolean GUN = false;
|
||||||
public boolean moving = false;
|
public boolean moving = false;
|
||||||
|
|
||||||
private int bullet_frequency = 0;
|
private int bullet_frequency = 0;
|
||||||
|
|
@ -74,6 +77,11 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
|
||||||
if(MOVE_BACKWARD) {
|
if(MOVE_BACKWARD) {
|
||||||
this.moveBackward();
|
this.moveBackward();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use the gun
|
||||||
|
if(GUN) {
|
||||||
|
this.fireBullet(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
package shootergame.init;
|
||||||
|
|
||||||
|
import shootergame.resources.Resource;
|
||||||
|
|
||||||
|
public class Resources
|
||||||
|
{
|
||||||
|
public static void loadResources()
|
||||||
|
{
|
||||||
|
TEXMAP_PNG.load();
|
||||||
|
GUN_OGG.load();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final Resource TEXMAP_PNG = new Resource("texmap.png");
|
||||||
|
public static final Resource GUN_OGG = new Resource("sound/gun.ogg");
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
package shootergame.init;
|
||||||
|
|
||||||
|
import shootergame.audio.AudioObject;
|
||||||
|
|
||||||
|
public class Sounds
|
||||||
|
{
|
||||||
|
public static void init()
|
||||||
|
{
|
||||||
|
GUN.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final AudioObject GUN = new AudioObject(Resources.GUN_OGG);
|
||||||
|
}
|
||||||
|
|
@ -23,8 +23,7 @@ public class Textures
|
||||||
|
|
||||||
public static final ArrayList<AnimationReference> animations = new ArrayList<AnimationReference>();
|
public static final ArrayList<AnimationReference> animations = new ArrayList<AnimationReference>();
|
||||||
|
|
||||||
public static final TextureMap texmap = new TextureMap(16,
|
public static final TextureMap texmap = new TextureMap(16, Resources.TEXMAP_PNG);
|
||||||
"/home/josua/eclipse-workspace/ShooterGame/src/shootergame/resources/texmap.png");
|
|
||||||
|
|
||||||
public static final TextureReference TILE_GRASS = texmap.getTextureReference(0, 1, 0, 1);
|
public static final TextureReference TILE_GRASS = texmap.getTextureReference(0, 1, 0, 1);
|
||||||
public static final TextureReference TILE_SAND = texmap.getTextureReference(1, 2, 0, 1);
|
public static final TextureReference TILE_SAND = texmap.getTextureReference(1, 2, 0, 1);
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,14 @@
|
||||||
package shootergame.input;
|
package shootergame.input;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.CharBuffer;
|
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.lwjgl.glfw.GLFW;
|
import org.lwjgl.glfw.GLFW;
|
||||||
import org.lwjgl.glfw.GLFWGamepadState;
|
|
||||||
import org.lwjgl.glfw.GLFWJoystickCallbackI;
|
import org.lwjgl.glfw.GLFWJoystickCallbackI;
|
||||||
|
|
||||||
import mainloop.task.IMainloopTask;
|
import mainloop.task.IMainloopTask;
|
||||||
import shootergame.Main;
|
import shootergame.Main;
|
||||||
import shootergame.entity.Entity;
|
|
||||||
import shootergame.entity.EntityBullet;
|
|
||||||
import shootergame.util.math.MathHelpers;
|
|
||||||
import shootergame.util.math.vec.Vec2d;
|
|
||||||
|
|
||||||
public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
|
public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,6 @@
|
||||||
package shootergame.input;
|
package shootergame.input;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_A;
|
import static org.lwjgl.glfw.GLFW.*;
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_D;
|
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SHIFT;
|
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_S;
|
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE;
|
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_KEY_W;
|
|
||||||
import static org.lwjgl.glfw.GLFW.GLFW_RELEASE;
|
|
||||||
|
|
||||||
import org.lwjgl.glfw.GLFWKeyCallbackI;
|
import org.lwjgl.glfw.GLFWKeyCallbackI;
|
||||||
|
|
||||||
|
|
@ -36,6 +30,10 @@ public class KeyCallback implements GLFWKeyCallbackI
|
||||||
if(key == GLFW_KEY_D) {
|
if(key == GLFW_KEY_D) {
|
||||||
Main.player.MOVE_RIGHT = pressed;
|
Main.player.MOVE_RIGHT = pressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(key == GLFW_KEY_ENTER) {
|
||||||
|
Main.player.GUN = pressed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,13 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask
|
||||||
private long max_mspf = 1;
|
private long max_mspf = 1;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClose() {
|
public void onClose()
|
||||||
|
{
|
||||||
|
// Close OpenAL
|
||||||
|
System.out.println("Closed OpenAL");
|
||||||
|
Main.audio.destroy();
|
||||||
|
|
||||||
|
// Send some info to stdout
|
||||||
System.out.println("Mainloop closed normally");
|
System.out.println("Mainloop closed normally");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,147 @@
|
||||||
|
package shootergame.resources;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
public class Resource
|
||||||
|
{
|
||||||
|
String path;
|
||||||
|
byte[] data;
|
||||||
|
|
||||||
|
private InputStream fileURLStream(String path, int tries)
|
||||||
|
{
|
||||||
|
System.err.println("Getting \""+fileURL(path)+"\". Try "+ tries);
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new URL(fileURL(path)).openStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(MalformedURLException e) {
|
||||||
|
System.err.println("Malformed URL");
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
if(tries == 10) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.err.println("Exceeded max tries");
|
||||||
|
System.exit(1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
e.printStackTrace();
|
||||||
|
try {
|
||||||
|
Thread.sleep(5000);
|
||||||
|
} catch (InterruptedException e1) {
|
||||||
|
System.err.println("Terminated by user");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
return this.fileURLStream(path, tries+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream fileURLStream(String path) {
|
||||||
|
return this.fileURLStream(path, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String fileURL(String path) {
|
||||||
|
return "https://www.onewaycoding.ml/files/game_resources/ShooterGame/"+path;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String filePath(String path) {
|
||||||
|
return "resources/"+path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Resource(String path)
|
||||||
|
{
|
||||||
|
// Set the specified data
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void load()
|
||||||
|
{
|
||||||
|
// Does the resource not exist
|
||||||
|
File file = new File(filePath(path));
|
||||||
|
if(!file.exists())
|
||||||
|
{
|
||||||
|
// Create the parent directories
|
||||||
|
file.getParentFile().mkdirs();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Download the file and open the file output stream
|
||||||
|
OutputStream file_stream = new FileOutputStream(filePath(path));
|
||||||
|
|
||||||
|
// Get the resource data from the server
|
||||||
|
data = ResourceDownload.downloadURL(fileURL(path));
|
||||||
|
|
||||||
|
// Save all the data downloaded to a file
|
||||||
|
file_stream.write(data);
|
||||||
|
file_stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
// Print the stacktrace and exit
|
||||||
|
System.err.println("Error downloading file");
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Open the file stored on the hard drive
|
||||||
|
InputStream file_stream = new FileInputStream(filePath(path));
|
||||||
|
|
||||||
|
data = new byte[file_stream.available()];
|
||||||
|
file_stream.read(data);
|
||||||
|
file_stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
// Print the stacktrace and exit
|
||||||
|
System.err.println("Error opening file");
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getBytes() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getString() {
|
||||||
|
return new String(data, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ByteBuffer getByteBuffer()
|
||||||
|
{
|
||||||
|
System.out.println(data.length);
|
||||||
|
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length);
|
||||||
|
|
||||||
|
for(int i=0;i<data.length;i++) {
|
||||||
|
buffer.put(i, data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package shootergame.resources;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
|
||||||
|
public class ResourceDownload
|
||||||
|
{
|
||||||
|
public static byte[] downloadURL(String path) throws IOException
|
||||||
|
{
|
||||||
|
// Get the url and the connection
|
||||||
|
URL url = new URL(path);
|
||||||
|
URLConnection con = url.openConnection();
|
||||||
|
|
||||||
|
// Get the file size
|
||||||
|
int fileSize = con.getContentLength();
|
||||||
|
byte[] data = new byte[fileSize];
|
||||||
|
|
||||||
|
// Get the input stream
|
||||||
|
InputStream stream = con.getInputStream();
|
||||||
|
|
||||||
|
// Get 1 kb packets from the server
|
||||||
|
for(int i = 0; i < fileSize; i += 1024)
|
||||||
|
{
|
||||||
|
// Load the stream contents to the buffer
|
||||||
|
byte buffer[] = new byte[1024];
|
||||||
|
stream.read(buffer);
|
||||||
|
|
||||||
|
// Send a status update
|
||||||
|
DecimalFormat decim = new DecimalFormat("#.##");
|
||||||
|
System.out.println(decim.format(i/1024.0) + " kb / " +
|
||||||
|
decim.format(fileSize/1024.0) + " kb - " +
|
||||||
|
decim.format((double)i / (double)fileSize) + "%");
|
||||||
|
|
||||||
|
// Store the bytes downloaded to the data variable
|
||||||
|
for(int b=0;b<buffer.length;b++) {
|
||||||
|
if(i+b < fileSize) {
|
||||||
|
data[i+b] = buffer[b];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the connection
|
||||||
|
stream.close();
|
||||||
|
|
||||||
|
// Send back the data array
|
||||||
|
return data;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
{
|
|
||||||
"protocol_version" : "0.0.2",
|
|
||||||
"configuration" : {
|
|
||||||
"version" : "5.18.0.240 (Debian 5.18.0.240+dfsg-2ubuntu2 Wed Apr 17 23:39:09 UTC 2019)",
|
|
||||||
"tlc" : "__thread",
|
|
||||||
"sigsgev" : "altstack",
|
|
||||||
"notifications" : "epoll",
|
|
||||||
"architecture" : "amd64",
|
|
||||||
"disabled_features" : "none",
|
|
||||||
"smallconfig" : "disabled",
|
|
||||||
"bigarrays" : "disabled",
|
|
||||||
"softdebug" : "enabled",
|
|
||||||
"interpreter" : "enabled",
|
|
||||||
"llvm_support" : "disabled",
|
|
||||||
"suspend" : "preemptive"
|
|
||||||
},
|
|
||||||
"memory" : {
|
|
||||||
"minor_gc_time" : "378907",
|
|
||||||
"major_gc_time" : "34891",
|
|
||||||
"minor_gc_count" : "24",
|
|
||||||
"major_gc_count" : "1",
|
|
||||||
"major_gc_time_concurrent" : "29295"
|
|
||||||
},
|
|
||||||
"threads" : [
|
|
||||||
{
|
|
||||||
"is_managed" : false,
|
|
||||||
"crashed" : true,
|
|
||||||
"managed_thread_ptr" : "0x0",
|
|
||||||
"native_thread_id" : "0x7fee3d843700",
|
|
||||||
"thread_info_addr" : "0x0",
|
|
||||||
"thread_name" : "Finalizer",
|
|
||||||
"ctx" : {
|
|
||||||
"IP" : "0x7fee40fd8ed7",
|
|
||||||
"SP" : "0x7fee3d842690",
|
|
||||||
"BP" : "0x7fee3d8429e0"
|
|
||||||
},
|
|
||||||
"managed_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
],
|
|
||||||
"unmanaged_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"is_managed" : false,
|
|
||||||
"crashed" : false,
|
|
||||||
"managed_thread_ptr" : "0x0",
|
|
||||||
"native_thread_id" : "0x7fee40f90780",
|
|
||||||
"thread_info_addr" : "0x0",
|
|
||||||
"thread_name" : "mono",
|
|
||||||
"ctx" : {
|
|
||||||
"IP" : "0x7fee410a6729",
|
|
||||||
"SP" : "0x7ffe255e5640",
|
|
||||||
"BP" : "0x3"
|
|
||||||
},
|
|
||||||
"managed_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
],
|
|
||||||
"unmanaged_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
{
|
|
||||||
"protocol_version" : "0.0.2",
|
|
||||||
"configuration" : {
|
|
||||||
"version" : "5.18.0.240 (Debian 5.18.0.240+dfsg-2ubuntu2 Wed Apr 17 23:39:09 UTC 2019)",
|
|
||||||
"tlc" : "__thread",
|
|
||||||
"sigsgev" : "altstack",
|
|
||||||
"notifications" : "epoll",
|
|
||||||
"architecture" : "amd64",
|
|
||||||
"disabled_features" : "none",
|
|
||||||
"smallconfig" : "disabled",
|
|
||||||
"bigarrays" : "disabled",
|
|
||||||
"softdebug" : "enabled",
|
|
||||||
"interpreter" : "enabled",
|
|
||||||
"llvm_support" : "disabled",
|
|
||||||
"suspend" : "preemptive"
|
|
||||||
},
|
|
||||||
"memory" : {
|
|
||||||
"minor_gc_time" : "41590",
|
|
||||||
"major_gc_time" : "0",
|
|
||||||
"minor_gc_count" : "2",
|
|
||||||
"major_gc_count" : "0",
|
|
||||||
"major_gc_time_concurrent" : "0"
|
|
||||||
},
|
|
||||||
"threads" : [
|
|
||||||
{
|
|
||||||
"is_managed" : false,
|
|
||||||
"crashed" : false,
|
|
||||||
"managed_thread_ptr" : "0x0",
|
|
||||||
"native_thread_id" : "0x7fa2fae57780",
|
|
||||||
"thread_info_addr" : "0x0",
|
|
||||||
"thread_name" : "mono",
|
|
||||||
"ctx" : {
|
|
||||||
"IP" : "0x7fa2faef4be9",
|
|
||||||
"SP" : "0x7ffee36a8db0",
|
|
||||||
"BP" : "0x100"
|
|
||||||
},
|
|
||||||
"managed_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
],
|
|
||||||
"unmanaged_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"is_managed" : false,
|
|
||||||
"crashed" : true,
|
|
||||||
"managed_thread_ptr" : "0x0",
|
|
||||||
"native_thread_id" : "0x7fa2f753d700",
|
|
||||||
"thread_info_addr" : "0x0",
|
|
||||||
"thread_name" : "Finalizer",
|
|
||||||
"ctx" : {
|
|
||||||
"IP" : "0x7fa2fae9fed7",
|
|
||||||
"SP" : "0x7fa2f753c690",
|
|
||||||
"BP" : "0x7fa2f753c9e0"
|
|
||||||
},
|
|
||||||
"managed_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
],
|
|
||||||
"unmanaged_frames" : [
|
|
||||||
{
|
|
||||||
"native_address" : "unregistered"
|
|
||||||
}
|
|
||||||
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
@ -11,6 +11,7 @@ public class TileRock extends TileVertical
|
||||||
// Set some settings
|
// Set some settings
|
||||||
this.opaqueTile = true;
|
this.opaqueTile = true;
|
||||||
this.tileSolid = true;
|
this.tileSolid = true;
|
||||||
|
this.tileHitbox = 0.4;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ public class TileTree extends TileVertical
|
||||||
// Set some settings
|
// Set some settings
|
||||||
this.opaqueTile = true;
|
this.opaqueTile = true;
|
||||||
this.tileSolid = true;
|
this.tileSolid = true;
|
||||||
|
this.tileHitbox = 0.3;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,20 @@
|
||||||
package shootergame.util.gl.texture;
|
package shootergame.util.gl.texture;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.nio.MappedByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import javax.swing.plaf.SliderUI;
|
||||||
|
|
||||||
import org.lwjgl.stb.STBImage;
|
import org.lwjgl.stb.STBImage;
|
||||||
|
|
||||||
|
import shootergame.resources.Resource;
|
||||||
|
|
||||||
public class Texture
|
public class Texture
|
||||||
{
|
{
|
||||||
private ByteBuffer texture;
|
private ByteBuffer texture;
|
||||||
|
|
@ -11,20 +22,30 @@ public class Texture
|
||||||
private int height;
|
private int height;
|
||||||
private int channels;
|
private int channels;
|
||||||
|
|
||||||
public Texture(String path)
|
public Texture(Resource tex)
|
||||||
{
|
{
|
||||||
// Get the width, height, and channels
|
// Get the width, height, and channels
|
||||||
int width[] = {0};
|
int width[] = {0};
|
||||||
int height[] = {0};
|
int height[] = {0};
|
||||||
int channels[] = {0};
|
int channels[] = {0};
|
||||||
|
|
||||||
// Load the specified texture
|
// Load the texture with STBImage
|
||||||
this.texture = STBImage.stbi_load(path, width, height, channels, STBImage.STBI_rgb_alpha);
|
this.texture = STBImage.stbi_load_from_memory(
|
||||||
|
tex.getByteBuffer(),
|
||||||
|
width, height, channels, STBImage.STBI_rgb_alpha);
|
||||||
|
|
||||||
|
// Check for a failure
|
||||||
|
String failure = STBImage.stbi_failure_reason();
|
||||||
|
if(failure != "") {
|
||||||
|
System.out.println("STB: "+failure);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the image data
|
||||||
this.width = width[0];
|
this.width = width[0];
|
||||||
this.height = height[0];
|
this.height = height[0];
|
||||||
this.channels = channels[0];
|
this.channels = channels[0];
|
||||||
|
|
||||||
System.out.println("w:"+this.width+" h:"+this.height+" c:"+this.channels+"\npath: "+path);
|
System.out.println("w:"+this.width+" h:"+this.height+" c:"+this.channels);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,19 @@ package shootergame.util.gl.texture;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
|
|
||||||
|
import shootergame.resources.Resource;
|
||||||
|
|
||||||
public class TextureMap
|
public class TextureMap
|
||||||
{
|
{
|
||||||
private int texture_gl;
|
private int texture_gl;
|
||||||
private String path;
|
|
||||||
private int max_x;
|
private int max_x;
|
||||||
private int max_y;
|
private int max_y;
|
||||||
private int scale;
|
private int scale;
|
||||||
|
private Resource resource;
|
||||||
|
|
||||||
public TextureMap(int scale, String path) {
|
public TextureMap(int scale, Resource resource) {
|
||||||
this.path = path;
|
|
||||||
this.scale = scale;
|
this.scale = scale;
|
||||||
|
this.resource = resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init()
|
public void init()
|
||||||
|
|
@ -24,7 +26,7 @@ public class TextureMap
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
// Load the texture into opengl
|
// Load the texture into opengl
|
||||||
Texture texture = new Texture(path);
|
Texture texture = new Texture(resource);
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
|
||||||
texture.getWidth(), texture.getHeight(),
|
texture.getWidth(), texture.getHeight(),
|
||||||
0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getByteBuffer());
|
0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getByteBuffer());
|
||||||
|
|
|
||||||
|
|
@ -61,12 +61,26 @@ public class MathHelpers
|
||||||
|
|
||||||
public static int floor(double a)
|
public static int floor(double a)
|
||||||
{
|
{
|
||||||
if(a < 0) {
|
if((int)a == a) {
|
||||||
return (int)a - 1;
|
return (int) a;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(a < 0) {
|
||||||
|
return (int)(a - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
return (int)a;
|
return (int)a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static double positive(double a) {
|
||||||
|
if(a < 0) return -a;
|
||||||
|
else return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int positive(int a) {
|
||||||
|
if(a < 0) return -a;
|
||||||
|
else return a;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,4 +44,13 @@ public class Vec2d
|
||||||
public Vec2d copy() {
|
public Vec2d copy() {
|
||||||
return new Vec2d(x, y);
|
return new Vec2d(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double squareDistance(Vec2d other)
|
||||||
|
{
|
||||||
|
double dx = MathHelpers.positive(other.x - x);
|
||||||
|
double dy = MathHelpers.positive(other.y - y);
|
||||||
|
|
||||||
|
if(dx > dy) return dx;
|
||||||
|
else return dy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,4 +67,13 @@ public class Vec2i
|
||||||
public Vec2i copy() {
|
public Vec2i copy() {
|
||||||
return new Vec2i(x, y);
|
return new Vec2i(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int squareDistance(Vec2i other)
|
||||||
|
{
|
||||||
|
int dx = MathHelpers.positive(other.x - x);
|
||||||
|
int dy = MathHelpers.positive(other.y - y);
|
||||||
|
|
||||||
|
if(dx > dy) return dx;
|
||||||
|
else return dy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,4 +46,15 @@ public class Vec3d
|
||||||
public Vec3d copy() {
|
public Vec3d copy() {
|
||||||
return new Vec3d(x, y, z);
|
return new Vec3d(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double squareDistance(Vec3d other)
|
||||||
|
{
|
||||||
|
double dx = MathHelpers.positive(other.x - x);
|
||||||
|
double dy = MathHelpers.positive(other.y - y);
|
||||||
|
double dz = MathHelpers.positive(other.z - z);
|
||||||
|
|
||||||
|
if(dx > dy) return dx;
|
||||||
|
if(dy > dz) return dy;
|
||||||
|
else return dz;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,4 +74,15 @@ public class Vec3i
|
||||||
public Vec3i copy() {
|
public Vec3i copy() {
|
||||||
return new Vec3i(x, y, z);
|
return new Vec3i(x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int squareDistance(Vec3i other)
|
||||||
|
{
|
||||||
|
int dx = MathHelpers.positive(other.x - x);
|
||||||
|
int dy = MathHelpers.positive(other.y - y);
|
||||||
|
int dz = MathHelpers.positive(other.z - z);
|
||||||
|
|
||||||
|
if(dx > dy) return dx;
|
||||||
|
if(dy > dz) return dy;
|
||||||
|
else return dz;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
package shootergame.world.chunk;
|
package shootergame.world.chunk;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import shootergame.display.Camera;
|
import shootergame.display.Camera;
|
||||||
import shootergame.entity.Entity;
|
import shootergame.entity.Entity;
|
||||||
import shootergame.init.Tiles;
|
import shootergame.init.Tiles;
|
||||||
import shootergame.tiles.Tile;
|
import shootergame.tiles.Tile;
|
||||||
|
import shootergame.util.math.vec.Vec2d;
|
||||||
import shootergame.util.math.vec.Vec2i;
|
import shootergame.util.math.vec.Vec2i;
|
||||||
|
|
||||||
public class ChunkEmpty extends Chunk
|
public class ChunkEmpty extends Chunk
|
||||||
|
|
@ -39,4 +42,17 @@ public class ChunkEmpty extends Chunk
|
||||||
@Override
|
@Override
|
||||||
public void setFrontTile(Tile tile, Vec2i pos) {}
|
public void setFrontTile(Tile tile, Vec2i pos) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkEntities() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ArrayList<Entity> getNearbyEntities(Vec2d pos, double distance) {
|
||||||
|
return new ArrayList<Entity>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void killEntity(Entity e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,8 +98,8 @@ public class Layer
|
||||||
|
|
||||||
private Vec2i getChunkPosFromPos(Vec2d pos) {
|
private Vec2i getChunkPosFromPos(Vec2d pos) {
|
||||||
return new Vec2i(
|
return new Vec2i(
|
||||||
MathHelpers.floor(pos.x / Chunk.CHUNK_SIZE.mx),
|
MathHelpers.floor(pos.x / (double)Chunk.CHUNK_SIZE.mx),
|
||||||
MathHelpers.floor(pos.y / Chunk.CHUNK_SIZE.my));
|
MathHelpers.floor(pos.y / (double)Chunk.CHUNK_SIZE.my));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tile getFrontTile(Vec2i pos)
|
public Tile getFrontTile(Vec2i pos)
|
||||||
|
|
@ -120,8 +120,12 @@ public class Layer
|
||||||
chunks.get(c_pos).spawnEntity(entity);
|
chunks.get(c_pos).spawnEntity(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadChunk(Vec2i pos) {
|
public void loadChunk(Vec2i pos)
|
||||||
chunks.set(pos, layergen.generateChunk(this, new Random(seed), pos));
|
{
|
||||||
|
// Create a unique seed specific to this chunk
|
||||||
|
long cseed = new Random(pos.x).nextLong() + new Random(pos.y).nextLong() + seed;
|
||||||
|
|
||||||
|
chunks.set(pos, layergen.generateChunk(this, seed, new Random(cseed), pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unloadChunk(Vec2i pos) {
|
public void unloadChunk(Vec2i pos) {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import shootergame.world.layer.Layer;
|
||||||
|
|
||||||
public abstract class LayerGen implements IMap2D<Chunk>
|
public abstract class LayerGen implements IMap2D<Chunk>
|
||||||
{
|
{
|
||||||
public abstract Chunk generateChunk(Layer layer, Random rand, Vec2i pos);
|
public abstract Chunk generateChunk(Layer layer, long seed, Random rand, Vec2i pos);
|
||||||
public abstract void spawnEntities(Layer layer, Random rand);
|
public abstract void spawnEntities(Layer layer, Random rand);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,13 @@ public class LayerGenEarth extends LayerGen
|
||||||
{
|
{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Chunk generateChunk(Layer layer, Random rand, Vec2i c_pos)
|
public Chunk generateChunk(Layer layer, long seed, Random rand, Vec2i c_pos)
|
||||||
{
|
{
|
||||||
// Create the new chunk
|
// Create the new chunk
|
||||||
Chunk chunk = new Chunk(layer, c_pos, rand);
|
Chunk chunk = new Chunk(layer, c_pos, rand);
|
||||||
|
|
||||||
// Get the noise generator
|
// Get the noise generator
|
||||||
OpenSimplexNoise noisegen_n = new OpenSimplexNoise(rand.nextLong());
|
OpenSimplexNoise noisegen_n = new OpenSimplexNoise(seed);
|
||||||
|
|
||||||
// Loop over the layer dimensions and create land
|
// Loop over the layer dimensions and create land
|
||||||
for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
|
for(int x=0;x<Chunk.CHUNK_SIZE.mx;x++) {
|
||||||
|
|
@ -46,12 +46,12 @@ public class LayerGenEarth extends LayerGen
|
||||||
else chunk.setFrontTile(Tiles.VOID, pos);
|
else chunk.setFrontTile(Tiles.VOID, pos);
|
||||||
|
|
||||||
// Terrain generation
|
// Terrain generation
|
||||||
if(noise_n < 20) {
|
if(noise_n < 40) {
|
||||||
chunk.setFrontTile(Tiles.WATER, pos);
|
chunk.setFrontTile(Tiles.WATER, pos);
|
||||||
chunk.setBackTile(Tiles.DIRT, pos);
|
chunk.setBackTile(Tiles.DIRT, pos);
|
||||||
}
|
}
|
||||||
else if(noise_n < 60) chunk.setBackTile(Tiles.GRASS, pos);
|
else if(noise_n < 70) chunk.setBackTile(Tiles.GRASS, pos);
|
||||||
else if(noise_n < 80) chunk.setBackTile(Tiles.DIRT, pos);
|
else if(noise_n < 90) chunk.setBackTile(Tiles.DIRT, pos);
|
||||||
else chunk.setBackTile(Tiles.STONE, pos);
|
else chunk.setBackTile(Tiles.STONE, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue