1
0
Fork 0
This commit is contained in:
Lucas 2017-01-21 01:53:51 +01:00
commit 58e0435cb8
11 changed files with 461 additions and 404 deletions

View file

@ -1,6 +1,7 @@
package globalgamejam.game;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
@ -18,121 +19,52 @@ import globalgamejam.tiles.TestTile;
import globalgamejam.tiles.Tile;
import globalgamejam.interfaces.MainInterfaces;
import globalgamejam.world.MainWorld;
import java.util.Random;
import org.lwjgl.glfw.GLFW;
/**
* Class created by MrDev023 (Florian RICHER) on 14/01/2017
*/
public class MainGame extends Game{
private ArrayList<Tile> tiles;
private Random rand;
private Player player1;
private ArrayList<GUI> guis;
private GUILabel label;
private MainWorld world;
private MainInterfaces interfaces;
public int[] scores;
@Override
public void init() {
tiles = new ArrayList<Tile>();
Fond fond = new Fond("res/textures/fond.png");
fond.getTransform().translate(Main.WIDTH/2, Main.HEIGHT/2, 0);
fond.getTransform().scale(Main.WIDTH,Main.HEIGHT, 0);
fond.getTransform().rotate(180, 0, 0);
guis = new ArrayList<GUI>();
tiles.add(fond);
TestTile test = new TestTile();
test.getTransform().translate(0, 80, 0);
test.getTransform().scale(10, 10, 0);
tiles.add(test);
player1 = new Player(-100, 0);
tiles.add(player1.getTile());
rand = new Random();
label = new GUILabel("Test");
label.setX(10);
label.setY(10);
label.setAction(new ActionGUI() {
@Override
public void enter(float mouseX, float mouseY) {
label.setColor(Color.RED);
}
@Override
public void leave(float mouseX, float mouseY) {
label.setColor(Color.WHITE);
}
});
guis.add(label);
generateEntity(3);
this.scores = new int[2];
world = new MainWorld(this);
interfaces = new MainInterfaces(this);
}
@Override
public void update() {
Camera.transform();
// player1.setPosition((rand.nextFloat() - 0.5f) * 200f, (rand.nextFloat() - 0.5f) * 150f);
// player1.applyTransform();
for(GUI g : guis)g.update();
interfaces.update();
world.update();
}
@Override
public void render2D() {
for(int i = tiles.size() - 1;i >= 0 ;i--)
tiles.get(i).render();
world.render();
}
@Override
public void renderGUI() {
for(GUI g : guis)g.render();
interfaces.render();
}
@Override
public void destroy() {
tiles.clear();
guis.clear();
interfaces.destroy();
world.destroy();
}
public void generateEntity(int nb){
final int MIN_HAUTEUR_MAX=150;
final int MIN_HAUTEUR=80;
int hauteurMax = (int) (MIN_HAUTEUR_MAX +Math.random()* Main.HEIGHT-80);
int nbMin = 0;
int nbMax = 0;
ArrayList<Tile> list = new ArrayList<>();
if(hauteurMax<MIN_HAUTEUR_MAX){
nbMin=nb-2;
nbMax=0;
}
if(hauteurMax>MIN_HAUTEUR_MAX && hauteurMax<Main.HEIGHT/2){
nbMin=nb-2;
nbMax=nb+2;
}
if(hauteurMax>Main.HEIGHT/2){
nbMin=0;
nbMax=nb+2;
}
int countJ1=(int)(nbMin + Math.random()*nbMax);
int countJ2=(int)(nbMin + Math.random()*nbMax);
for(int i =0;i<countJ1;i++){
list.add(new Objet((int)(Math.random()* Main.WIDTH/2),(int) (MIN_HAUTEUR+Math.random()* hauteurMax)));
}
for(int i =0;i<countJ1;i++){
list.add(new Objet((int)(Main.WIDTH/2+Math.random()* Main.WIDTH),(int) (MIN_HAUTEUR +Math.random()* hauteurMax)));
}
for(Tile t : list){
tiles.add(t);
}
}
}

View file

@ -20,7 +20,7 @@ public class Player extends PhysicalEntity {
public Player(float x, float y){
super(x, y, 100, 0, 0, 10);
this.tile = new PlayerTile("res/textures/default.png", -250, 0);
this.tile = new PlayerTile("res/textures/perso.png", x, y);
this.longueurBalai = 100;
this.brosse = new PhysicalEntity(x, y + this.longueurBalai, 20, 0, 0, 0);
@ -30,6 +30,14 @@ public class Player extends PhysicalEntity {
return this.tile;
}
public void move(float x, float y){
this.addPosition(x, y);
this.tile.setPosition(new Vector2f(this.x, this.y));
this.tile.applyTransform();
this.brosse.addPosition(x, y);
}
public void rotate(float angleRotation){
this.angle += angleRotation;
this.angle %= 360;
@ -38,6 +46,7 @@ public class Player extends PhysicalEntity {
}
this.tile.setRotation(this.angle);
this.tile.applyTransform();
float angleRad = (float)(this.angle * (Math.PI / 180));
@ -51,6 +60,11 @@ public class Player extends PhysicalEntity {
return this.brosse.collideWithSquareHitBox(entity);
}
@Override
public String toString(){
return this.brosse.toString();
}
private class PlayerTile extends Tile {
public PlayerTile(String texturePath, float x, float y){
@ -58,16 +72,11 @@ public class Player extends PhysicalEntity {
this.setTexture(Texture.loadTexture(texturePath));
this.setPosition(x, y);
this.setPosition(new Vector2f(x, y));
this.setScale(new Vector2f(50, 50));
this.setScale(new Vector2f(this.getTexture().width, this.getTexture().height));
this.applyTransform();
}
public void setPosition(float x, float y){
this.setPosition(new Vector2f(x, y));
}
}
}

View file

@ -18,6 +18,7 @@ public abstract class GUI {
this.y = y;
this.width = 0;
this.height = 0;
this.action = new ActionGUI();
}
public void setAction(IActionGUI action){

View file

@ -54,9 +54,9 @@ public class GUILabel extends GUI {
this.vbo = GL15.glGenBuffers();
float[] a = new float[]{
0,0, 0.0f,0.0f,
this.texture.width,0, 1.0f,0.0f,
this.texture.width,this.texture.height, 1.0f,1.0f,
0,this.texture.height, 0.0f,1.0f
1,0, 1.0f,0.0f,
1,1, 1.0f,1.0f,
0,1, 0.0f,1.0f
};
FloatBuffer buff = BufferUtils.createFloatBuffer(a.length);
buff.put(a).flip();
@ -72,6 +72,7 @@ public class GUILabel extends GUI {
Shaders.MAIN_SHADERS.uniform("camera", Camera.matrix);
Matrix4f transform = new Matrix4f();
transform.translate(super.x,super.y,0);
transform.scale(this.getWitdh(),this.getHeight(),1);
Shaders.MAIN_SHADERS.uniform("transform", transform);
Shaders.MAIN_SHADERS.uniform("projection", DisplayManager.projection);
Shaders.MAIN_SHADERS.uniform("color", Color4f.WHITE);
@ -142,8 +143,6 @@ public class GUILabel extends GUI {
this.texture = Texture.loadFont(text,color,font,size);
}
/**
* Return the x coordonnate of the Label (upper left corner)
* @return x (float) : the x coordonnate of the Label
@ -191,7 +190,7 @@ public class GUILabel extends GUI {
* @return witdh (int) : the width
*/
public int getWitdh() {
return super.width;
return this.texture.width;
}
/**
@ -199,7 +198,7 @@ public class GUILabel extends GUI {
* @return height (int) : the height
*/
public int getHeight() {
return super.height;
return this.texture.height;
}
public void destroy(){

View file

@ -0,0 +1,53 @@
package globalgamejam.interfaces;
import globalgamejam.Main;
import globalgamejam.game.MainGame;
import globalgamejam.gui.GUI;
import globalgamejam.gui.GUILabel;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by trexr on 20/01/2017.
*/
public class MainInterfaces {
private final int SIZE_OF_DETAILS = 100;
private MainGame game;
private ArrayList<GUI> guis;
private GUILabel p1,p2;
public MainInterfaces(MainGame game){
this.game = game;
guis = new ArrayList<GUI>();
init();
}
public void init(){
p1 = new GUILabel("Player 1 : ", Main.WIDTH/4 - 50,10, Color.WHITE,"Arial",16);
p2 = new GUILabel("Player 2 : ", Main.WIDTH/4 * 3 - 50,10, Color.WHITE,"Arial",16);
guis.add(p1);
guis.add(p2);
}
public void update(){
p1.setText("Player 1 : " + this.game.scores[0]);
p1.setX((Main.WIDTH-SIZE_OF_DETAILS)/4 - p1.getWitdh()/2);
p2.setText("Player 2 : " + this.game.scores[1]);
p2.setX((Main.WIDTH-SIZE_OF_DETAILS)/4*3 - p2.getWitdh()/2);
for(GUI g : guis)g.update();
}
public void render(){
for(GUI g : guis)g.render();
}
public void destroy(){
guis.clear();
}
}

View file

@ -67,4 +67,14 @@ public class PhysicalEntity {
this.x = x;
this.y = y;
}
public void addPosition(float x, float y){
this.x += x;
this.y += y;
}
@Override
public String toString(){
return this.x + " " + this.y;
}
}

View file

@ -82,7 +82,6 @@ public class Texture {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
System.out.println("Texture loaded ! " + width + "x" + height + " id:" + textureID);
return new Texture(image.getWidth(),image.getHeight(),textureID);
}
@ -122,6 +121,8 @@ public class Texture {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
glBindTexture(GL_TEXTURE_2D, 0);
System.out.println("Texture loaded ! " + width + "x" + height + " id:" + id);
return new Texture(width, height, id);
} catch (IOException e) {

View file

@ -33,10 +33,10 @@ public abstract class Tile {
this.color = Color4f.WHITE;
this.vbo = GL15.glGenBuffers();
float[] a = new float[]{
-.5f,-.5f, 0.0f,0.0f,
.5f,-.5f, 1.0f,0.0f,
.5f,.5f, 1.0f,1.0f,
-.5f,.5f, 0.0f,1.0f
-.5f,-.5f, 0.0f,1.0f,
.5f,-.5f, 1.0f,1.0f,
.5f,.5f, 1.0f,0.0f,
-.5f,.5f, 0.0f,0.0f
};
FloatBuffer buffer = BufferUtils.createFloatBuffer(a.length);
buffer.put(a).flip();

View file

@ -0,0 +1,137 @@
package globalgamejam.world;
import globalgamejam.Main;
import globalgamejam.game.MainGame;
import globalgamejam.game.Player;
import globalgamejam.gui.ActionGUI;
import globalgamejam.gui.GUI;
import globalgamejam.gui.GUILabel;
import globalgamejam.input.Input;
import globalgamejam.tiles.Fond;
import globalgamejam.tiles.Objet;
import globalgamejam.tiles.TestTile;
import globalgamejam.tiles.Tile;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
import org.lwjgl.glfw.GLFW;
/**
* Created by trexr on 20/01/2017.
*/
public class MainWorld {
private ArrayList<Tile> tiles;
private MainGame game;
private Player player1;
public MainWorld(MainGame game){
this.game = game;
tiles = new ArrayList<Tile>();
init();
}
public void init(){
player1 = new Player(200, 150);
tiles.add(player1.getTile());
tiles = new ArrayList<Tile>();
Fond fond = new Fond("res/textures/fond.png");
fond.getTransform().translate(Main.WIDTH/2, Main.HEIGHT/2, 0);
fond.getTransform().scale(Main.WIDTH,Main.HEIGHT, 0);
tiles.add(fond);
TestTile test = new TestTile();
test.getTransform().translate(0, 80, 0);
test.getTransform().scale(10, 10, 0);
tiles.add(test);
player1 = new Player(100, 0);
tiles.add(player1.getTile());
generateEntity(3);
}
public void update(){
float xDep = 0, yDep = 0;
if(Input.isKey(GLFW.GLFW_KEY_W)){
yDep = 10;
}
if(Input.isKey(GLFW.GLFW_KEY_S)){
yDep = -10;
}
if(Input.isKey(GLFW.GLFW_KEY_A)){
xDep = -10;
}
if(Input.isKey(GLFW.GLFW_KEY_D)){
xDep = 10;
}
if(xDep != 0.0 && yDep != 0.0){
xDep *= Math.cos(Math.PI / 4);
yDep *= Math.cos(Math.PI / 4);
}
player1.move(xDep, yDep);
if(Input.isKey(GLFW.GLFW_KEY_SPACE)){
player1.rotate(-5);
}
if(Input.isKey(GLFW.GLFW_KEY_LEFT_ALT)){
player1.rotate(5);
}
System.out.println(player1);
}
public void render(){
for(int i = tiles.size() - 1;i >= 0 ;i--)
tiles.get(i).render();
}
public void destroy(){
tiles.clear();
}
public void generateEntity(int nb){
final int MIN_HAUTEUR_MAX=150;
final int MIN_HAUTEUR=80;
int hauteurMax = (int) (MIN_HAUTEUR_MAX +Math.random()* Main.HEIGHT-80);
int nbMin = 0;
int nbMax = 0;
ArrayList<Tile> list = new ArrayList<>();
if(hauteurMax<MIN_HAUTEUR_MAX){
nbMin=nb-2;
nbMax=0;
}
if(hauteurMax>MIN_HAUTEUR_MAX && hauteurMax<Main.HEIGHT/2){
nbMin=nb-2;
nbMax=nb+2;
}
if(hauteurMax>Main.HEIGHT/2){
nbMin=0;
nbMax=nb+2;
}
int countJ1=(int)(nbMin + Math.random()*nbMax);
int countJ2=(int)(nbMin + Math.random()*nbMax);
for(int i =0;i<countJ1;i++){
list.add(new Objet((int)(Math.random()* Main.WIDTH/2),(int) (MIN_HAUTEUR+Math.random()* hauteurMax)));
}
for(int i =0;i<countJ2;i++){
list.add(new Objet((int)(Main.WIDTH/2+Math.random()* Main.WIDTH),(int) (MIN_HAUTEUR +Math.random()* hauteurMax)));
}
for(Tile t : list){
tiles.add(t);
}
}
}