1
0
Fork 0

Refactoring Travis

This commit is contained in:
MrDev023 2016-09-17 16:33:50 +02:00
parent c423e05fc2
commit e4310b8238
1894 changed files with 54 additions and 320 deletions

View file

@ -0,0 +1,142 @@
package mrdev023.audio;
import java.io.*;
import org.lwjgl.util.*;
import static org.lwjgl.openal.AL10.*;
public class Audio {
private String fileName;
private WaveData soundData;
private int buffer,source;
public static final int INITIAL_STATE = 4113,PAUSED_STATE = 4115,STOPPED_STATE = 4116,PLAYING_STATE = 4114;
public Audio(String fileName) throws FileNotFoundException{
this.fileName = fileName;
setSound();
}
private void setSound() throws FileNotFoundException{
soundData = WaveData.create(new BufferedInputStream(new FileInputStream("res" + File.separatorChar +
"sounds" + File.separatorChar + fileName)));
buffer = alGenBuffers();
alBufferData(buffer, soundData.format, soundData.data, soundData.samplerate);
soundData.dispose();
source = alGenSources();
alSourcei(source, AL_BUFFER, buffer);
}
public void playSound(){
alSourcePlay(source);
}
public int getStateSound(){
return alGetSourcei(source, AL_SOURCE_STATE);
}
public boolean isStopped(){
if(alGetSourcei(source, AL_SOURCE_STATE) == STOPPED_STATE)return true;
else return false;
}
public boolean isPaused(){
if(alGetSourcei(source, AL_SOURCE_STATE) == PAUSED_STATE)return true;
else return false;
}
public boolean isPlaying(){
if(alGetSourcei(source, AL_SOURCE_STATE) == PLAYING_STATE)return true;
else return false;
}
public boolean isInitial(){
if(alGetSourcei(source, AL_SOURCE_STATE) == INITIAL_STATE)return true;
else return false;
}
public void stopSound(){
alSourceStop(source);
}
public void pauseSound(){
alSourcePause(source);
}
public void rewindSound(){
alSourceRewind(source);
}
public void setGain(float gain){
if(gain > 1.0f)gain = 1.0f;
if(gain < 0.0f)gain = 0.0f;
alSourcef(source, AL_GAIN, gain);
}
public void setPitch(float pitch){
if(pitch < 0.0f)pitch = 0.0f;
alSourcef(source, AL_PITCH, pitch);
}
public float getGain(){
return alGetSourcef(source, AL_GAIN);
}
public float getPitch(){
return alGetSourcef(source, AL_PITCH);
}
public void setLooping(boolean looping){
if(looping){
alSourcef(source, AL_LOOPING, AL_TRUE);
}else{
alSourcef(source, AL_LOOPING, AL_FALSE);
}
}
public void destroySound(){
alDeleteSources(source);
alDeleteBuffers(buffer);
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) throws FileNotFoundException {
this.fileName = fileName;
setSound();
}
public WaveData getSoundData() {
return soundData;
}
public void setSoundData(WaveData soundData) {
this.soundData = soundData;
}
public int getBuffer() {
return buffer;
}
public void setBuffer(int buffer) {
this.buffer = buffer;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
}

View file

@ -0,0 +1,40 @@
package mrdev023.audio;
import java.io.*;
import java.util.*;
import org.lwjgl.*;
import org.lwjgl.openal.*;
public class AudioManager {
public static ArrayList<Audio> music = new ArrayList<Audio>();
public static ArrayList<Audio> sound = new ArrayList<Audio>();
public static void create() throws LWJGLException{
AL.create();
}
public static void addMusic(String name){
try {
music.add(new Audio(name));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void addSound(String name){
try {
sound.add(new Audio(name));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void destroy(){
AL.destroy();
}
}

View file

@ -0,0 +1,123 @@
package mrdev023.blocks;
import java.io.*;
import mrdev023.math.*;
public abstract class Block implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4814279699620370899L;
public static final Block OAK_WOOD = new OakWoodBlock(),
GRASS = new GrassBlock(),
LEAF = new LeafBlock(),
FIR_LEAF = new FirLeafBlock(),
FIR_WOOD = new FirWoodBlock();
private float r,g,b,a;
private static final float size = 1f;
public Block(float r,float g,float b,float a){
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public Block(Color4f c) {
this.r = c.getR();
this.g = c.getG();
this.b = c.getB();
this.a = c.getA();
}
public Block setColor(float r,float g,float b){
this.r = r;
this.g = g;
this.b = b;
return this;
}
public Block setColor(Color4f c){
this.r = c.getR();
this.g = c.getG();
this.b = c.getB();
return this;
}
public float[] getDataFront(float x,float y,float z,float[] shading) {
return new float[]{
x,y,z, r * 0.9f * shading[0],g * 0.9f * shading[0],b * 0.9f * shading[0],a,
x+size,y,z, r * 0.9f * shading[1],g * 0.9f * shading[1],b * 0.9f * shading[1],a,
x+size,y+size,z, r * 0.9f * shading[2],g * 0.9f * shading[2],b * 0.9f * shading[2],a,
x,y+size,z, r * 0.9f * shading[3],g * 0.9f * shading[3],b * 0.9f * shading[3],a
};
}
public float[] getDataBack(float x,float y,float z,float[] shading) {
return new float[]{
x+size,y,z+size, r * 0.9f * shading[0],g * 0.9f * shading[0],b * 0.9f * shading[0],a,
x,y,z+size, r * 0.9f * shading[1],g * 0.9f * shading[1],b * 0.9f * shading[1],a,
x,y+size,z+size, r * 0.9f * shading[2],g * 0.9f * shading[2],b * 0.9f * shading[2],a,
x+size,y+size,z+size, r * 0.9f * shading[3],g * 0.9f * shading[3],b * 0.9f * shading[3],a
};
}
public float[] getDataLeft(float x,float y,float z,float[] shading) {
return new float[]{
x,y,z, r * 0.8f * shading[0],g * 0.8f * shading[0],b * 0.8f * shading[0],a,
x,y+size,z, r * 0.8f * shading[1],g * 0.8f * shading[1],b * 0.8f * shading[1],a,
x,y+size,z+size, r * 0.8f * shading[2],g * 0.8f * shading[2],b * 0.8f * shading[2],a,
x,y,z+size, r * 0.8f * shading[3],g * 0.8f * shading[3],b * 0.8f * shading[3],a
};
}
public float[] getDataRight(float x,float y,float z,float[] shading) {
return new float[]{
x+size,y+size,z, r * 0.8f * shading[0],g * 0.8f * shading[0],b * 0.8f * shading[0],a,
x+size,y,z, r * 0.8f * shading[1],g * 0.8f * shading[1],b * 0.8f * shading[1],a,
x+size,y,z+size, r * 0.8f * shading[2],g * 0.8f * shading[2],b * 0.8f * shading[2],a,
x+size,y+size,z+size, r * 0.8f * shading[3],g * 0.8f * shading[3],b * 0.8f * shading[3],a
};
}
public float[] getDataDown(float x,float y,float z,float[] shading) {
return new float[]{
x+size,y,z, r * 0.7f * shading[0],g * 0.7f * shading[0],b * 0.7f * shading[0],a,
x,y,z, r * 0.7f * shading[1],g * 0.7f * shading[1],b * 0.7f * shading[1],a,
x,y,z+size, r * 0.7f * shading[2],g * 0.7f * shading[2],b * 0.7f * shading[2],a,
x+size,y,z+size, r * 0.7f * shading[3],g * 0.7f * shading[3],b * 0.7f * shading[3],a
};
}
public float[] getDataUp(float x,float y,float z,float[] shading) {
return new float[]{
x,y+size,z, r * 1.0f * shading[0],g * 1.0f * shading[0],b * 1.0f * shading[0],a,
x+size,y+size,z, r * 1.0f * shading[1],g * 1.0f * shading[1],b * 1.0f * shading[1],a,
x+size,y+size,z+size, r * 1.0f * shading[2],g * 1.0f * shading[2],b * 1.0f * shading[2],a,
x,y+size,z+size, r * 1.0f * shading[3],g * 1.0f * shading[3],b * 1.0f * shading[3],a
};
}
public static float getSize() {
return size;
}
public abstract String toString();
}

View file

@ -0,0 +1,15 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class FirLeafBlock extends Block{
public FirLeafBlock() {
super(new Color4f(0.0f,0.4f,0f));
}
public String toString() {
return this.getClass().getSimpleName();
}
}

View file

@ -0,0 +1,11 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class FirWoodBlock extends WoodBlock{
public FirWoodBlock() {
super(new Color4f(0.32f,0.22f,0.04f));
}
}

View file

@ -0,0 +1,14 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class GrassBlock extends Block{
public GrassBlock() {
super(new Color4f(0.2f,0.6f,0f));
}
public String toString() {
return this.getClass().getSimpleName();
}
}

View file

@ -0,0 +1,15 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class LeafBlock extends Block{
public LeafBlock() {
super(new Color4f(0,0.6f,0,1));
}
public String toString() {
return this.getClass().getSimpleName();
}
}

View file

@ -0,0 +1,11 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class OakWoodBlock extends WoodBlock{
public OakWoodBlock() {
super(new Color4f(0.42f,0.32f,0.14f));
}
}

View file

@ -0,0 +1,15 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class TransparentBlock extends Block {
public TransparentBlock(){
super(new Color4f(1,1,1,0));
}
public String toString() {
return this.getClass().getSimpleName();
}
}

View file

@ -0,0 +1,15 @@
package mrdev023.blocks;
import mrdev023.math.*;
public class WoodBlock extends Block{
public WoodBlock(Color4f c) {
super(c);
}
public String toString() {
return this.getClass().getSimpleName();
}
}

View file

@ -0,0 +1,13 @@
package mrdev023.entities;
import mrdev023.entities.ia.*;
import mrdev023.entities.physics.*;
import mrdev023.world.*;
public class BlockMonster extends Entity{
public BlockMonster(World world, Physics physics, PathFinding pathFinding) {
super(world, physics, pathFinding);
}
}

View file

@ -0,0 +1,45 @@
package mrdev023.entities;
import mrdev023.entities.ia.*;
import mrdev023.entities.physics.*;
import mrdev023.world.*;
public abstract class Entity {
private World world;
private Physics physics;
private PathFinding pathFinding;
public Entity(World world,Physics physics,PathFinding pathFinding){
this.pathFinding = pathFinding;
this.world = world;
this.physics = physics;
}
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
public Physics getPhysics() {
return physics;
}
public void setPhysics(Physics physics) {
this.physics = physics;
}
public PathFinding getPathFinding() {
return pathFinding;
}
public void setPathFinding(PathFinding pathFinding) {
this.pathFinding = pathFinding;
}
}

View file

@ -0,0 +1,11 @@
package mrdev023.entities;
import mrdev023.world.*;
public class Player extends Entity{
public Player(World world) {
super(world, null, null);
}
}

View file

@ -0,0 +1,34 @@
package mrdev023.entities.ia;
import mrdev023.entities.*;
import mrdev023.world.*;
public class PathFinding {
private World world;
private Entity entity;
public PathFinding(World world,Entity entity){
this.world = world;
this.entity = entity;
}
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
}

View file

@ -0,0 +1,34 @@
package mrdev023.entities.physics;
import mrdev023.entities.*;
import mrdev023.world.*;
public class Physics {
private World world;
private Entity entity;
public Physics(World world,Entity entity){
this.entity = entity;
this.world = world;
}
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
public Entity getEntity() {
return entity;
}
public void setEntity(Entity entity) {
this.entity = entity;
}
}

View file

@ -0,0 +1,44 @@
package mrdev023.entity;
import java.util.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.world.*;
public class PlayerRayCast {
private List<Vector3f> points;
private int length = 10;
private int precision = 16;
public PlayerRayCast(){
this.points = new ArrayList<Vector3f>();
for(int i = 0; i < length * precision; i++){
points.add(new Vector3f());
}
}
public void update(){
int i = 0;
for(Vector3f v : points){
Vector3f pos = Camera.getPosition().copy().add(0f,Camera.getHeight(),0f).add(Camera.getDirection().copy().mul(i/(float)precision));
v.set(pos);
i++;
}
}
public Vector3f getBlock(World world){
for(Vector3f v : points){
boolean nx = false, ny = false, nz = false;
if(v.x < 0)nx = true;
if(v.y < 0)ny = true;
if(v.z < 0)nz = true;
boolean block = world.getBlock((int)v.x, (int)v.y, (int)v.z,nx,ny,nz) != null;
if(block) return new Vector3f(v.x, v.y,v.z);
}
return null;
}
}

View file

@ -0,0 +1,9 @@
package mrdev023.exception;
public class LostConnectionException extends Exception{
public LostConnectionException(String a){
super(a);
}
}

View file

@ -0,0 +1,45 @@
package mrdev023.game;
import mrdev023.game.gamestate.*;
import mrdev023.rendering.gui.*;
import mrdev023.world.*;
public class AboutMenu implements GameInterface{
public void render() {
}
public void update() {
}
public void renderGUI() {
}
public void destroyGameState() {
}
public World getWorld() {
return null;
}
public void updateMouse() {
}
public void updateKeyboard() {
}
public void updateGUI() {
}
public void init() {
}
}

View file

@ -0,0 +1,39 @@
package mrdev023.game;
import mrdev023.world.*;
public abstract class Game {
protected World world;
protected int update = 0;
public Game(World world){
this.world = world;
}
public abstract void update();
public abstract void render();
public abstract void renderGUI();
public World getWorld() {
return world;
}
public void setWorld(World world) {
this.world = world;
}
public int getUpdate() {
return update;
}
public void setUpdate(int update) {
this.update = update;
}
public void destroyGame(){
world.destroyWorld();
}
}

View file

@ -0,0 +1,109 @@
package mrdev023.game;
import static org.lwjgl.opengl.GL11.*;
import java.io.*;
import java.util.*;
import mrdev023.audio.*;
import mrdev023.game.gamestate.*;
import mrdev023.gameengine.*;
import mrdev023.math.*;
import mrdev023.rendering.gui.*;
import mrdev023.rendering.gui.action.*;
import mrdev023.world.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
public class MainMenu implements GameInterface{
public ArrayList<GUI> guiList = new ArrayList<GUI>();
public MainMenu(){}
public void init(){
GUI button = new ButtonGUI("SOLO",0.4f,0.3f,0.2f,0.075f);
button.setAction(new Action() {
public void manageAction(int action) {
if(action == GUI.CLICKED)GameEngine.changeGameState(GameState.SOLO_GAME);
}
public void hover(Vector2f position) {}
});
guiList.add(button);
button = new ButtonGUI("OPTION",0.4f,0.4f,0.2f,0.075f);
button.setAction(new Action() {
public void manageAction(int action) {
if(action == GUI.CLICKED)GameEngine.changeGameState(GameState.OPTION_MENU);
}
public void hover(Vector2f position) {}
});
guiList.add(button);
button = new ButtonGUI("ABOUT",0.4f,0.5f,0.2f,0.075f);
button.setAction(new Action() {
public void manageAction(int action) {
if(action == GUI.CLICKED)GameEngine.changeGameState(GameState.ABOUT_MENU);
}
public void hover(Vector2f position) {}
});
guiList.add(button);
button = new ButtonGUI("EXIT",0.4f,0.6f,0.2f,0.075f);
button.setAction(new Action() {
public void manageAction(int action) {
if(action == GUI.CLICKED)GameEngine.destroy();
}
public void hover(Vector2f position) {}
});
guiList.add(button);
guiList.add(new LabelGUI("Test Voxel v0.1", 0.34f,0.1f,0.3f,0.10f));
}
public void render() {
}
public void update() {
for(GUI gui : guiList){
gui.updateAction();
}
}
public void renderGUI() {
glColor3f(0,0,0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(Mouse.getX(),Display.getDisplayMode().getHeight() - Mouse.getY());
glEnd();
for(GUI gui : guiList){
gui.render();
}
}
public void destroyGameState() {
}
public World getWorld() {
return null;
}
public void updateMouse() {
}
public void updateKeyboard() {
}
public void updateGUI() {
for(GUI gui : guiList){
gui.updateGUI();
}
}
}

View file

@ -0,0 +1,209 @@
package mrdev023.game;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
import mrdev023.blocks.*;
import mrdev023.game.gamestate.*;
import mrdev023.gameengine.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.update.*;
import mrdev023.world.*;
public class MultiGame extends Game implements GameInterface{
private static Block selectedBlock = null;
private static Vector3f selectedVector = new Vector3f(0,0,0);
public MultiGame() {
super(null);
}
public void render(){
Camera.renderCamera();
this.world.render();
if(selectedVector != null && selectedVector !=null){
renderBlock((int)selectedVector.x,(int)selectedVector.y,(int)selectedVector.z);
}
}
public void update(){
Camera.getPlayerRaycast().update();
if(GameEngine.getGameState().getWorld() != null){
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()) != null){
boolean nx = false, ny = false, nz = false;
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).x < 0)nx = true;
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).y < 0)ny = true;
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).z < 0)nz = true;
selectedBlock = GameEngine.getGameState().getWorld().getBlock((int)Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getX(), (int)Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getY(), (int)Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getZ(),nx,ny,nz);
selectedVector = new Vector3f(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getX(), Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getY(), Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getZ());
}else{
selectedBlock = null;
selectedVector = null;
}
}
if(update >= 2){
world.update();
update = 0;
}
update++;
}
public void renderGUI() {
}
public void destroyGameState() {
destroyGame();
}
public void init() {
setWorld(new MultiWorld(0,120,50));
}
private static void renderBlock(int x,int y ,int z){
float s = 1;
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_CULL_FACE);
glLineWidth(2);
glBegin(GL_QUADS);
glVertex3f(x,y,z);
glVertex3f(x + s,y,z);
glVertex3f(x + s, y + s, z);
glVertex3f(x,y + s,z);
glVertex3f(x + s,y,z + s);
glVertex3f(x,y,z + s);
glVertex3f(x,y + s,z + s);
glVertex3f(x + s, y + s, z + s);
glVertex3f(x + s,y,z);
glVertex3f(x,y,z);
glVertex3f(x,y,z + s);
glVertex3f(x + s,y,z + s);
glVertex3f(x,y + s,z);
glVertex3f(x + s,y + s,z);
glVertex3f(x + s,y + s,z + s);
glVertex3f(x,y + s,z + s);
glVertex3f(x,y,z);
glVertex3f(x,y + s,z);
glVertex3f(x,y + s,z + s);
glVertex3f(x,y,z + s);
glVertex3f(x + s,y + s,z);
glVertex3f(x + s,y,z);
glVertex3f(x + s,y,z + s);
glVertex3f(x + s,y + s,z + s);
glEnd();
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
private static float xDir,yDir,zDir;
private static float speed = 0.02f;
private static float xa = 0,ya = 0,za = 0;
public void updateMouse() {
if(Mouse.isGrabbed()){
Camera.getRotation().x -= Mouse.getDY() * 0.5;
Camera.getRotation().y += Mouse.getDX() * 0.5;
while(Mouse.next()){
if(Mouse.getEventButtonState()){
}else{
}
}
}
}
public void updateKeyboard() {
xDir = 0;
yDir = 0;
zDir = 0;
while(Keyboard.next()){
if(Keyboard.getEventKeyState()){
if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE){
GameEngine.setRunning(false);
}
if(Keyboard.getEventKey() == Keyboard.KEY_F2){
Mouse.setGrabbed(!Mouse.isGrabbed());
}
if(Keyboard.getEventKey() == Keyboard.KEY_F5){
Camera.setPosition(new Vector3f(0,2,0));
}
if(Keyboard.getEventKey() == Keyboard.KEY_X){
Camera.noClip = !Camera.noClip;
}
}else{
}
}
if(Mouse.isGrabbed()){
if(Camera.getRotation().x < -90) Camera.getRotation().x = -90;
if(Camera.getRotation().x > 90) Camera.getRotation().x = 90;
if(Keyboard.isKeyDown(Keyboard.KEY_Z)){
zDir = -speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_S)){
zDir = speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_Q)){
xDir = -speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
xDir = speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
if(Camera.grounded)yDir = 0.3f;
if(!Camera.gravity)yDir = speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
speed = 0.01f;
}else{
speed = 0.02f;
}
if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)){
if(!Camera.gravity)yDir = -speed;
}
}
xa += xDir * Math.cos(Math.toRadians(Camera.getRotation().y)) - zDir * Math.sin(Math.toRadians(Camera.getRotation().y));
ya += yDir;
za += zDir * Math.cos(Math.toRadians(Camera.getRotation().y)) + xDir * Math.sin(Math.toRadians(Camera.getRotation().y));
Camera.move(xa,ya,za);
xa *= 0.9f;
ya *= 0.9f;
za *= 0.9f;
if(Display.isCloseRequested()){
GameEngine.setRunning(false);
}
}
public void updateGUI() {
}
}

View file

@ -0,0 +1,165 @@
package mrdev023.game;
import static org.lwjgl.opengl.GL11.*;
import java.util.*;
import org.lwjgl.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
import mrdev023.game.gamestate.*;
import mrdev023.gameengine.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.rendering.gui.*;
import mrdev023.rendering.gui.action.*;
import mrdev023.world.*;
public class OptionMenu implements GameInterface {
public ArrayList<GUI> guiList = new ArrayList<GUI>();
public DisplayMode[] availableDisplayMode;
public int selectDisplayMode = 0;
public GUI displayRender,fullscreenRender;
public boolean IsFullscreen;
public OptionMenu() {}
public void init() {
guiList.clear();
availableDisplayMode = DisplayManager.getDisplayModeArray();
IsFullscreen = Display.isFullscreen();
displayRender = new ButtonGUI(Display.getDisplayMode().getWidth() + "x"
+ Display.getDisplayMode().getHeight() + ":"
+ Display.getDisplayMode().getFrequency() + ":"
+ Display.getDisplayMode().getBitsPerPixel(),0.35f,0.3f,0.3f,0.075f).setAction(
new Action() {
public void manageAction(int action) {
}
public void hover(Vector2f position) {
}
}).setHoverAnimation(false);
guiList.add(new ButtonGUI("Back",0.2f,0.8f,0.2f,0.075f).setAction(new Action() {
public void manageAction(int action) {
if (action == GUI.CLICKED)
GameEngine.changeGameState(GameState.MAIN_MENU);
}
public void hover(Vector2f position) {
}
}));
guiList.add(new ButtonGUI("Apply",0.6f,0.8f,0.2f,0.075f).setAction(new Action() {
public void manageAction(int action) {
try {
Display.setDisplayMode(availableDisplayMode[selectDisplayMode]);
Display.setFullscreen(IsFullscreen);
DisplayManager.updateDisplay();
} catch (LWJGLException e) {}
}
public void hover(Vector2f position) {
}
}));
guiList.add(new ButtonGUI("<",0.1f,0.3f,0.2f,0.075f).setAction(new Action() {
public void manageAction(int action) {
if (action == GUI.CLICKED) {
selectDisplayMode--;
if (selectDisplayMode > availableDisplayMode.length - 1)
selectDisplayMode = 0;
if (selectDisplayMode < 0)
selectDisplayMode = availableDisplayMode.length - 1;
displayRender.setValue(availableDisplayMode[selectDisplayMode].getWidth()
+ "x"
+ availableDisplayMode[selectDisplayMode].getHeight()
+ ":"
+ availableDisplayMode[selectDisplayMode].getFrequency()
+ ":" + availableDisplayMode[selectDisplayMode].getBitsPerPixel() + " (" + (selectDisplayMode+1) + "/" + availableDisplayMode.length + ")");
}
}
public void hover(Vector2f position) {
}
}));
guiList.add(new ButtonGUI(">",0.7f,0.3f,0.2f,0.075f).setAction(new Action() {
public void manageAction(int action) {
if (action == GUI.CLICKED) {
selectDisplayMode++;
if (selectDisplayMode > availableDisplayMode.length - 1)
selectDisplayMode = 0;
if (selectDisplayMode < 0)
selectDisplayMode = availableDisplayMode.length - 1;
displayRender.setValue(availableDisplayMode[selectDisplayMode].getWidth()
+ "x"
+ availableDisplayMode[selectDisplayMode].getHeight()
+ ":"
+ availableDisplayMode[selectDisplayMode].getFrequency()
+ ":" + availableDisplayMode[selectDisplayMode].getBitsPerPixel());
}
}
public void hover(Vector2f position) {
}
}));
guiList.add(displayRender);
guiList.add((fullscreenRender = new ButtonGUI("Fullscreen: " + Display.isFullscreen(),0.4f,0.4f,0.2f,0.075f).setAction(
new Action() {
public void manageAction(int action) {
IsFullscreen = !IsFullscreen;
fullscreenRender.setValue("Fullscreen: " + IsFullscreen);
}
public void hover(Vector2f position) {
}
})));
guiList.add(new LabelGUI("Option", 0.34f,0.1f,0.3f,0.10f));
}
public void render() {
}
public void updateGUI() {
for(GUI gui : guiList){
gui.updateGUI();
}
}
public void update() {
for (GUI gui : guiList) {
gui.updateAction();
}
}
public void renderGUI() {
glColor3f(0, 0, 0);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(Mouse.getX(),
Display.getDisplayMode().getHeight() - Mouse.getY());
glEnd();
for (GUI gui : guiList) {
gui.render();
}
}
public void destroyGameState() {
}
public World getWorld() {
return null;
}
public void updateMouse() {
}
public void updateKeyboard() {
}
}

View file

@ -0,0 +1,196 @@
package mrdev023.game;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
import mrdev023.blocks.*;
import mrdev023.game.gamestate.*;
import mrdev023.gameengine.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.rendering.gui.*;
import mrdev023.update.*;
import mrdev023.world.*;
public class SoloGame extends Game implements GameInterface{
private static Block selectedBlock = null;
private static Vector3f selectedVector = new Vector3f(0,0,0);
public SoloGame() {
super(null);
}
public void render(){
Camera.renderCamera();
this.world.render();
if(selectedVector != null && selectedVector !=null){
renderBlock((int)selectedVector.x,(int)selectedVector.y,(int)selectedVector.z);
}
}
public void update(){
Camera.getPlayerRaycast().update();
if(GameEngine.getGameState().getWorld() != null){
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()) != null){
boolean nx = false, ny = false, nz = false;
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).x < 0)nx = true;
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).y < 0)ny = true;
if(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).z < 0)nz = true;
selectedBlock = GameEngine.getGameState().getWorld().getBlock((int)Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getX(), (int)Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getY(), (int)Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getZ(),nx,ny,nz);
selectedVector = new Vector3f(Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getX(), Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getY(), Camera.getPlayerRaycast().getBlock(GameEngine.getGameState().getWorld()).getZ());
}else{
selectedBlock = null;
selectedVector = null;
}
}
if(update >= 2){
world.update();
update = 0;
}
update++;
}
public void renderGUI() {
}
public void updateGUI() {
}
public void destroyGameState() {
destroyGame();
}
private static void renderBlock(int x,int y ,int z){
float s = 1;
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDisable(GL_CULL_FACE);
glColor3f(1, 1, 1);
glLineWidth(2);
glBegin(GL_QUADS);
glVertex3f(x,y,z);
glVertex3f(x + s,y,z);
glVertex3f(x + s, y + s, z);
glVertex3f(x,y + s,z);
glVertex3f(x + s,y,z + s);
glVertex3f(x,y,z + s);
glVertex3f(x,y + s,z + s);
glVertex3f(x + s, y + s, z + s);
glVertex3f(x + s,y,z);
glVertex3f(x,y,z);
glVertex3f(x,y,z + s);
glVertex3f(x + s,y,z + s);
glVertex3f(x,y + s,z);
glVertex3f(x + s,y + s,z);
glVertex3f(x + s,y + s,z + s);
glVertex3f(x,y + s,z + s);
glVertex3f(x,y,z);
glVertex3f(x,y + s,z);
glVertex3f(x,y + s,z + s);
glVertex3f(x,y,z + s);
glVertex3f(x + s,y + s,z);
glVertex3f(x + s,y,z);
glVertex3f(x + s,y,z + s);
glVertex3f(x + s,y + s,z + s);
glEnd();
glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
private static float xDir,yDir,zDir;
private static float speed = 0.02f;
private static float xa = 0,ya = 0,za = 0;
public void updateMouse() {
if(Mouse.isGrabbed()){
Camera.getRotation().x -= Mouse.getDY() * 0.5;
Camera.getRotation().y += Mouse.getDX() * 0.5;
while(Mouse.next()){
if(Mouse.getEventButtonState()){
}else{
}
}
}
}
public void updateKeyboard() {
xDir = 0;
yDir = 0;
zDir = 0;
if (Update.keyboardButtonPressed(Keyboard.KEY_ESCAPE))GameEngine.changeGameState(GameState.MAIN_MENU);
if (Update.keyboardButtonPressed(Keyboard.KEY_F2)) Mouse.setGrabbed(!Mouse.isGrabbed());
if (Update.keyboardButtonPressed(Keyboard.KEY_F5)) Camera.setPosition(new Vector3f(0, 2, 0));
if (Update.keyboardButtonPressed(Keyboard.KEY_X)) Camera.noClip = !Camera.noClip;
if(Mouse.isGrabbed()){
if(Camera.getRotation().x < -90) Camera.getRotation().x = -90;
if(Camera.getRotation().x > 90) Camera.getRotation().x = 90;
if(Keyboard.isKeyDown(Keyboard.KEY_Z)){
zDir = -speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_S)){
zDir = speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_Q)){
xDir = -speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_D)){
xDir = speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)){
if(Camera.grounded)yDir = 0.3f;
if(!Camera.gravity)yDir = speed;
}
if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)){
speed = 0.01f;
}else{
speed = 0.02f;
}
if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)){
if(!Camera.gravity)yDir = -speed;
}
}
xa += xDir * Math.cos(Math.toRadians(Camera.getRotation().y)) - zDir * Math.sin(Math.toRadians(Camera.getRotation().y));
ya += yDir;
za += zDir * Math.cos(Math.toRadians(Camera.getRotation().y)) + xDir * Math.sin(Math.toRadians(Camera.getRotation().y));
Camera.move(xa,ya,za);
xa *= 0.9f;
ya *= 0.9f;
za *= 0.9f;
if(Display.isCloseRequested()){
GameEngine.setRunning(false);
}
}
public void init() {
setWorld(new SoloWorld(0,120,50));
}
}

View file

@ -0,0 +1,17 @@
package mrdev023.game.gamestate;
import mrdev023.world.*;
public interface GameInterface {
public void render();
public void update();
public void renderGUI();
public void destroyGameState();
public World getWorld();
public void updateMouse();
public void updateKeyboard();
public void updateGUI();
public void init();
}

View file

@ -0,0 +1,51 @@
package mrdev023.game.gamestate;
import mrdev023.game.*;
import mrdev023.world.*;
public enum GameState {
MAIN_MENU(new MainMenu()),OPTION_MENU(new OptionMenu()),ABOUT_MENU(new AboutMenu()),SOLO_GAME(new SoloGame()),MULTI_GAME(new MultiGame());
GameInterface gameInterface;
GameState(GameInterface gameInterface){
this.gameInterface = gameInterface;
}
public void render(){
this.gameInterface.render();
}
public void update(){
this.gameInterface.update();
}
public void renderGUI(){
this.gameInterface.renderGUI();
}
public void destroyGameState(){
this.gameInterface.destroyGameState();
}
public World getWorld(){
return this.gameInterface.getWorld();
}
public void updateMouse() {
this.gameInterface.updateMouse();
}
public void updateKeyboard() {
this.gameInterface.updateKeyboard();
}
public void updateGUI() {
this.gameInterface.updateGUI();
}
public void init() {
this.gameInterface.init();
}
}

View file

@ -0,0 +1,184 @@
package mrdev023.gameengine;
import java.util.concurrent.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
import mrdev023.audio.*;
import mrdev023.game.*;
import mrdev023.game.gamestate.*;
import mrdev023.main.*;
import mrdev023.rendering.*;
import mrdev023.update.*;
import mrdev023.world.*;
public class GameEngine {
private static boolean IsRunning = true;
private static long current = System.currentTimeMillis(),current2, elapsedInfo = 0,
elapsed = 0, previous = 0;
private static long timeTicks = 0, timeFps = 0;
private static int FPS = 0, TICKS = 0, LAST_TICKS = 60, LAST_FPS = 60;
private static final String TITLE = "Test VBO";
private static final int width = 1280, height = 720;
public static GameState gameState = GameState.MAIN_MENU;
public static void initWindow(){
Main.mainPool = Executors.newWorkStealingPool(1);
try {
Display.setTitle(TITLE);
Display.setDisplayMode(new DisplayMode(width, height));
Display.setResizable(true);
Display.create();
AudioManager.create();
Camera.initCamera();
Mouse.setGrabbed(true);
changeGameState(GameState.MAIN_MENU);
loop();
} catch (Exception e) {
}
}
/**
* @Info Boucle principal avec Timer
*/
public static long time = 0;
public static void loop() {
while (IsRunning) {
previous = current;
current = System.currentTimeMillis();
elapsed += current - previous;
elapsedInfo += current - previous;
DisplayManager.setDelta(current - previous);
current2 = System.nanoTime();
if ((float)elapsed >= 1000.0f / 60.0f) {
Display.processMessages();
Update.updateMouse();
Update.updateKeyboard();
Update.update();
TICKS++;
elapsed -= (long)(1000.0f/60.0f);
timeTicks = System.nanoTime() - current2;
} else {
DisplayManager.clearScreen();
DisplayManager.preRender3D();
DisplayManager.render3D();
DisplayManager.preRender2D();
DisplayManager.render2D();
FPS++;
timeFps = System.nanoTime() - current2;
Display.update(false);
}
if (elapsedInfo >= 1000) {
LAST_FPS = FPS;
LAST_TICKS = TICKS;
Display.setTitle(TITLE + " | FPS:" + (int)(1000000000.0f/timeFps) + " TICKS:"
+ LAST_TICKS + " timeFps:" + timeFps + "ns timeTicks:"
+ timeTicks + "ns" + " | PX:"
+ Camera.getPosition().getX() + " PY:"
+ Camera.getPosition().getY() + " PZ:"
+ Camera.getPosition().getZ() + " | "
+ World.updateWorldTime + " " + DisplayManager.getDelta()
+ " | "
+ (int)((((float)Runtime.getRuntime().freeMemory()/1024.0)/((float)Runtime.getRuntime().totalMemory()/1024.0f))*100)
+ "% free | "
+ Runtime.getRuntime().totalMemory()/1024);
FPS = 0;
TICKS = 0;
elapsedInfo = 0;
}
}
destroy();
}
public static void changeGameState(GameState newGameState){
gameState.destroyGameState();
newGameState.init();
gameState = newGameState;
}
public static void destroy(){
gameState.destroyGameState();
AudioManager.destroy();
Display.destroy();
}
public static String getStringByNoString(Object... a){
String b = "";
for(Object c : a)b+= c + " ";
return b;
}
public static boolean isRunning() {
return IsRunning;
}
public static void setRunning(boolean isRunning) {
IsRunning = isRunning;
}
public static long getCurrent() {
return current;
}
public static long getElapsedInfo() {
return elapsedInfo;
}
public static long getElapsed() {
return elapsed;
}
public static long getPrevious() {
return previous;
}
public static long getTimeTicks() {
return timeTicks;
}
public static long getTimeFps() {
return timeFps;
}
public static int getFPS() {
return FPS;
}
public static int getTICKS() {
return TICKS;
}
public static int getLAST_TICKS() {
return LAST_TICKS;
}
public static int getLAST_FPS() {
return LAST_FPS;
}
public static String getTitle() {
return TITLE;
}
public static int getWidth() {
return width;
}
public static int getHeight() {
return height;
}
public static GameState getGameState() {
return gameState;
}
}

77
src/mrdev023/io/IO.java Normal file
View file

@ -0,0 +1,77 @@
package mrdev023.io;
import java.io.*;
import java.util.zip.*;
import mrdev023.blocks.*;
import mrdev023.math.*;
import mrdev023.world.*;
import mrdev023.world.chunk.*;
public class IO {
public static void saveChunk(Chunk c,String map) throws IOException{
long current = System.currentTimeMillis();
String file = "save" + File.separatorChar + map + File.separatorChar + c.getX() + "_" + c.getY() + "_" + c.getZ() + ".chunk";
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))));
oos.writeObject(c.getBlocks());
oos.flush();
oos.close();
System.out.println("File: " + file + " saved in " + (System.currentTimeMillis() - current) + "ms");
}
public static Chunk loadChunk(Vector3i c,String map) throws IOException, ClassNotFoundException{
long current = System.currentTimeMillis();
String file = "save" + File.separatorChar + map + File.separatorChar + c.getX() + "_" + c.getY() + "_" + c.getZ() + ".chunk";
FileInputStream fis=new FileInputStream(file);
GZIPInputStream gzis=new GZIPInputStream(fis);
BufferedInputStream bis = new BufferedInputStream(gzis);
ObjectInputStream in=new ObjectInputStream(bis);
Chunk c1 = new Chunk((int)c.getX(),(int)c.getY(),(int)c.getZ());
Block[][][] blocks = (Block[][][])in.readObject();
c1.setBlocks(blocks);
in.close();
gzis.close();
bis.close();
fis.close();
System.out.println("File: " + file + " loaded in " + (System.currentTimeMillis() - current) + "ms");
return c1;
}
public static boolean chunkIsSaved(Vector3i c,String map){
String file = "save" + File.separatorChar + map + File.separatorChar + c.getX() + "_" + c.getY() + "_" + c.getZ() + ".chunk";
File f = new File(file);
return f.isFile();
}
public static byte[] compressData(Object data) throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzout = new GZIPOutputStream(out);
BufferedOutputStream bout = new BufferedOutputStream(gzout);
ObjectOutputStream oos = new ObjectOutputStream(bout);
oos.writeObject(data);
oos.flush();
oos.close();
bout.close();
gzout.close();
byte[] outData = out.toByteArray();
out.close();
return outData;
}
public static Object decompressData(byte[] data) throws IOException, ClassNotFoundException{
ByteArrayInputStream in = new ByteArrayInputStream(data);
GZIPInputStream gzin = new GZIPInputStream(in);
BufferedInputStream bin = new BufferedInputStream(gzin);
ObjectInputStream ois = new ObjectInputStream(bin);
Object obj = ois.readObject();
ois.close();
gzin.close();
bin.close();
in.close();
return obj;
}
}

View file

@ -0,0 +1,34 @@
package mrdev023.main;
import java.util.concurrent.*;
import mrdev023.gameengine.*;
import mrdev023.network.server.*;
public class Main {
public static ExecutorService mainPool;
/**
* @param args
* @Info Fonction principal
*/
public static void main(String[] args) {
boolean IsServer = false;
for(String arg : args){
if(arg.equals("-server"))IsServer = true;
}
if(IsServer){
Server.initServer();
}else{
GameEngine.initWindow();
}
}
public static Runnable addThread(Runnable t,String name){
mainPool.submit(t);
return t;
}
}

View file

@ -0,0 +1,107 @@
package mrdev023.math;
public class Color4f {
private float r,g,b,a;
public Color4f(float r,float g,float b,float a){
this.r = r;
this.b = b;
this.g = g;
this.a = a;
}
public Color4f(float r,float g,float b){
this.r = r;
this.b = b;
this.g = g;
this.a = 1;
}
public Color4f(Color4f c){
this.r = c.getR();
this.b = c.getG();
this.g = c.getB();
this.a = c.getA();
}
public static Color4f interpolate(Color4f ca, Color4f cb, float t){
float r = ca.r + (cb.r - ca.r) * t;
float g = ca.g + (cb.g - ca.g) * t;
float b = ca.b + (cb.b - ca.b) * t;
float a = ca.a + (cb.a - ca.a) * t;
return new Color4f(r,g,b,a);
}
public Color4f add(Color4f c){
r += c.getR();
g += c.getG();
b += c.getB();
a += c.getA();
return this;
}
public Color4f sub(Color4f c){
r -= c.getR();
g -= c.getG();
b -= c.getB();
a -= c.getA();
return this;
}
public Color4f mul(float c){
r *= c;
g *= c;
b *= c;
a *= c;
return this;
}
public Color4f copy(){
return new Color4f(r,g,b,a);
}
public void div(Color4f c){
r /= c.getR();
g /= c.getG();
b /= c.getB();
a /= c.getA();
}
public float getR() {
return r;
}
public void setR(float r) {
this.r = r;
}
public float getG() {
return g;
}
public void setG(float g) {
this.g = g;
}
public float getB() {
return b;
}
public void setB(float b) {
this.b = b;
}
public float getA() {
return a;
}
public void setA(float a) {
this.a = a;
}
public java.awt.Color toAwtColor(){
return new java.awt.Color(r, g, g, a);
}
}

View file

@ -0,0 +1,37 @@
package mrdev023.math;
public class Vector2f {
public float x,y;
public Vector2f(float x,float y){
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public Vector2f copy() {
return new Vector2f(x,y);
}
public String toString(){
return x + " " + y;
}
}

View file

@ -0,0 +1,200 @@
package mrdev023.math;
public class Vector3f {
public float x, y, z;
public Vector3f() {
this(0, 0, 0);
}
public Vector3f(Vector3f v) {
this(v.x, v.y, v.z);
}
public Vector3f(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public float length() {
return (float) Math.sqrt(x * x + y * y + z * z);
}
public Vector3f normalize() {
x /= length();
y /= length();
z /= length();
return this;
}
public Vector3f add(Vector3f vec) {
x += vec.getX();
y += vec.getY();
z += vec.getZ();
return this;
}
public Vector3f check(){
float max = Math.max(Math.max(x, y),z);
float min = Math.min(Math.min(x, y),z);
float absMax = Math.abs(max - 1);
float absMin = Math.abs(min);
float v = 0;
if(absMax>absMin)v=min;
else v=max;
int rv = 1;
if(v<0.5f)rv=-1;
return new Vector3f(v == x ? rv : 0,v == y ? rv : 0,v == z ? rv : 0);
}
public Vector3f copy(){
return new Vector3f(this);
}
public Vector3f sub(Vector3f vec) {
x -= vec.getX();
y -= vec.getY();
z -= vec.getZ();
return this;
}
public void set(Vector3f vec){
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
}
public Vector3f mul(Vector3f vec) {
x *= vec.getX();
y *= vec.getY();
z *= vec.getZ();
return this;
}
public Vector3f div(Vector3f vec) {
x /= vec.getX();
y /= vec.getY();
z /= vec.getZ();
return this;
}
public Vector3f add(float v) {
x += v;
y += v;
z += v;
return this;
}
public Vector3f add(float x,float y, float z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
public Vector3f sub(float v) {
x -= v;
y -= v;
z -= v;
return this;
}
public Vector3f mul(float v) {
x *= v;
y *= v;
z *= v;
return this;
}
public Vector3f div(float v) {
x /= v;
y /= v;
z /= v;
return this;
}
// ---- X
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public Vector3f addX(float v) {
x += v;
return this;
}
public Vector3f subX(float v) {
x -= v;
return this;
}
// ----- Y
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public Vector3f addY(float v) {
y += v;
return this;
}
public Vector3f subY(float v) {
y -= v;
return this;
}
// ----- Z
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public Vector3f addZ(float v) {
z += v;
return this;
}
public Vector3f subZ(float v) {
z -= v;
return this;
}
public String toString(){
return x + " " + y + " " + z;
}
public static float distance(Vector3f a,Vector3f b){
return (float)Math.sqrt((Math.pow(b.getX()-a.getX(), 2))+(Math.pow(b.getY()-a.getY(), 2))+(Math.pow(b.getZ()-a.getZ(), 2)));
}
}

View file

@ -0,0 +1,200 @@
package mrdev023.math;
public class Vector3i {
public int x, y, z;
public Vector3i() {
this(0, 0, 0);
}
public Vector3i(Vector3i v) {
this(v.x, v.y, v.z);
}
public Vector3i(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int length() {
return (int) Math.sqrt(x * x + y * y + z * z);
}
public Vector3i normalize() {
x /= length();
y /= length();
z /= length();
return this;
}
public Vector3i add(Vector3i vec) {
x += vec.getX();
y += vec.getY();
z += vec.getZ();
return this;
}
public Vector3i check(){
int max = Math.max(Math.max(x, y),z);
int min = Math.min(Math.min(x, y),z);
int absMax = Math.abs(max - 1);
int absMin = Math.abs(min);
int v = 0;
if(absMax>absMin)v=min;
else v=max;
int rv = 1;
if(v<0.5f)rv=-1;
return new Vector3i(v == x ? rv : 0,v == y ? rv : 0,v == z ? rv : 0);
}
public Vector3i copy(){
return new Vector3i(this);
}
public Vector3i sub(Vector3i vec) {
x -= vec.getX();
y -= vec.getY();
z -= vec.getZ();
return this;
}
public void set(Vector3i vec){
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
}
public Vector3i mul(Vector3i vec) {
x *= vec.getX();
y *= vec.getY();
z *= vec.getZ();
return this;
}
public Vector3i div(Vector3i vec) {
x /= vec.getX();
y /= vec.getY();
z /= vec.getZ();
return this;
}
public Vector3i add(int v) {
x += v;
y += v;
z += v;
return this;
}
public Vector3i add(int x,int y, int z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
public Vector3i sub(int v) {
x -= v;
y -= v;
z -= v;
return this;
}
public Vector3i mul(int v) {
x *= v;
y *= v;
z *= v;
return this;
}
public Vector3i div(int v) {
x /= v;
y /= v;
z /= v;
return this;
}
// ---- X
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public Vector3i addX(int v) {
x += v;
return this;
}
public Vector3i subX(int v) {
x -= v;
return this;
}
// ----- Y
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Vector3i addY(int v) {
y += v;
return this;
}
public Vector3i subY(int v) {
y -= v;
return this;
}
// ----- Z
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
public Vector3i addZ(int v) {
z += v;
return this;
}
public Vector3i subZ(int v) {
z -= v;
return this;
}
public String toString(){
return x + " " + y + " " + z;
}
public static int distance(Vector3i a,Vector3i b){
return (int)Math.sqrt((Math.pow(b.getX()-a.getX(), 2))+(Math.pow(b.getY()-a.getY(), 2))+(Math.pow(b.getZ()-a.getZ(), 2)));
}
}

View file

@ -0,0 +1,149 @@
package mrdev023.network.client;
import java.io.*;
import java.net.*;
import mrdev023.network.packet.*;
import mrdev023.network.packet.wait.*;
public class Client {
private String ip;
private short port;
private Socket connection;
public boolean IsError = false;
public boolean IsRunning = true;
private ObjectOutputStream out;
private PacketManager paketManager;
private Thread threadPacketManager;
public Client(String ip,short port)throws Exception{
this.ip = ip;
this.port = port;
this.connection = new Socket(ip, port);
this.out = new ObjectOutputStream(connection.getOutputStream());
this.paketManager = new PacketManager(this);
this.threadPacketManager = new Thread(this.paketManager);
this.threadPacketManager.start();
}
public void sendData(Packet packet) throws IOException{
out.writeObject(packet);
out.flush();
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public short getPort() {
return port;
}
public void setPort(short port) {
this.port = port;
}
public Socket getConnection() {
return connection;
}
public void setConnection(Socket connection) {
this.connection = connection;
}
public boolean isIsError() {
return IsError;
}
public void setIsError(boolean isError) {
IsError = isError;
}
public boolean isIsRunning() {
return IsRunning;
}
public void setIsRunning(boolean isRunning) {
IsRunning = isRunning;
}
public ObjectOutputStream getOut() {
return out;
}
public void setOut(ObjectOutputStream out) {
this.out = out;
}
public PacketManager getPaketManager() {
return paketManager;
}
public void setPaketManager(PacketManager paketManager) {
this.paketManager = paketManager;
}
public Thread getThreadPacketManager() {
return threadPacketManager;
}
public void setThreadPacketManager(Thread threadPacketManager) {
this.threadPacketManager = threadPacketManager;
}
}
class ClientReader implements Runnable{
private Client client;
private ObjectInputStream in;
public ClientReader(Client client){
this.client = client;
}
public void run() {
while(client.IsRunning){
try {
Object packet = in.readObject();
if(packet != null){
if(packet instanceof WaitPacket){
WaitPacketManager.setWaitPacket(((WaitPacket)packet).getId(), (WaitPacket)packet);
}else if(packet instanceof Packet){
client.getPaketManager().addPacket((Packet)packet);
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
client.IsError = true;
e.printStackTrace();
}
}
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
public ObjectInputStream getIn() {
return in;
}
public void setIn(ObjectInputStream in) {
this.in = in;
}
}

View file

@ -0,0 +1,31 @@
package mrdev023.network.client;
import java.util.*;
import java.util.Map.Entry;
import mrdev023.network.*;
import mrdev023.network.packet.*;
public class PacketManager implements Runnable{
public ArrayList<Packet> packetList = new ArrayList<Packet>();
public Client client;
public PacketManager(Client client){
this.client = client;
}
public void run() {
while(client.IsRunning){
for (Packet e : packetList) {
e.manageClientPacket(client, e);
}
}
}
public void addPacket(Packet packet){
packetList.add(packet);
}
}

View file

@ -0,0 +1,25 @@
package mrdev023.network.client;
import java.io.*;
import java.util.*;
import mrdev023.network.packet.wait.*;
public class WaitPacketManager{
public static HashMap<String,WaitPacket> waitPacketList = new HashMap<String,WaitPacket>();
public static void sendWaitPacket(WaitPacket packet,Client client) throws IOException{
packet.sendPacket(client, packet);
client.sendData(packet);
waitPacketList.put(packet.getId(), packet);
}
public static boolean isRep(String id){
return waitPacketList.get(id).getIsRep();
}
public static void setWaitPacket(String id,WaitPacket packet){
waitPacketList.replace(id, packet);
}
}

View file

@ -0,0 +1,53 @@
package mrdev023.network.packet;
import java.io.*;
import mrdev023.io.*;
import mrdev023.math.*;
import mrdev023.network.client.*;
import mrdev023.network.packet.wait.*;
import mrdev023.network.server.*;
import mrdev023.world.chunk.*;
public class ChunkPacket implements Packet{
public Vector3i position;
public Chunk chunk = null;
public ChunkPacket(Vector3i position){
this.position = position;
this.chunk = null;
}
public void setChunk(Chunk ch){
this.chunk = ch;
}
public void manageClientPacket(Client client, Packet packet) {
WaitPacketManager.setWaitPacket(((WaitPacket)packet).getId(), (WaitPacket)packet);
}
public void manageServerPacket(ClientConnection client, Packet packet) {
if((((WaitPacket)packet).getPacket()) != null){
Chunk ch = (Chunk)((((WaitPacket)packet).getPacket()));
if(Server.getChunk(ch.getPosition()) == null){
try {
IO.saveChunk(ch, "multiWorld");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
try {
Vector3i pos = ((ChunkPacket)(((WaitPacket)packet).getPacket())).position;
((WaitPacket)packet).setPacket(Server.getChunk(pos));
client.sendData(packet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,12 @@
package mrdev023.network.packet;
import mrdev023.network.*;
import mrdev023.network.client.*;
import mrdev023.network.server.*;
public interface Packet {
public void manageClientPacket(Client client,Packet packet);
public void manageServerPacket(ClientConnection client,Packet packet);
}

View file

@ -0,0 +1,17 @@
package mrdev023.network.packet;
import mrdev023.network.*;
import mrdev023.network.client.*;
import mrdev023.network.server.*;
public class TestPacket implements Packet {
public void manageClientPacket(Client client, Packet packet) {
}
public void manageServerPacket(ClientConnection client, Packet packet) {
}
}

View file

@ -0,0 +1,28 @@
package mrdev023.network.packet.wait;
import mrdev023.network.client.*;
import mrdev023.network.packet.*;
import mrdev023.network.server.*;
public class ChunkWaitPacket extends WaitPacket{
public ChunkWaitPacket(String id) {
super("chunkWaitPacket" + (Math.random()*20000));
}
public void manageClientPacket(Client client, Packet packet) {
((ChunkPacket)(((WaitPacket)packet).getPacket())).manageClientPacket(client, packet);
}
public void manageServerPacket(ClientConnection client, Packet packet) {
((ChunkPacket)(((WaitPacket)packet).getPacket())).manageServerPacket(client, packet);
}
public void repPacket(ClientConnection client, WaitPacket packet) {
packet.IsRep = true;
}
public void sendPacket(Client client, WaitPacket packet) {
}
}

View file

@ -0,0 +1,36 @@
package mrdev023.network.packet.wait;
import mrdev023.network.client.*;
import mrdev023.network.packet.*;
import mrdev023.network.server.*;
public abstract class WaitPacket implements Packet{
protected boolean IsRep = false;
protected String id;
protected Object packet;
public WaitPacket(String id){
this.id = id;
}
public String getId(){
return this.id;
}
public boolean getIsRep(){
return this.IsRep;
}
public Object getPacket(){
return this.packet;
}
public void setPacket(Object obj){
this.packet = obj;
}
public abstract void repPacket(ClientConnection client,WaitPacket packet);
public abstract void sendPacket(Client client,WaitPacket packet);
}

View file

@ -0,0 +1,130 @@
package mrdev023.network.server;
import java.io.*;
import java.net.*;
import java.util.*;
import mrdev023.network.packet.*;
import mrdev023.network.packet.wait.*;
public class ClientConnection {
private Socket connection;
private ObjectOutputStream out;
private ClientReader in;
private Thread th;
public boolean IsRunning = true;
public boolean IsError = false;
public ClientConnection(Socket connection) throws IOException{
this.connection = connection;
out = new ObjectOutputStream(connection.getOutputStream());
in = new ClientReader(this);
}
public ClientConnection start(){
th = new Thread(in);
th.start();
return this;
}
public void sendData(Packet packet) throws IOException{
out.writeObject(packet);
out.flush();
}
public Socket getConnection() {
return connection;
}
public void setConnection(Socket connection) {
this.connection = connection;
}
public void destroy(){
th.stop();
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
connection = null;
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
try {
in.in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
IsRunning = false;
}
public ObjectOutputStream getOut() {
return out;
}
public void setOut(ObjectOutputStream out) {
this.out = out;
}
public ClientReader getIn() {
return in;
}
public void setIn(ClientReader in) {
this.in = in;
}
public Thread getTh() {
return th;
}
public void setTh(Thread th) {
this.th = th;
}
}
class ClientReader implements Runnable{
public ClientConnection client;
public ObjectInputStream in;
public ClientReader(ClientConnection client) throws IOException{
this.client = client;
in = new ObjectInputStream(client.getConnection().getInputStream());
}
public void run() {
while(client.IsRunning){
try {
Object packet = in.readObject();
if(packet != null){
if(packet instanceof WaitPacket){
manageWaitPacket((WaitPacket)packet);
}else if(packet instanceof Packet){
if(packet != null){
Server.packetManager.addPacket(client, (Packet)packet);
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
client.IsError = true;
e.printStackTrace();
}
}
}
public void manageWaitPacket(WaitPacket packet){
packet.repPacket(client,packet);
}
}

View file

@ -0,0 +1,32 @@
package mrdev023.network.server;
import java.util.*;
import java.util.Map.Entry;
import mrdev023.network.*;
import mrdev023.network.packet.*;
import mrdev023.network.server.*;
public class PacketManager implements Runnable{
public HashMap<Packet,ClientConnection> packetList = new HashMap<Packet,ClientConnection>();
public PacketManager(){
}
public void run() {
while(Server.IsRunning){
for (Entry<Packet, ClientConnection> e : packetList.entrySet()) {
Packet packet = e.getKey();
ClientConnection client = e.getValue();
packet.manageServerPacket(client, packet);
}
}
}
public void addPacket(ClientConnection client,Packet packet){
packetList.put(packet, client);
}
}

View file

@ -0,0 +1,71 @@
package mrdev023.network.server;
import java.io.*;
import java.net.*;
import java.util.*;
import mrdev023.exception.*;
import mrdev023.io.*;
import mrdev023.math.*;
import mrdev023.network.packet.*;
import mrdev023.world.chunk.*;
public class Server {
public static ServerSocket server;
public static PacketManager packetManager;
public static Thread threadPacketManager;
public static boolean IsRunning = true;
public static ArrayList<ClientConnection> clientList = new ArrayList<ClientConnection>();
public static void initServer(){
try {
server = new ServerSocket(9999);
server.setSoTimeout(1000);
packetManager = new PacketManager();
(threadPacketManager = new Thread(packetManager)).start();
while(IsRunning){
try{
Socket client = server.accept();
clientList.add(new ClientConnection(client).start());
System.out.println("Client:" + client.getInetAddress().getHostAddress() + ":" + client.getPort() + " join game");
}catch(Exception e){}
for (ClientConnection cl : clientList) {
try{
cl.sendData(new TestPacket());
if(cl.IsError)throw new LostConnectionException("Data Error");
} catch (Exception e) {
cl = null;
clientList.remove(cl);
System.out.println("Client:"
+ cl.getConnection().getInetAddress()
.getHostAddress() + ":"
+ cl.getConnection().getPort() + " left game");
}
}
}
for(ClientConnection client : clientList){
client.destroy();
}
server.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public static Chunk getChunk(Vector3i pos){
try {
return IO.loadChunk(pos, "multiWorld");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

View file

@ -0,0 +1,291 @@
package mrdev023.rendering;
import static org.lwjgl.opengl.GL11.*;
import mrdev023.blocks.*;
import mrdev023.entity.*;
import mrdev023.gameengine.*;
import mrdev023.main.*;
import mrdev023.math.*;
import mrdev023.world.*;
import mrdev023.world.chunk.*;
public class Camera {
private static Vector3f position = new Vector3f(0, 4, 0);
private static Vector2f rotation = new Vector2f(0, 120);
private static PlayerRayCast playerRaycast;
private static final float HEIGHT = (float) (1.5f - (Block.getSize() / 2.0f));
public static boolean gravity = true;
public static float gravityFactor = 0;
public static boolean grounded = false;
public static boolean noClip = false;
public static void initCamera() {
playerRaycast = new PlayerRayCast();
}
public static void renderCamera() {
if (position.getY() < (int)((new Noise(GameEngine.getGameState().getWorld().seed, GameEngine
.getGameState().getWorld().octave,
GameEngine.getGameState().getWorld().amplitude).getNoise(position.getX(),
position.getZ())) + 1)) {
position.setY((int)((new Noise(GameEngine.getGameState().getWorld().seed, GameEngine
.getGameState().getWorld().octave,
GameEngine.getGameState().getWorld().amplitude).getNoise(position.getX(),
position.getZ())) + 2));
}
glLoadIdentity();
glRotatef(rotation.getX(), 1, 0, 0);
glRotatef(rotation.getY(), 0, 1, 0);
glTranslatef(-position.getX(), -(position.getY() + getHeight()),
-position.getZ());
}
public static void move(float xa, float ya, float za) {
gravityFactor += World.GRAVITY * 0.01f;
if (grounded)
gravityFactor = 0;
ya -= gravityFactor;
int xStep = (int) Math.abs(xa * 100);
for (int i = 0; i < xStep; i++) {
if (!isColliding(xa / xStep, 0, 0) || noClip) {
position.x += xa / xStep;
} else {
xa = 0;
}
}
int yStep = (int) Math.abs(ya * 100);
for (int i = 0; i < yStep; i++) {
if (!isColliding(0, ya / yStep, 0) || noClip) {
position.y += ya / yStep;
} else {
ya = 0;
}
}
int zStep = (int) Math.abs(za * 100);
for (int i = 0; i < zStep; i++) {
if (!isColliding(0, 0, za / zStep) || noClip) {
position.z += za / zStep;
} else {
za = 0;
}
}
}
public static Vector3f getDirection() {
Vector3f r = new Vector3f();
float cosY = (float) Math.cos(Math.toRadians(rotation.getY() - 90));
float sinY = (float) Math.sin(Math.toRadians(rotation.getY() - 90));
float cosP = (float) Math.cos(Math.toRadians(-rotation.getX()));
float sinP = (float) Math.sin(Math.toRadians(-rotation.getX()));
r.setX(cosY * cosP);
r.setY(sinP);
r.setZ(sinY * cosP);
r.normalize();
return r;
}
public static Vector3f getPosition() {
return position;
}
public static void setPosition(Vector3f position) {
Camera.position = position;
}
public static Vector2f getRotation() {
return rotation;
}
public static void setRotation(Vector2f rotation) {
Camera.rotation = rotation;
}
public static float getHeight() {
return HEIGHT;
}
public static PlayerRayCast getPlayerRaycast() {
return playerRaycast;
}
public static void setPlayerRaycast(PlayerRayCast playerRaycast) {
Camera.playerRaycast = playerRaycast;
}
public static boolean isColliding(float xa, float ya, float za) {
World world = GameEngine.getGameState().getWorld();
float r = 0.3f;
boolean nx = false, ny = false, nz = false;
int x0 = (int) (position.x + xa - r);
int x1 = (int) (position.x + xa + r);
int y0 = (int) (position.y + ya - r);
int y1 = (int) (position.y + ya + r);
int z0 = (int) (position.z + za - r);
int z1 = (int) (position.z + za + r);
int xmid = (int) (position.x + xa);
int yb = (int) (position.y + ya - r - 0.01f);
int zmid = (int) (position.z + za);
if (xmid < 0)
nx = true;
else
nx = false;
if (yb < 0)
ny = true;
else
ny = false;
if (zmid < 0)
nz = true;
else
nz = false;
if (world.getBlock(xmid, yb, zmid, nx, ny, nz) != null) {
grounded = true;
} else {
grounded = false;
}
if (x0 < 0)
nx = true;
else
nx = false;
if (y0 < 0)
ny = true;
else
ny = false;
if (z0 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x0, y0, z0, nx, ny, nz) != null)
return true;
if (x1 < 0)
nx = true;
else
nx = false;
if (y0 < 0)
ny = true;
else
ny = false;
if (z0 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x1, y0, z0, nx, ny, nz) != null)
return true;
if (x1 < 0)
nx = true;
else
nx = false;
if (y1 < 0)
ny = true;
else
ny = false;
if (z0 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x1, y1, z0, nx, ny, nz) != null)
return true;
if (x0 < 0)
nx = true;
else
nx = false;
if (y1 < 0)
ny = true;
else
ny = false;
if (z0 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x0, y1, z0, nx, ny, nz) != null)
return true;
if (x0 < 0)
nx = true;
else
nx = false;
if (y0 < 0)
ny = true;
else
ny = false;
if (z1 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x0, y0, z1, nx, ny, nz) != null)
return true;
if (x1 < 0)
nx = true;
else
nx = false;
if (y0 < 0)
ny = true;
else
ny = false;
if (z1 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x1, y0, z1, nx, ny, nz) != null)
return true;
if (x1 < 0)
nx = true;
else
nx = false;
if (y1 < 0)
ny = true;
else
ny = false;
if (z1 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x1, y1, z1, nx, ny, nz) != null)
return true;
if (x0 < 0)
nx = true;
else
nx = false;
if (y1 < 0)
ny = true;
else
ny = false;
if (z1 < 0)
nz = true;
else
nz = false;
if (world.getBlock(x0, y1, z1, nx, ny, nz) != null)
return true;
// if(world.getBlock(x0, y0+1, z0) != null) return true;
// if(world.getBlock(x1, y0+1, z0) != null) return true;
// if(world.getBlock(x1, y1+1, z0) != null) return true;
// if(world.getBlock(x0, y1+1, z0) != null) return true;
//
// if(world.getBlock(x0, y0+1, z1) != null) return true;
// if(world.getBlock(x1, y0+1, z1) != null) return true;
// if(world.getBlock(x1, y1+1, z1) != null) return true;
// if(world.getBlock(x0, y1+1, z1) != null) return true;
return false;
}
}

View file

@ -0,0 +1,132 @@
package mrdev023.rendering;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;
import java.util.*;
import mrdev023.gameengine.*;
import org.lwjgl.*;
import org.lwjgl.opengl.*;
public class DisplayManager {
private static long delta = 0;
private static float fov = 45;
private static int height = GameEngine.getHeight(),width = GameEngine.getWidth();
/**
* @Info Nettoie l'ecran
*/
public static void clearScreen(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.19f, 0.79f, 0.82f, 1);
}
/**
* @Info Definie le mode d'affichage pour le rendu en 3d
*/
public static void preRender3D(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, (float)Display.getDisplayMode().getWidth()/(float)Display.getDisplayMode().getHeight(), 0.1f, 1000f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
glEnable(GL_TEXTURE_2D);
}
/**
* @Info Fait le rendu 3d
*/
public static void render3D(){
GameEngine.getGameState().render();
}
/**
* @Info Definie le mode d'affichage pour le rendu en 2d
*/
public static void preRender2D(){
glDisable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Display.getDisplayMode().getWidth(),Display.getDisplayMode().getHeight(),0,-1,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/**
* @Info Fait le rendu 2d
*/
public static void render2D(){
GameEngine.getGameState().renderGUI();
}
/**
* @Info mets a jour la resolution de l'ecran
*/
public static void updateDisplay() {
width = Display.getDisplayMode().getWidth();
height = Display.getDisplayMode().getHeight();
glViewport(0, 0, width,height);
GameEngine.gameState.updateGUI();
}
public static DisplayMode[] getDisplayModeArray(){
try {
ArrayList<DisplayMode> dl = new ArrayList<DisplayMode>();
for(DisplayMode d : Display.getAvailableDisplayModes()){
if(d.getBitsPerPixel() == 32 && d.getFrequency() == 60){
dl.add(d);
}
}
DisplayMode[] dl2 = new DisplayMode[dl.size()];
for(int i = 0;i < dl2.length;i++){
dl2[i] = dl.get(i);
}
dl = null;
return dl2;
} catch (LWJGLException e) {
e.printStackTrace();
return null;
}
}
public static long getDelta() {
return delta;
}
public static void setDelta(long delta) {
DisplayManager.delta = delta;
}
public static float getFov() {
return fov;
}
public static void setFov(float fov) {
DisplayManager.fov = fov;
}
public static int getHeight() {
return height;
}
public static void setHeight(int height) {
DisplayManager.height = height;
}
public static int getWidth() {
return width;
}
public static void setWidth(int width) {
DisplayManager.width = width;
}
}

View file

@ -0,0 +1,117 @@
package mrdev023.rendering;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import java.nio.*;
import java.util.*;
import org.lwjgl.*;
public class VBO {
private int bufferSize = 0;
private int vboID = 0;
private FloatBuffer buffer;
private ArrayList<Float> floatlist = new ArrayList<Float>();
private static final int FLOAT_SIZE = 4;
public VBO(){
this.vboID = createVBO();
}
/**
* @Info creer le vbo
*/
public int createVBO(){
return glGenBuffers();
}
/**
* @param a
* @Info () doit contenir par ligne au moins l'axe (x,y,z) + les couleurs (r,g,b,a)
* Ex: getFloatBufferByFloatArray(new float[]{x,y,z,r,g,b,a})
*
*
*/
public void clearBuffer(){
floatlist.clear();
}
public void addDataByFloatArray(float[] a){
if(a == null)return;
for(float c :a){
floatlist.add(c);
}
}
/**
* @param a
* @Info () doit contenir par ligne au moins l'axe (x,y,z) + les couleurs (r,g,b,a)
* Ex: getFloatBufferbyFloatList(x,y,z,r,g,b,a)
*
*
*/
public void addDataByFloatList(float... a){
if(a == null)return;
for(float c :a){
floatlist.add(c);
}
}
/**
* @param id
* @param data
* @Info Stocke le FloatBuffer dans le GPU
*/
public void bufferData(){
buffer = BufferUtils.createFloatBuffer(floatlist.size());
for(Float f : floatlist){
buffer.put(f);
}
buffer.flip();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
// glBufferSubData(vboID, 0, buffer);
glBindBuffer(GL_ARRAY_BUFFER, 0);
bufferSize = buffer.limit() / 7;// 7 = 3 vertex(x,y,z) + 4 color (r,g,b,a)
}
/**
* @Info fait le rendu du vbo
*/
public void renderVBO(){
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, (3 + 4) * FLOAT_SIZE, 0);
glColorPointer(4, GL_FLOAT, (3 + 4) * FLOAT_SIZE, 3 * FLOAT_SIZE);
//x,y,z,r,g,b,a,nx,ny,nz
//glVertexPointer(3, GL_FLOAT, (3 + 4 + 3) * FLOAT_SIZE, 0);
//glColorPointer(4, GL_FLOAT, (3 + 4 + 3) * FLOAT_SIZE, 3 * FLOAT_SIZE);
//glNormalPointer(4, GL_FLOAT, (3 + 4 + 3) * FLOAT_SIZE, (3 + 4) * FLOAT_SIZE);
// glVertexAttribPointer(0,3,GL_FLOAT,false,7 * 4,0);
// glVertexAttribPointer(1,4,GL_FLOAT,false,7 * 4,12);
glDrawArrays(GL_QUADS, 0, bufferSize);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
/**
* @Info detruit le vbo
*/
public void destroyVBO(){
glDeleteBuffers(vboID);
}
}

View file

@ -0,0 +1,62 @@
package mrdev023.rendering.gui;
import static org.lwjgl.opengl.GL11.*;
import mrdev023.rendering.*;
import org.newdawn.slick.*;
public class ButtonGUI extends GUI{
public boolean IsLoaded = false;
public float px,py,pwidth,pheight;
public ButtonGUI(String value,float x,float y,float width,float height){
super(value,x * DisplayManager.getWidth(), y * DisplayManager.getHeight(), width * DisplayManager.getWidth(), height * DisplayManager.getHeight(),24);
if(!IsLoaded){
int size = (int)((height * DisplayManager.getHeight())/2);
setFont(new TrueTypeFont (new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, size),false));
IsLoaded = true;
}
this.px = x;
this.py = y;
this.pwidth = width;
this.pheight = height;
}
public void render()
{
glPushMatrix();
glLoadIdentity();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if(mouseIsCollide() && IsHover){
font.drawString((int)(x + (width/2 - font.getWidth(value)/2)),(int)(y + font.getHeight()/2 - height/16),value,Color.green);
}else{
font.drawString((int)(x + (width/2 - font.getWidth(value)/2)),(int)(y + font.getHeight()/2 - height/16),value,Color.red);
}
glDisable(GL_BLEND);
if(mouseIsCollide() && IsHover)glColor4f(0,1,0,1);
else glColor4f(1,0,0,1);
glLineWidth(height/(height/2));
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUADS);
glVertex2f(x,y);
glVertex2f(x + width,y);
glVertex2f(x + width,y + height);
glVertex2f(x,y + height);
glEnd();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPopMatrix();
}
public void update() {
}
public void updateGUI() {
updateFrame(px * DisplayManager.getWidth(), py * DisplayManager.getHeight(), pwidth * DisplayManager.getWidth(), pheight * DisplayManager.getHeight(),(int)((pheight * DisplayManager.getHeight())/2));
}
}

View file

@ -0,0 +1,152 @@
package mrdev023.rendering.gui;
import mrdev023.math.*;
import mrdev023.rendering.gui.action.*;
import mrdev023.update.*;
import org.lwjgl.input.*;
import org.lwjgl.opengl.*;
import org.newdawn.slick.*;
import org.newdawn.slick.font.effects.*;
public abstract class GUI {
protected float x,y,width,height;
protected Color4f color;
protected String value;
public static final int NULL = 0,CLICKED = 1;
protected Action action = null;
protected boolean IsHover = true;
protected TrueTypeFont font = null;
public GUI(String value,float x,float y,float width,float height,int size){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = new Color4f(1f,1f,1f,1f);
this.value = value;
font = new TrueTypeFont (new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, size),false);
}
public abstract void render();
public abstract void update();
public void updateFrame(float x,float y,float width,float height,int size){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
font = null;
font = new TrueTypeFont (new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, size),false);
}
public abstract void updateGUI();
public void updateAction(){
if(this.action != null){
if(Update.mouseButtonPressed(0) && mouseIsCollide())action.manageAction(CLICKED);
if(mouseIsCollide())action.hover(new Vector2f(Mouse.getX(),(Display.getDisplayMode().getHeight() - Mouse.getY())));
}
}
public GUI setAction(Action a){
this.action = a;
return this;
}
public GUI setHoverAnimation(boolean a){
this.IsHover = a;
return this;
}
public boolean mouseIsCollide(){
if(Mouse.getX() > x && Mouse.getX() < (x+width)
&& (Display.getDisplayMode().getHeight() - Mouse.getY()) > y && (Display.getDisplayMode().getHeight() - Mouse.getY()) < (y+height))return true;
return false;
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public Color4f getColor() {
return color;
}
public void setColor(Color4f color) {
this.color = color;
}
public Action getAction() {
return action;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public boolean isIsHover() {
return IsHover;
}
public void setIsHover(boolean isHover) {
IsHover = isHover;
}
public TrueTypeFont getFont() {
return font;
}
public void setFont(TrueTypeFont font) {
this.font = font;
}
public static int getNull() {
return NULL;
}
public static int getClicked() {
return CLICKED;
}
}

View file

@ -0,0 +1,45 @@
package mrdev023.rendering.gui;
import static org.lwjgl.opengl.GL11.*;
import mrdev023.rendering.*;
import org.newdawn.slick.*;
public class LabelGUI extends GUI{
public boolean IsLoaded = false;
public float px,py,pwidth,pheight;
public LabelGUI(String value,float x, float y, float width, float height) {
super(value,x * DisplayManager.getWidth(), y * DisplayManager.getHeight(), width * DisplayManager.getWidth(), height * DisplayManager.getHeight(),24);
if(!IsLoaded){
int size = (int)((height * DisplayManager.getHeight())/2);
setFont(new TrueTypeFont (new java.awt.Font("Times New Roman", java.awt.Font.PLAIN, size),false));
IsLoaded = true;
}
this.px = x;
this.py = y;
this.pwidth = width;
this.pheight = height;
}
public void render() {
glPushMatrix();
glLoadIdentity();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
font.drawString((int)(x + (width/2 - font.getWidth(value)/2)),(int)(y + font.getHeight()/2 - height/16),value,Color.red);
glDisable(GL_BLEND);
glPopMatrix();
}
public void update() {
}
public void updateGUI() {
updateFrame(px * DisplayManager.getWidth(), py * DisplayManager.getHeight(), pwidth * DisplayManager.getWidth(), pheight * DisplayManager.getHeight(),(int)((pheight * DisplayManager.getHeight())/2));
}
}

View file

@ -0,0 +1,72 @@
package mrdev023.rendering.gui;
import java.awt.*;
import mrdev023.math.*;
import org.lwjgl.opengl.*;
public class Text {
public static int getWidth(String a){
return a.length() * 10;
}
public static int getHeight(){
return 8;
}
public static void drawString(String s, int x, int y,int size,Color4f color) {
int startX = x;
int a = 10;
GL11.glPushMatrix();
GL11.glColor4f(color.getR(), color.getG(), color.getB(), color.getA());
GL11.glPointSize(size);
GL11.glBegin(GL11.GL_POINTS);
char[] c = s.toCharArray();
for(char c1 : c){
switch(c1){
case 'a':
GL11.glVertex2f(x, y);
break;
}
}
GL11.glEnd();
GL11.glPopMatrix();
}
private static String chars = "abcdefghijklmnopqrstuvwxyz 0123456789:!?.,()";
public static void drawString(String msg, int x, int y, int size) {
msg = msg.toLowerCase();
// Texture.font.bind();
GL11.glBegin(GL11.GL_QUADS);
for (int i = 0; i < msg.length(); i++) {
int xi = chars.indexOf(msg.charAt(i));
int yi = 0;
if (xi >= 27) {
xi %= 27;
yi++;
}
if ((yi >= 0) && (xi >= 0)) {
quadData(x + i * size, y, size, size, xi, yi, 27.0F, 4.0F);
}
}
GL11.glEnd();
// Texture.font.unbind();
}
public static void quadData(int x, int y, int w, int h, int xo, int yo,
float xSize, float ySize) {
GL11.glTexCoord2f((0 + xo) / xSize, (0 + yo) / ySize);
GL11.glVertex2f(x, y);
GL11.glTexCoord2f((1 + xo) / xSize, (0 + yo) / ySize);
GL11.glVertex2f(x + w, y);
GL11.glTexCoord2f((1 + xo) / xSize, (1 + yo) / ySize);
GL11.glVertex2f(x + w, y + h);
GL11.glTexCoord2f((0 + xo) / xSize, (1 + yo) / ySize);
GL11.glVertex2f(x, y + h);
}
}

View file

@ -0,0 +1,9 @@
package mrdev023.rendering.gui.action;
import mrdev023.math.*;
public abstract class Action {
public abstract void manageAction(int action);
public abstract void hover(Vector2f position);
}

View file

@ -0,0 +1,84 @@
package mrdev023.update;
import java.util.*;
import org.lwjgl.input.*;
import mrdev023.gameengine.*;
public class Update {
private static ArrayList<Integer> mousePressed = new ArrayList<Integer>();
private static ArrayList<Integer> mouseReleased = new ArrayList<Integer>();
private static ArrayList<Integer> keyboardPressed = new ArrayList<Integer>();
private static ArrayList<Integer> keyboardReleased = new ArrayList<Integer>();
/**
* @Info Fonction permettant de gerer les action de la souris
*/
public static void updateMouse(){
mousePressed.clear();
mouseReleased.clear();
while(Mouse.next()){
if(Mouse.getEventButtonState()){
mousePressed.add(Mouse.getEventButton());
}else{
mouseReleased.add(Mouse.getEventButton());
}
}
GameEngine.getGameState().updateMouse();
}
public static boolean mouseButtonPressed(int button){
for(Integer m : mousePressed){
if(m == button)return true;
}
return false;
}
public static boolean mouseButtonReleased(int button){
for(Integer m : mouseReleased){
if(m == button)return true;
}
return false;
}
/**
* @Info Fonction permettant de gerer les action du clavier
*/
public static void updateKeyboard(){
keyboardPressed.clear();
keyboardReleased.clear();
while(Keyboard.next()){
if(Keyboard.getEventKeyState()){
keyboardPressed.add(Keyboard.getEventKey());
}else{
keyboardReleased.add(Keyboard.getEventKey());
}
}
GameEngine.getGameState().updateKeyboard();
}
public static boolean keyboardButtonPressed(int button){
for(Integer m : keyboardPressed){
if(m == button)return true;
}
return false;
}
public static boolean keyboardButtonReleased(int button){
for(Integer m : keyboardReleased){
if(m == button)return true;
}
return false;
}
/**
* @Info Fonction de mettre a jour le display et d'autre choses predefinie
*/
public static void update(){
GameEngine.getGameState().update();
}
}

View file

@ -0,0 +1,59 @@
package mrdev023.world;
import java.util.*;
import mrdev023.rendering.*;
import mrdev023.world.chunk.*;
public class MultiWorld extends World{
public MultiWorld(long seed, int octave, int amplitude) {
super(seed, octave, amplitude);
}
public void update() {
long current = System.currentTimeMillis();
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK;
int delta_x = xb - xa;
int delta_z = zb - za;
for(int i = 0; i <= delta_x;i++){
for(int k = 0;k <= delta_z;k++){
// for(int j = 0; j < HEIGHT; j++){
if(getChunk((xa + i), 0, (za + k)) != null)continue;
Chunk ch = new Chunk((xa + i),0,(za + k),this);
chunks.add(ch);
// }
}
}
ArrayList<Chunk> removeList = new ArrayList<Chunk>();
for(int i = 0; i < chunks.size();i++){
Chunk c = chunks.get(i);
if(c.getPosition().getX() < xa || c.getPosition().getX() > xb || c.getPosition().getZ() < za || c.getPosition().getZ() > zb){
c.destroyChunk();
removeList.add(c);
}else{
if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate() && !c.isDestroy())c.createChunk(this);
if(!c.isLoaded() && c.isGenerated() && !c.isDestroy())c.loadBufferData();
if(!c.isDestroy())c.update();
}
}
for(Chunk c: removeList){
removeByChunk(c);
}
removeList.clear();
updateWorldTime = System.currentTimeMillis() - current;
}
public void render() {
for(Chunk c : chunks){
if(c.isLoaded()){
c.render();
}
}
}
}

View file

@ -0,0 +1,80 @@
package mrdev023.world;
import java.io.*;
import java.util.*;
import mrdev023.io.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.world.chunk.*;
public class SoloWorld extends World{
public SoloWorld(long seed, int octave, int amplitude) {
super(seed, octave, amplitude);
}
public void update() {
long current = System.currentTimeMillis();
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - VIEW_CHUNK;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + VIEW_CHUNK;
int delta_x = xb - xa;
int delta_z = zb - za;
for(int i = 0; i <= delta_x;i++){
for(int k = 0;k <= delta_z;k++){
// for(int j = 0; j < HEIGHT; j++){
if(getChunk((xa + i), 0, (za + k)) != null)continue;
if(IO.chunkIsSaved(new Vector3i((xa + i),0,(za + k)),"soloWorld")){
try {
Chunk ch = IO.loadChunk(new Vector3i((xa + i),0,(za + k)),"soloWorld");
chunks.add(ch);
} catch (ClassNotFoundException e1) {
if(getChunk((xa + i), 0, (za + k)) != null)continue;
Chunk ch = new Chunk((xa + i),0,(za + k),this);
chunks.add(ch);
System.err.println(e1.getMessage());
} catch (IOException e1) {
if(getChunk((xa + i), 0, (za + k)) != null)continue;
Chunk ch = new Chunk((xa + i),0,(za + k),this);
chunks.add(ch);
e1.printStackTrace();
}
}else{
if(getChunk((xa + i), 0, (za + k)) != null)continue;
Chunk ch = new Chunk((xa + i),0,(za + k),this);
chunks.add(ch);
}
// }
}
}
ArrayList<Chunk> removeList = new ArrayList<Chunk>();
for(int i = 0; i < chunks.size();i++){
Chunk c = chunks.get(i);
if(c.getPosition().getX() < xa || c.getPosition().getX() > xb || c.getPosition().getZ() < za || c.getPosition().getZ() > zb){
c.destroyChunk();
removeList.add(c);
}else{
if(!c.isLoaded() && !c.isGenerated() && !c.isCurrentGenerate() && !c.isDestroy())c.createChunk(this);
if(!c.isLoaded() && c.isGenerated() && !c.isDestroy())c.loadBufferData();
if(!c.isDestroy())c.update();
}
}
for(Chunk c: removeList){
removeByChunk(c);
}
removeList.clear();
updateWorldTime = System.currentTimeMillis() - current;
}
public void render() {
for(Chunk c : chunks){
if(c.isLoaded()){
c.render();
}
}
}
}

View file

@ -0,0 +1,185 @@
package mrdev023.world;
import static org.lwjgl.opengl.GL11.*;
import java.util.*;
import mrdev023.blocks.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.world.chunk.*;
public abstract class World {
public long seed;
public int octave,amplitude;
public final int SIZE = 1,HEIGHT = 1;
public static final float GRAVITY = 1;
public static final int VIEW_CHUNK = 2;
public ArrayList<Chunk> chunks = new ArrayList<Chunk>();
public World(long seed,int octave,int amplitude){
this.seed= seed;
this.octave = octave;
this.amplitude = amplitude;
}
public static long updateWorldTime = 0;
public abstract void update();
public abstract void render();
public void renderPoints(Vector2f... a){
glPointSize(10);
glBegin(GL_POINTS);
for(Vector2f b : a){
glVertex3f(b.getX(),Camera.getPosition().getY() + Camera.getHeight(),b.getY());
}
glEnd();
}
public void removeByChunk(Chunk ch){
for(int i = 0;i < chunks.size();i++){
if(chunks.get(i).equals(ch)){
chunks.remove(i);
}
}
}
public Chunk getChunk(int xc, int yc, int zc) {
Chunk c = null;
Object[] chunk = chunks.toArray();
for(int i = 0;i < chunk.length;i++){
Chunk ch = (Chunk)chunk[i];
if(ch.getX() == xc && ch.getY() == yc && ch.getZ() == zc){
c = ch;
break;
}
}
return c;
}
public void addBlock(int x,int y,int z,Block b){
int xc = (x / Chunk.SIZE);
int zc = (z / Chunk.SIZE);
int yc = (y / Chunk.SIZE);
Chunk chunk = getChunk(xc, yc, zc);
if(chunk == null)return;
int xb = x % Chunk.SIZE;
int yb = y % Chunk.SIZE;
int zb = z % Chunk.SIZE;
chunk.addBlock(xb, yb, zb, b);
}
public Vector3f getLocalChunk(int x, int y, int z,boolean nx,boolean ny,boolean nz){
int xc = (x / Chunk.SIZE);
int zc = (z / Chunk.SIZE);
int yc = (y / Chunk.SIZE);
if(nx)xc -= 1;
if(ny)yc -= 1;
if(nz)zc -= 1;
return new Vector3f(xc,yc,zc);
}
public Block getBlock(int x, int y, int z,boolean nx,boolean ny,boolean nz) {
int xc = (x / Chunk.SIZE);
int zc = (z / Chunk.SIZE);
int yc = (y / Chunk.SIZE);
if(nx)xc -= 1;
if(ny)yc -= 1;
if(nz)zc -= 1;
Chunk chunk = getChunk(xc, yc, zc);
if(chunk == null)return null;
int xb = 0;
int yb = 0;
int zb = 0;
if( x <= 0 && nx){
xb = (Chunk.SIZE-1)-((-x) % Chunk.SIZE) + 1;
}else{
xb = x % Chunk.SIZE;
}
if( y <= 0 && ny){
yb = (Chunk.SIZE-1)-((-y) % Chunk.SIZE) + 1;
}else{
yb = y % Chunk.SIZE;
}
if( z <= 0 && nz){
zb = (Chunk.SIZE-1)-((-z) % Chunk.SIZE) + 1;
}else{
zb = z % Chunk.SIZE;
}
return chunk.getBlock(xb, yb, zb);
}
public long getSeed() {
return seed;
}
public void setSeed(long seed) {
this.seed = seed;
}
public ArrayList<Chunk> getChunks() {
return chunks;
}
public void setChunks(ArrayList<Chunk> chunks) {
this.chunks = chunks;
}
public static long getUpdateWorldTime() {
return updateWorldTime;
}
public static void setUpdateWorldTime(long updateWorldTime) {
World.updateWorldTime = updateWorldTime;
}
public int getSIZE() {
return SIZE;
}
public int getHEIGHT() {
return HEIGHT;
}
public static float getGravity() {
return GRAVITY;
}
public static int getViewChunk() {
return VIEW_CHUNK;
}
public static void printBlockArray(Block[][][] blocks){
for(int x = 0; x < blocks.length;x++){
for(int y = 0; y < blocks[x].length;y++){
for(int z = 0; z < blocks[x][y].length;z++){
System.out.println(blocks[x][y][z]);
}
}
}
}
public void destroyWorld(){
for(Chunk c : chunks){
c.destroyChunk();
c = null;
}
chunks.clear();
}
}

View file

@ -0,0 +1,326 @@
package mrdev023.world.chunk;
import java.io.*;
import mrdev023.blocks.*;
import mrdev023.gameengine.*;
import mrdev023.io.*;
import mrdev023.main.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.world.*;
public class Chunk{
public final static int SIZE = 64;
private int x, y, z;
private VBO vbo;
private World world;
Block[][][] blocks;
private boolean IsLoad = false;
private boolean IsGenerated = false;
private boolean IsCurrentGenerate = false;
private boolean IsDestroy = false;
public Chunk(int x, int y, int z, World world) {
this.x = x;
this.y = y;
this.z = z;
this.world = world;
this.blocks = new Block[SIZE][SIZE][SIZE];
vbo = new VBO();
}
public Chunk(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
this.world = null;
this.blocks = new Block[SIZE][SIZE][SIZE];
vbo = new VBO();
}
public void render() {
vbo.renderVBO();
}
public void update() {
}
public void setBlocks(Block[][][] blocks){
this.blocks = blocks;
vbo.clearBuffer();
Main.addThread(new LoadChunk(this, world),"Load Chunk");
IsCurrentGenerate = true;
}
public Block[][][] getBlocks(){
return blocks;
}
public void createChunk(World world) {
this.world = world;
Main.addThread(new Generate(this, world),"Create Chunk");
IsCurrentGenerate = true;
}
public void loadBufferData() {
vbo.bufferData();
IsLoad = true;
}
public void updateChunk() {
vbo.clearBuffer();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int k = 0; k < SIZE; k++) {
loopChunk(i, j, k);
}
}
}
vbo.bufferData();
}
public int getSIZE() {
return SIZE;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public Vector3i getPosition() {
return new Vector3i((int)x, (int)y, (int)z);
}
public Block getBlock(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0 || x >= SIZE || y >= SIZE || z >= SIZE)
return null;
return blocks[x][y][z];
}
public void addBlock(int x, int y, int z,Block b) {
if (x < 0 || y < 0 || z < 0 || x >= SIZE || y >= SIZE || z >= SIZE)
return;
blocks[x][y][z] = b;
}
public void destroyChunk() {
vbo.destroyVBO();
IsDestroy = false;
}
public void loopChunk(int x, int y, int z) {
if(GameEngine.getGameState().getWorld() != null)world = GameEngine.getGameState().getWorld();
int xx = this.x * SIZE + x;
int yy = this.y * SIZE + y;
int zz = this.z * SIZE + z;
boolean up = true;
boolean down = true;
boolean left = true;
boolean right = true;
boolean back = true;
boolean front = true;
boolean nx = false, ny = false, nz = false;
if(this.x < 0)nx = true;
if(this.y < 0)ny = true;
if(this.z < 0)nz = true;
up = world.getBlock(xx, yy + 1, zz,nx,ny,nz) == null;
down = world.getBlock(xx, yy - 1, zz,nx,ny,nz) == null;
left = world.getBlock(xx - 1, yy, zz,nx,ny,nz) == null;
right = world.getBlock(xx + 1, yy, zz,nx,ny,nz) == null;
front = world.getBlock(xx, yy, zz - 1,nx,ny,nz) == null;
back = world.getBlock(xx, yy, zz + 1,nx,ny,nz) == null;
if (!up && !down && !left && !right && !front && !back)
return;
if (blocks[x][y][z] == null)
return;
Block b = blocks[x][y][z];
float ao = 0.7f;
// up + 1 = down - 1 = y
// left - 1 = right + 1 = x
// front - 1 = back + 1 = z
if (up) {
// aa ab bb ba
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx - 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx, yy + 1, zz - 1,nx,ny,nz) != null)
a[0] = ao;
if (world.getBlock(xx + 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx, yy + 1, zz - 1,nx,ny,nz) != null)
a[1] = ao;
if (world.getBlock(xx + 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx, yy + 1, zz + 1,nx,ny,nz) != null)
a[2] = ao;
if (world.getBlock(xx - 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx, yy + 1, zz + 1,nx,ny,nz) != null)
a[3] = ao;
vbo.addDataByFloatArray(b.getDataUp(xx, yy, zz, a));
}
if (down) {
float[] a = new float[] { 1, 1, 1, 1 };
// ambient occlusion
if (world.getBlock(xx - 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx, yy - 1, zz - 1,nx,ny,nz) != null)
a[1] = ao;
if (world.getBlock(xx + 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx, yy - 1, zz - 1,nx,ny,nz) != null)
a[0] = ao;
if (world.getBlock(xx + 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx, yy - 1, zz + 1,nx,ny,nz) != null)
a[3] = ao;
if (world.getBlock(xx - 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx, yy - 1, zz + 1,nx,ny,nz) != null)
a[2] = ao;
// affiche la face si il n'y a rien en dessous
vbo.addDataByFloatArray(b.getDataDown(xx, yy, zz, a));
}
if (left) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx - 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz - 1,nx,ny,nz) != null)
a[0] = ao;
if (world.getBlock(xx - 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz - 1,nx,ny,nz) != null)
a[1] = ao;
if (world.getBlock(xx - 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz + 1,nx,ny,nz) != null)
a[2] = ao;
if (world.getBlock(xx - 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz + 1,nx,ny,nz) != null)
a[3] = ao;
vbo.addDataByFloatArray(b.getDataLeft(xx, yy, zz, a));
}
if (right) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx + 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz - 1,nx,ny,nz) != null)
a[1] = ao;
if (world.getBlock(xx + 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz - 1,nx,ny,nz) != null)
a[0] = ao;
if (world.getBlock(xx + 1, yy + 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz + 1,nx,ny,nz) != null)
a[3] = ao;
if (world.getBlock(xx + 1, yy - 1, zz,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz + 1,nx,ny,nz) != null)
a[2] = ao;
vbo.addDataByFloatArray(b.getDataRight(xx, yy, zz, a));
}
if (front) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz - 1,nx,ny,nz) != null)
a[0] = ao;
if (world.getBlock(xx, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz - 1,nx,ny,nz) != null)
a[3] = ao;
if (world.getBlock(xx, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy + 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz - 1,nx,ny,nz) != null)
a[2] = ao;
if (world.getBlock(xx, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy - 1, zz - 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz - 1,nx,ny,nz) != null)
a[1] = ao;
vbo.addDataByFloatArray(b.getDataFront(xx, yy, zz, a));
}
if (back) {
float[] a = new float[] { 1, 1, 1, 1 };
if (world.getBlock(xx, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz + 1,nx,ny,nz) != null)
a[1] = ao;
if (world.getBlock(xx, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx - 1, yy, zz + 1,nx,ny,nz) != null)
a[2] = ao;
if (world.getBlock(xx, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy + 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz + 1,nx,ny,nz) != null)
a[3] = ao;
if (world.getBlock(xx, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy - 1, zz + 1,nx,ny,nz) != null
|| world.getBlock(xx + 1, yy, zz + 1,nx,ny,nz) != null)
a[0] = ao;
vbo.addDataByFloatArray(b.getDataBack(xx, yy, zz, a));
}
}
public boolean isLoaded() {
return IsLoad;
}
public boolean isGenerated() {
return IsGenerated;
}
public void setGenerated(boolean g,boolean IsGenerate) {
IsGenerated = g;
if(g && IsGenerate){
try {
IO.saveChunk(this,"soloWorld");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String toString(){
return x + " " + y + " " + z;
}
public boolean isCurrentGenerate(){
return IsCurrentGenerate;
}
public boolean isDestroy() {
return IsDestroy;
}
}

View file

@ -0,0 +1,101 @@
package mrdev023.world.chunk;
import java.util.*;
import mrdev023.blocks.*;
import mrdev023.math.*;
import mrdev023.rendering.*;
import mrdev023.world.*;
import mrdev023.world.chunk.*;
class Generate implements Runnable {
private Chunk chunk;
private World world;
public Generate(Chunk chunk, World world) {
this.chunk = chunk;
this.world = world;
}
/*
* boolean grounded = noise.getNoise(xx, zz) > yy - 1 && noise.getNoise(xx, zz) < yy;
float percentOfSpawnTree = 0.005f;
if(random.nextFloat() < percentOfSpawnTree && grounded){
if(random.nextInt(2) == 0)
Tree.addOak(world, xx, yy, zz);
else
Tree.addFir(world, xx, yy, zz);
}(non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
long current = System.currentTimeMillis();
long elapsed1 = 0;
boolean IsError = true;
Noise noise = new Noise(world.seed, world.octave, world.amplitude);
Random random = new Random(world.seed);
Noise colorVariationNoise = new Noise(world.seed,world.octave,2);
for (int x = 0; x < chunk.SIZE; x++) {
for (int z = 0; z < chunk.SIZE; z++) {
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
if(chunk.getX() < xa || chunk.getX() > xb || chunk.getZ() < za || chunk.getZ() > zb ){
System.out.println(Thread.currentThread().getName() + " stopped | " + (System.currentTimeMillis()-current));
Thread.currentThread().stop();
return;
}
int xx = chunk.getX() * chunk.SIZE + x;
int zz = chunk.getZ() * chunk.SIZE + z;
Color4f green = new Color4f(0.2f,0.6f,0);
Color4f yellow = new Color4f(0.36f,0.77f,0.17f);
float v = colorVariationNoise.getNoise(xx, zz);
Color4f finalColor = Color4f.interpolate(yellow, green, v);
chunk.blocks[x][(int)noise.getNoise(xx, zz)][z] = new GrassBlock().setColor(finalColor);
}
}
elapsed1 = System.currentTimeMillis() - current;
while(IsError){
IsError = false;
try{
// synchronized (world.chunks) {
for (int i = 0; i < chunk.SIZE; i++) {
// for (int j = 0; j < chunk.SIZE; j++) {
for (int k = 0; k < chunk.SIZE; k++) {
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
if(chunk.getX() < xa || chunk.getX() > xb || chunk.getZ() < za || chunk.getZ() > zb ){
System.out.println(Thread.currentThread().getName() + " stopped | " + (System.currentTimeMillis()-current));
Thread.currentThread().stop();
return;
}
int xx = chunk.getX() * chunk.SIZE + i;
int zz = chunk.getZ() * chunk.SIZE + k;
chunk.loopChunk(i, (int)noise.getNoise(xx, zz), k);
}
// }
}
// }
chunk.setGenerated(true,true);
}catch (Exception e){
e.printStackTrace();
IsError = true;
}
}
System.out.println(Thread.currentThread().getName() + " terminated | loop1:" + elapsed1 + "ms loop2:" + (System.currentTimeMillis()-current) + "ms");
Thread.currentThread().stop();
}
}

View file

@ -0,0 +1,53 @@
package mrdev023.world.chunk;
import mrdev023.rendering.*;
import mrdev023.world.*;
public class LoadChunk implements Runnable{
private Chunk chunk;
private World world;
public LoadChunk(Chunk chunk, World world) {
this.chunk = chunk;
this.world = world;
}
public void run(){
long current = System.currentTimeMillis();
boolean IsError = true;
while(IsError){
IsError = false;
try{
// synchronized (world.chunks) {
for (int i = 0; i < chunk.SIZE; i++) {
for (int j = 0; j < chunk.SIZE; j++) {
for (int k = 0; k < chunk.SIZE; k++) {
int xa = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int xb = (int)((Camera.getPosition().getX()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
int za = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) - World.VIEW_CHUNK;
int zb = (int)((Camera.getPosition().getZ()-((float)Chunk.SIZE/2.0f))/(float)Chunk.SIZE) + World.VIEW_CHUNK;
if(chunk.getX() < xa || chunk.getX() > xb || chunk.getZ() < za || chunk.getZ() > zb ){
System.out.println(Thread.currentThread().getName() + " stopped | " + (System.currentTimeMillis()-current));
Thread.currentThread().stop();
return;
}
int xx = chunk.getX() * chunk.SIZE + i;
int zz = chunk.getZ() * chunk.SIZE + k;
chunk.loopChunk(i, j, k);
}
}
}
// }
chunk.setGenerated(true,false);
}catch (Exception e){
e.printStackTrace();
IsError = true;
}
}
System.out.println(Thread.currentThread().getName() + " terminated | loop1:" + (System.currentTimeMillis()-current) + "ms");
Thread.currentThread().stop();
}
}

View file

@ -0,0 +1,75 @@
package mrdev023.world.chunk;
import java.util.*;
import mrdev023.math.*;
public class Noise {
private long seed;
private Random rand;
private int octave;
private float amplitude;
public Noise(long seed,int octave, float amplitude){
this.seed = seed;
this.octave = octave;
this.amplitude = amplitude;
rand = new Random();
}
public float getNoise(float x,float z){
int xmin = 0,xmax = 0,zmin = 0,zmax = 0;
if(x<0){
xmin = (int)(double)x/octave;
xmax = (int) xmin - 1;
}else{
xmin = (int)(double)x/octave;
xmax = (int) xmin + 1;
}
if(z<0){
zmin = (int)(double)z/octave;
zmax = (int) zmin - 1;
}else{
zmin = (int)(double)z/octave;
zmax = (int) zmin + 1;
}
Vector2f a = new Vector2f(xmin,zmin);
Vector2f b = new Vector2f(xmax,zmin);
Vector2f c = new Vector2f(xmax,zmax);
Vector2f d = new Vector2f(xmin,zmax);
float ra = (float)noise(a);
float rb = (float)noise(b);
float rc = (float)noise(c);
float rd = (float)noise(d);
float t = (x - xmin * octave) / octave;
float i1 = interpolate(ra,rb,t);
float i2 = interpolate(rd,rc,t);
float h = interpolate(i1,i2,(z - zmin * octave) / octave);
return h * amplitude;
}
private float interpolate(float a, float b, float t){
float ft = (float) (t * Math.PI);
float f = (float )((1f - Math.cos(ft)) * 0.5f);
float ret = a * ( 1f - f) + b * f;
return ret;
}
private double noise(Vector2f coord){
double var = 10000 * (Math.sin(coord.getX() + Math.cos(coord.getY())) + Math.tan(seed));
rand.setSeed((long)var);
double ret = rand.nextDouble();
return ret;
}
}

View file

@ -0,0 +1,57 @@
package mrdev023.world.trees;
import mrdev023.blocks.*;
import mrdev023.world.*;
public class Tree {
public static void addOak(World world, int x, int y, int z) {
int treeHeight = 9;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
float ii = i - 4.5f;
float jj = j - 4.5f;
float kk = k - 4.5f;
float l = (float) Math.sqrt(ii * ii + jj * jj + kk * kk);
if (l < 4.5f) {
world.addBlock(x + (int) ii, y + (int) jj + treeHeight,
z + (int) kk, Block.LEAF);
}
}
}
}
for (int i = 0; i < treeHeight; i++) {
world.addBlock(x, y + i, z, Block.OAK_WOOD);
}
}
public static void addFir(World world, int x, int y, int z) {
int treeHeight = 13;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 13; j++) {
for (int k = 0; k < 9; k++) {
float ii = i - 4f;
float jj = j - 3f;
float kk = k - 4f;
float l = (float) Math.sqrt(ii * ii + kk * kk);
float size = 1;
size -= (float)j /13.0f;
if(l < 4.5f * size){
world.addBlock(x + (int) ii, y + (int) jj + 7, z
+ (int) kk, Block.FIR_LEAF);
}
}
}
}
for (int i = 0; i < treeHeight; i++) {
world.addBlock(x, y + i, z, Block.FIR_WOOD);
}
}
}