Added sounds, resource downloading, and fixed collision detection.

This commit is contained in:
josua 2019-08-26 18:38:02 +10:00
parent 28be74ec0d
commit 6e17063619
44 changed files with 8588 additions and 222 deletions

BIN
_resources/sound/gun.ogg Normal file

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 56 KiB

1120
hs_err_pid31967.log Normal file

File diff suppressed because it is too large Load Diff

1119
hs_err_pid613.log Normal file

File diff suppressed because it is too large Load Diff

1120
hs_err_pid675.log Normal file

File diff suppressed because it is too large Load Diff

1120
hs_err_pid713.log Normal file

File diff suppressed because it is too large Load Diff

1120
hs_err_pid792.log Normal file

File diff suppressed because it is too large Load Diff

1121
hs_err_pid832.log Normal file

File diff suppressed because it is too large Load Diff

1147
hs_err_pid982.log Normal file

File diff suppressed because it is too large Load Diff

BIN
resources/sound/gun.ogg Normal file

Binary file not shown.

BIN
resources/texmap.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

View File

@ -5,6 +5,8 @@ import java.util.Random;
import javax.swing.text.html.parser.Entity;
import mainloop.manager.MainloopManager;
import shootergame.audio.AudioEngine;
import shootergame.audio.AudioSources;
import shootergame.cheats.Cheats;
import shootergame.display.DisplayStatsEventHandler;
import shootergame.display.DisplayWindow;
@ -12,11 +14,15 @@ import shootergame.entity.EntityEventHandler;
import shootergame.entity.EntityZombie;
import shootergame.entity.player.EntityPlayer;
import shootergame.init.Entities;
import shootergame.init.Resources;
import shootergame.init.Sounds;
import shootergame.init.Textures;
import shootergame.input.JoystickCallback;
import shootergame.mainloop.MainloopEventHandler;
import shootergame.resources.Resource;
import shootergame.util.math.MathHelpers;
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.LayerGenEarth;
@ -27,12 +33,24 @@ public class Main
public static DisplayWindow window;
public static EntityPlayer player = new EntityPlayer();
public static World world;
public static AudioEngine audio;
public static void main(String[] args)
{
// Initialize the cheats
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
mainloop = new MainloopManager(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
mainloop.register(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);

90
src/shootergame/Test.java Normal file
View File

@ -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);*/
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -99,46 +99,36 @@ public class Entity implements ITransparentObject
// Get the layer
Layer l = Main.world.getLayer();
// Get some tiles
Vec2i[] tiles = {
new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0)
};
// Check the tile the player is standing on
Vec2i tpos = new Vec2i(MathHelpers.floor(pos.x)+0, MathHelpers.floor(pos.y)+0);
for(Vec2i tpos : tiles)
{
{
// Get the tile
Tile t = l.getBackTile(tpos);
// Get the tile
Tile t = l.getBackTile(tpos);
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
System.out.println("1");
return false;
}
// Send false if the tile isn't walkable
if(!t.tileWalkable) {
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
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)
// Is the entity in the tiles hitbox
if(pos.squareDistance(new Vec2d(tpos.x + 0.5, tpos.y + 0.5)) < t.tileHitbox)
{
// Is the entity in the tiles hitbox
if(
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;
}
// Send back false
return false;
}
}
}

View File

@ -4,9 +4,14 @@ import java.util.Random;
import shootergame.display.Camera;
import shootergame.entity.particle.ParticleBlood;
import shootergame.init.Sounds;
import shootergame.tiles.Tile;
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.Vec2i;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;
@ -24,6 +29,9 @@ public class EntityBullet extends EntityParticle
this.pos = pos;
this.angle = angle;
this.parent = parent;
// Play the gun sound
Sounds.GUN.play(new Vec3d(pos.x, pos.y, 0.4), 2);
}
@Override
@ -33,6 +41,24 @@ public class EntityBullet extends EntityParticle
// Move forward in the bullets angle, very quickly
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
for(Entity e : layer.getNearbyEntities(pos, 0.5))
{

View File

@ -3,8 +3,10 @@ package shootergame.entity;
import java.util.Random;
import shootergame.Main;
import shootergame.init.Sounds;
import shootergame.init.Textures;
import shootergame.util.math.random.OpenSimplexNoise;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer;

View File

@ -9,11 +9,13 @@ import shootergame.entity.Entity;
import shootergame.entity.EntityAlive;
import shootergame.entity.EntityBullet;
import shootergame.entity.EntityVertical;
import shootergame.init.Sounds;
import shootergame.init.Textures;
import shootergame.util.gl.texture.TextureReference;
import shootergame.util.math.MathHelpers;
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;
@ -23,6 +25,7 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
public boolean MOVE_BACKWARD = false;
public boolean MOVE_LEFT = false;
public boolean MOVE_RIGHT = false;
public boolean GUN = false;
public boolean moving = false;
private int bullet_frequency = 0;
@ -74,6 +77,11 @@ public class EntityPlayer extends EntityVertical implements EntityAlive
if(MOVE_BACKWARD) {
this.moveBackward();
}
// Use the gun
if(GUN) {
this.fireBullet(0);
}
}
@Override

View File

@ -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");
}

View File

@ -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);
}

View File

@ -23,8 +23,7 @@ public class Textures
public static final ArrayList<AnimationReference> animations = new ArrayList<AnimationReference>();
public static final TextureMap texmap = new TextureMap(16,
"/home/josua/eclipse-workspace/ShooterGame/src/shootergame/resources/texmap.png");
public static final TextureMap texmap = new TextureMap(16, Resources.TEXMAP_PNG);
public static final TextureReference TILE_GRASS = texmap.getTextureReference(0, 1, 0, 1);
public static final TextureReference TILE_SAND = texmap.getTextureReference(1, 2, 0, 1);

View File

@ -1,20 +1,14 @@
package shootergame.input;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWGamepadState;
import org.lwjgl.glfw.GLFWJoystickCallbackI;
import mainloop.task.IMainloopTask;
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
{

View File

@ -1,12 +1,6 @@
package shootergame.input;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_A;
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 static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.glfw.GLFWKeyCallbackI;
@ -36,6 +30,10 @@ public class KeyCallback implements GLFWKeyCallbackI
if(key == GLFW_KEY_D) {
Main.player.MOVE_RIGHT = pressed;
}
if(key == GLFW_KEY_ENTER) {
Main.player.GUN = pressed;
}
}
}

View File

@ -12,7 +12,13 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask
private long max_mspf = 1;
@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");
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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"
}
]
}
]
}

View File

@ -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"
}
]
}
]
}

View File

@ -11,6 +11,7 @@ public class TileRock extends TileVertical
// Set some settings
this.opaqueTile = true;
this.tileSolid = true;
this.tileHitbox = 0.4;
}
}

View File

@ -11,6 +11,7 @@ public class TileTree extends TileVertical
// Set some settings
this.opaqueTile = true;
this.tileSolid = true;
this.tileHitbox = 0.3;
}
}

View File

@ -1,9 +1,20 @@
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.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.charset.StandardCharsets;
import javax.swing.plaf.SliderUI;
import org.lwjgl.stb.STBImage;
import shootergame.resources.Resource;
public class Texture
{
private ByteBuffer texture;
@ -11,20 +22,30 @@ public class Texture
private int height;
private int channels;
public Texture(String path)
public Texture(Resource tex)
{
// Get the width, height, and channels
int width[] = {0};
int height[] = {0};
int channels[] = {0};
// Load the specified texture
this.texture = STBImage.stbi_load(path, width, height, channels, STBImage.STBI_rgb_alpha);
// Load the texture with STBImage
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.height = height[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() {

View File

@ -2,17 +2,19 @@ package shootergame.util.gl.texture;
import static org.lwjgl.opengl.GL11.*;
import shootergame.resources.Resource;
public class TextureMap
{
private int texture_gl;
private String path;
private int max_x;
private int max_y;
private int scale;
private Resource resource;
public TextureMap(int scale, String path) {
this.path = path;
public TextureMap(int scale, Resource resource) {
this.scale = scale;
this.resource = resource;
}
public void init()
@ -24,7 +26,7 @@ public class TextureMap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Load the texture into opengl
Texture texture = new Texture(path);
Texture texture = new Texture(resource);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
texture.getWidth(), texture.getHeight(),
0, GL_RGBA, GL_UNSIGNED_BYTE, texture.getByteBuffer());

View File

@ -61,12 +61,26 @@ public class MathHelpers
public static int floor(double a)
{
if(a < 0) {
return (int)a - 1;
if((int)a == a) {
return (int) a;
}
else if(a < 0) {
return (int)(a - 1);
}
else {
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;
}
}

View File

@ -44,4 +44,13 @@ public class Vec2d
public Vec2d copy() {
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;
}
}

View File

@ -67,4 +67,13 @@ public class Vec2i
public Vec2i copy() {
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;
}
}

View File

@ -46,4 +46,15 @@ public class Vec3d
public Vec3d copy() {
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;
}
}

View File

@ -74,4 +74,15 @@ public class Vec3i
public Vec3i copy() {
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;
}
}

View File

@ -1,9 +1,12 @@
package shootergame.world.chunk;
import java.util.ArrayList;
import shootergame.display.Camera;
import shootergame.entity.Entity;
import shootergame.init.Tiles;
import shootergame.tiles.Tile;
import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i;
public class ChunkEmpty extends Chunk
@ -39,4 +42,17 @@ public class ChunkEmpty extends Chunk
@Override
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) {
}
}

View File

@ -98,8 +98,8 @@ public class Layer
private Vec2i getChunkPosFromPos(Vec2d pos) {
return new Vec2i(
MathHelpers.floor(pos.x / Chunk.CHUNK_SIZE.mx),
MathHelpers.floor(pos.y / Chunk.CHUNK_SIZE.my));
MathHelpers.floor(pos.x / (double)Chunk.CHUNK_SIZE.mx),
MathHelpers.floor(pos.y / (double)Chunk.CHUNK_SIZE.my));
}
public Tile getFrontTile(Vec2i pos)
@ -120,8 +120,12 @@ public class Layer
chunks.get(c_pos).spawnEntity(entity);
}
public void loadChunk(Vec2i pos) {
chunks.set(pos, layergen.generateChunk(this, new Random(seed), pos));
public void loadChunk(Vec2i 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) {

View File

@ -9,7 +9,7 @@ import shootergame.world.layer.Layer;
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);
@Override

View File

@ -20,13 +20,13 @@ public class LayerGenEarth extends LayerGen
{
@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
Chunk chunk = new Chunk(layer, c_pos, rand);
// 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
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);
// Terrain generation
if(noise_n < 20) {
if(noise_n < 40) {
chunk.setFrontTile(Tiles.WATER, pos);
chunk.setBackTile(Tiles.DIRT, pos);
}
else if(noise_n < 60) chunk.setBackTile(Tiles.GRASS, pos);
else if(noise_n < 80) chunk.setBackTile(Tiles.DIRT, 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);
}
}