Adding TestUDPGame to git
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
apply plugin: "java"
|
||||
|
||||
sourceCompatibility = 1.6
|
||||
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
|
||||
|
||||
sourceSets.main.java.srcDirs = [ "src/" ]
|
||||
|
||||
|
||||
eclipse.project {
|
||||
name = appName + "-core"
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.jwinslow.game;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.jwinslow.game.gameobjects.GameObject;
|
||||
import com.jwinslow.game.gameobjects.GameObjectHandler;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
import com.jwinslow.game.net.Connection;
|
||||
import com.jwinslow.game.states.*;
|
||||
import com.jwinslow.game.ui.UIDebug;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
import com.jwinslow.game.utils.CameraHandler;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
import com.jwinslow.game.utils.Fonts;
|
||||
import com.jwinslow.game.utils.TextInputListener;
|
||||
|
||||
public class Handler {
|
||||
|
||||
//--- Propreties
|
||||
private float elapsedTime;
|
||||
|
||||
public UIDebug debugger;
|
||||
private TextInputListener til;
|
||||
private CameraHandler camHandler;
|
||||
private GameObjectHandler gameObjectHandler;
|
||||
private State initState;
|
||||
private State gameState;
|
||||
private State menuState;
|
||||
private State pauseState;
|
||||
private State connectState;
|
||||
private Main main;
|
||||
|
||||
private BitmapFont font;
|
||||
public String message;
|
||||
|
||||
//--- Constructor
|
||||
public Handler(Main main) {
|
||||
this.main = main;
|
||||
font = new BitmapFont(Gdx.files.internal("customPixelFont1.fnt"));
|
||||
font.setColor(Color.RED);
|
||||
font.getData().scale(0.01f);
|
||||
message = "";
|
||||
elapsedTime = 0;
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public void init() {
|
||||
debugger = new UIDebug(this);
|
||||
getStage().addActor(debugger);
|
||||
debugger.msg = "Debugger:";
|
||||
|
||||
Connection.init(this);
|
||||
til = new TextInputListener(this);
|
||||
|
||||
camHandler = new CameraHandler(this, main.getCam());
|
||||
gameObjectHandler = new GameObjectHandler(this);
|
||||
initState = new InitState(this);
|
||||
gameState = new GameState(this);
|
||||
menuState = new MenuState(this);
|
||||
pauseState = new PauseState(this);
|
||||
connectState = new ConnectState(this, -1);
|
||||
State.setCurrentState(initState);
|
||||
State.getCurrentState().init();
|
||||
Controls.initControls();
|
||||
Controls.setControllerType(Controls.TOUCH);
|
||||
ButtonEffects.init(this);
|
||||
Fonts.init(this);
|
||||
}
|
||||
|
||||
public void update(float dt) {
|
||||
Connection.update(dt);
|
||||
elapsedTime += dt;
|
||||
//message = "" + Gdx.graphics.getWidth();//(int) elapsedTime;
|
||||
|
||||
debugger.update(dt);
|
||||
Controls.update();
|
||||
camHandler.update(dt);
|
||||
if (State.getCurrentState() != null) State.getCurrentState().update(dt);
|
||||
//main.stage.act(dt);
|
||||
}
|
||||
|
||||
public void render(SpriteBatch batch) {
|
||||
if (State.getCurrentState() != null) State.getCurrentState().render(batch);
|
||||
font.draw(batch, message, getCam().position.x-main.cam.viewportWidth/2+2, getCam().position.y-main.cam.viewportHeight/2+15);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
gameObjectHandler.dispose();
|
||||
System.out.println("Disposing of textures.");
|
||||
initState.dispose();
|
||||
gameState.dispose();
|
||||
menuState.dispose();
|
||||
pauseState.dispose();
|
||||
connectState.dispose();
|
||||
Assets.disposeAllTextures();
|
||||
font.dispose();
|
||||
Fonts.dispose();
|
||||
debugger.dispose();
|
||||
Connection.dispose();
|
||||
}
|
||||
|
||||
public void addGameObject(GameObject o) {
|
||||
gameObjectHandler.add(o);
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public Main getMain() {return main;}
|
||||
public InitState getInitState() {return (InitState)initState;}
|
||||
public GameState getGameState() {return (GameState) gameState;}
|
||||
public MenuState getMenuState() {return (MenuState) menuState;}
|
||||
public PauseState getPauseState() {return (PauseState) pauseState;}
|
||||
public ConnectState getConnectState() {return (ConnectState) connectState;}
|
||||
public OrthographicCamera getCam() {return main.getCam();}
|
||||
public CameraHandler getCamHandler() {return camHandler;}
|
||||
public GameObjectHandler getGameObjectHandler() {return gameObjectHandler;}
|
||||
public Array<Player> getPlayers() {return gameObjectHandler.getPlayers();}
|
||||
public Stage getStage() {return main.stage;}
|
||||
public TextInputListener getTil() {return til;}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.jwinslow.game;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
||||
|
||||
public class Main extends ApplicationAdapter {
|
||||
|
||||
public static final int WIDTH = 320, HEIGHT = 480;
|
||||
|
||||
SpriteBatch batch;
|
||||
Handler handler;
|
||||
OrthographicCamera cam;
|
||||
Stage stage;
|
||||
|
||||
@Override
|
||||
public void create () {
|
||||
batch = new SpriteBatch();
|
||||
handler = new Handler(this);
|
||||
stage = new Stage(new ScreenViewport(), batch);
|
||||
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
|
||||
|
||||
|
||||
cam = new OrthographicCamera(Main.WIDTH/4, Main.HEIGHT/4);
|
||||
cam.setToOrtho(false, cam.viewportWidth, cam.viewportHeight);
|
||||
handler.init();
|
||||
|
||||
cam.update();
|
||||
}
|
||||
|
||||
public void resize() {
|
||||
|
||||
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
|
||||
cam.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render () {
|
||||
//Gdx.gl.glClearColor(1, 0, 0, 1);
|
||||
float dt = Gdx.graphics.getDeltaTime();
|
||||
cam.update();
|
||||
batch.setProjectionMatrix(cam.combined);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
handler.update(dt);
|
||||
//stage.act(dt);
|
||||
|
||||
batch.begin();
|
||||
handler.render(batch);
|
||||
|
||||
batch.end();
|
||||
batch.setColor(1, 1, 1, 1);
|
||||
stage.draw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose () {
|
||||
handler.dispose();
|
||||
batch.dispose();
|
||||
stage.dispose();
|
||||
}
|
||||
|
||||
public OrthographicCamera getCam() {return cam;}
|
||||
|
||||
public void setCam(OrthographicCamera cam) {this.cam = cam;}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.jwinslow.game.gameobjects;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
public abstract class GameObject {
|
||||
|
||||
//--- Propreties
|
||||
protected Handler handler;
|
||||
protected float x;
|
||||
protected float y;
|
||||
protected float depth;
|
||||
protected TextureRegion image;
|
||||
protected Rectangle bounds;
|
||||
|
||||
//--- Constructor
|
||||
public GameObject(Handler handler, float x, float y, TextureRegion image) {
|
||||
this.handler = handler;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.depth = y;
|
||||
this.image = image;
|
||||
this.bounds = new Rectangle();
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public abstract void update(float dt);
|
||||
public abstract void render(SpriteBatch batch);
|
||||
public abstract void dispose();
|
||||
|
||||
|
||||
//--- Getters and Setters
|
||||
public float getX() {return x;}
|
||||
public float getY() {return y;}
|
||||
public float getDepth() {return depth;}
|
||||
public Rectangle getBounds() {return bounds;}
|
||||
|
||||
public void setX(float x) {this.x = x;}
|
||||
public void setY(float y) {this.y = y;}
|
||||
public void setPosition(float x, float y) {this.x = x; this.y = y;}
|
||||
public void setDepth(float depth) {this.depth = depth;}
|
||||
public void setBounds(Rectangle bounds) {this.bounds = bounds;}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.jwinslow.game.gameobjects;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
public class GameObjectHandler {
|
||||
|
||||
//--- Propreties
|
||||
private Handler handler;
|
||||
private Array<GameObject> gameObjects;
|
||||
private Array<Player> players;
|
||||
|
||||
//--- Constructor
|
||||
public GameObjectHandler(Handler handler) {
|
||||
this.handler = handler;
|
||||
gameObjects = new Array<GameObject>();
|
||||
players = new Array<Player>();
|
||||
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public void update(float dt) {
|
||||
sort();
|
||||
for (GameObject o : gameObjects) {
|
||||
o.update(dt);
|
||||
}
|
||||
//System.out.println("Updateing gameObjects.");
|
||||
}
|
||||
|
||||
public void render(SpriteBatch batch) {
|
||||
for (GameObject o : gameObjects) {
|
||||
o.render(batch);
|
||||
}
|
||||
//System.out.println("rendering gameObjects " + gameObjects.size);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
gameObjects.clear();
|
||||
}
|
||||
|
||||
public void sort() {
|
||||
for (int i = 0; i < gameObjects.size; i++) {
|
||||
if (i+1 == gameObjects.size) break;
|
||||
if (gameObjects.get(i).getDepth() < gameObjects.get(i+1).getDepth()) {
|
||||
gameObjects.swap(i, i+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void add(GameObject o) {
|
||||
gameObjects.add(o);
|
||||
}
|
||||
|
||||
public void addAll(GameObject[] objects) {
|
||||
for (GameObject o: objects) {
|
||||
add(o);
|
||||
}
|
||||
}
|
||||
|
||||
public void remove(GameObject o) {
|
||||
gameObjects.removeValue(o, false);
|
||||
}
|
||||
|
||||
public void addPlayer(Player player) {
|
||||
players.add(player);
|
||||
add(player);
|
||||
/*
|
||||
for (int i = 0; i < players.length; i++) {
|
||||
if (players[i] != null) {
|
||||
players[i] = player;
|
||||
add(players[i]);
|
||||
break;
|
||||
}
|
||||
}//*/
|
||||
}
|
||||
|
||||
public void removePlayer(Player player) {
|
||||
players.removeValue(player, false);
|
||||
remove(player);/*
|
||||
for (int i = 0; i < players.length; i++) {
|
||||
if (players[i].equals(player)) {
|
||||
remove(player);
|
||||
players[i] = null;
|
||||
break;
|
||||
}
|
||||
}//*/
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public Array<GameObject> getGameObjects() {return gameObjects;}
|
||||
public Array<Player> getPlayers() {return players;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.jwinslow.game.gameobjects;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/12/2018.
|
||||
*/
|
||||
|
||||
public class OnlinePlayer extends Player {
|
||||
|
||||
public OnlinePlayer(Handler handler, float x, float y, TextureRegion image, int pid, String name) {
|
||||
super(handler, x, y, image);
|
||||
this.pid = pid;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
handleAnimation();
|
||||
bounds.x = x;
|
||||
bounds.y = y;
|
||||
sprite.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleAnimation() {
|
||||
if (dir <= 45 && dir >= -45) sprite.setAnimCurrent(sprite.getAnimRight());
|
||||
if (dir > 45 && dir < 135) sprite.setAnimCurrent(sprite.getAnimUp());
|
||||
if (dir >= 135 && dir <= 225) sprite.setAnimCurrent(sprite.getAnimLeft());
|
||||
if (dir > -135 && dir < -45) sprite.setAnimCurrent(sprite.getAnimDown());
|
||||
|
||||
if (idle == 1) {
|
||||
sprite.setManual(true);
|
||||
sprite.setCurrentFrame(sprite.getAnimCurrent().getKeyFrame(0));
|
||||
} else {
|
||||
sprite.setManual(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {return "OnlinePlayer";}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
package com.jwinslow.game.gameobjects;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.sprites.PlayerSprite;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class Player extends GameObject {
|
||||
|
||||
//--- Propreties
|
||||
public static int playerid = 1;
|
||||
public static final float SPEED = 0.7f;
|
||||
|
||||
private float vecX, vecY;
|
||||
private float speed;
|
||||
protected float dir;
|
||||
protected PlayerSprite sprite;
|
||||
protected int pid;
|
||||
protected int idle;
|
||||
protected String name;
|
||||
//private PlayerControls Controls;
|
||||
|
||||
|
||||
//--- Constructor
|
||||
public Player(Handler handler, float x, float y, TextureRegion image) {
|
||||
super(handler, x, y, image);
|
||||
this.bounds = new Rectangle(x, y, 16, 16);
|
||||
this.vecX = 0;
|
||||
this.vecY = 0;
|
||||
this.dir = 0;
|
||||
this.speed = 0;
|
||||
this.sprite = new PlayerSprite(handler, bounds.x, bounds.y, bounds.width, bounds.height, image, this);
|
||||
this.pid = 0;
|
||||
this.idle = 0;
|
||||
this.name = "Player";
|
||||
//this.Controls = new PlayerControls(PlayerControls.TOUCH);
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
handleInput();
|
||||
handleCollision();
|
||||
handleAnimation();
|
||||
|
||||
x += vecX;
|
||||
y += vecY;
|
||||
bounds.x = x;
|
||||
bounds.y = y;
|
||||
depth = y;
|
||||
sprite.update(dt);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
sprite.render(batch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {return "Player";}
|
||||
|
||||
public void handleInput() {
|
||||
//Controls.update();
|
||||
|
||||
if (Controls.getControllerType() == Controls.KEYBOARD) {
|
||||
if ((Controls.left && Controls.right) || (!Controls.left && !Controls.right)) {
|
||||
vecX = 0;
|
||||
} else if (Controls.left) {
|
||||
vecX = -SPEED;
|
||||
} else if (Controls.right) {
|
||||
vecX = SPEED;
|
||||
}
|
||||
|
||||
if ((Controls.up && Controls.down) || (!Controls.up && !Controls.down)) {
|
||||
vecY = 0;
|
||||
} else if (Controls.up) {
|
||||
vecY = SPEED;
|
||||
} else if (Controls.down) {
|
||||
vecY = -SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
if (Controls.getControllerType() == Controls.TOUCH) {
|
||||
float startX = Controls.sx;
|
||||
float startY = Controls.sy;
|
||||
float endX = Controls.ex;
|
||||
float endY = Controls.ey;
|
||||
|
||||
vecX = SPEED * (endX-startX) / 50;
|
||||
vecY = SPEED * (endY-startY) / 50;
|
||||
|
||||
float starter = 0.2f * (Gdx.graphics.getWidth()/ Main.WIDTH);
|
||||
if (vecX < starter && vecX > -starter) vecX = 0;
|
||||
if (vecY < starter && vecY > -starter) vecY = 0;
|
||||
|
||||
if (vecX > SPEED) vecX = SPEED;
|
||||
else if (vecX < -SPEED) vecX = -SPEED;
|
||||
if (vecY > SPEED) vecY = SPEED;
|
||||
else if (vecY < -SPEED) vecY = -SPEED;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void handleAnimation() {
|
||||
if (vecX != 0 || vecY != 0)
|
||||
dir = MathUtils.atan2(vecY, vecX) * MathUtils.radiansToDegrees;
|
||||
if (dir <= 45 && dir >= -45) sprite.setAnimCurrent(sprite.getAnimRight());
|
||||
if (dir > 45 && dir < 135) sprite.setAnimCurrent(sprite.getAnimUp());
|
||||
if (dir >= 135 && dir <= 225) sprite.setAnimCurrent(sprite.getAnimLeft());
|
||||
if (dir > -135 && dir < -45) sprite.setAnimCurrent(sprite.getAnimDown());
|
||||
|
||||
if (vecX == 0 && vecY == 0) {
|
||||
idle = 1;
|
||||
sprite.setManual(true);
|
||||
sprite.setCurrentFrame(sprite.getAnimCurrent().getKeyFrame(0));
|
||||
} else {
|
||||
idle = 0;
|
||||
sprite.setManual(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void handleCollision() {
|
||||
if (bounds.x + bounds.width + vecX > Assets.background.getWidth()) {
|
||||
vecX = 0;
|
||||
}
|
||||
if (bounds.x + vecX < 0) vecX = 0;
|
||||
if (bounds.y + bounds.height + vecY > Assets.background.getHeight() ||
|
||||
bounds.y + vecY < 0) {
|
||||
vecY = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public float getDir() {return dir;}
|
||||
public int getPid() {return pid;}
|
||||
public int getIdle() {return idle;}
|
||||
public String getName() {return name;}
|
||||
//public PlayerControls getControls() {return Controls;}
|
||||
|
||||
public void setDir(float dir) {this.dir = dir;}
|
||||
public void setPid(int pid) {this.pid = pid;}
|
||||
public void setIdle(int idle) {this.idle = idle;}
|
||||
public void setName(String name) {this.name = name;}
|
||||
|
||||
|
||||
public class PlayerControls {
|
||||
|
||||
public static final int KEYBOARD = 0, TOUCH = 1;
|
||||
|
||||
private int controlType;
|
||||
public boolean up, down, left, right;
|
||||
private int key_up, key_down, key_left, key_right;
|
||||
public float sx, sy, ex, ey;
|
||||
public float jx, jy;
|
||||
private boolean touched;
|
||||
|
||||
public PlayerControls(int controlType) {
|
||||
this.controlType = controlType;
|
||||
initControls();
|
||||
}
|
||||
|
||||
public void update() {
|
||||
switch (controlType) {
|
||||
case KEYBOARD:
|
||||
updateKeyboard();
|
||||
break;
|
||||
case TOUCH:
|
||||
updateTouch();
|
||||
break;
|
||||
default:
|
||||
controlType = KEYBOARD;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateKeyboard() {
|
||||
up = Gdx.input.isKeyPressed(key_up);
|
||||
down = Gdx.input.isKeyPressed(key_down);
|
||||
left = Gdx.input.isKeyPressed(key_left);
|
||||
right = Gdx.input.isKeyPressed(key_right);
|
||||
}
|
||||
|
||||
private void updateTouch() {
|
||||
if (Gdx.input.isTouched()) {
|
||||
if (!touched) {
|
||||
sx = Gdx.input.getX();
|
||||
sy = Gdx.input.getY();
|
||||
ex = sx;
|
||||
ey = sy;
|
||||
touched = true;
|
||||
} else {
|
||||
ex = Gdx.input.getX();
|
||||
ey = Gdx.input.getY();
|
||||
}
|
||||
|
||||
} else {
|
||||
sx = 0; sy = 0;
|
||||
ex = 0; ey = 0;
|
||||
touched = false;
|
||||
}
|
||||
|
||||
if (Gdx.input.justTouched()) {
|
||||
jx = Gdx.input.getX();
|
||||
jy = Gdx.input.getY();
|
||||
} else {
|
||||
jx = -1;
|
||||
jy = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private void initControls() {
|
||||
key_up = Keys.W;
|
||||
key_down = Keys.S;
|
||||
key_left = Keys.A;
|
||||
key_right = Keys.D;
|
||||
}
|
||||
|
||||
public int getControllerType() {return controlType;}
|
||||
public float getSx() {return sx;}
|
||||
public float getSy() {return sy;}
|
||||
public float getEx() {return ex;}
|
||||
public float getEy() {return ey;}
|
||||
public float getJx() {return jx;}
|
||||
public float getJy() {return jy;}
|
||||
public boolean getUp() {return up;}
|
||||
public boolean getDown() {return down;}
|
||||
public boolean getLeft() {return left;}
|
||||
public boolean getRight() {return right;}
|
||||
|
||||
public void setControllerType(int controlType) {this.controlType = controlType;}
|
||||
public void setKey_up(int key) {this.key_up = key;}
|
||||
public void setKey_down(int key) {this.key_down = key;}
|
||||
public void setKey_left(int key) {this.key_left = key;}
|
||||
public void setKey_right(int key) {this.key_right = key;}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,387 @@
|
||||
package com.jwinslow.game.net;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.gameobjects.OnlinePlayer;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
import com.jwinslow.game.states.State;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Created by jalen on 12/29/2017.
|
||||
*/
|
||||
|
||||
public class Connection {
|
||||
|
||||
public static final int PORT = 11321;
|
||||
|
||||
public enum MStates {
|
||||
CREATE_PLAYER_FROM_CLIENT,
|
||||
CREATE_PLAYER_FROM_SERVER,
|
||||
REMOVE_PLAYER,
|
||||
REMOVE_SERVER_PLAYER,
|
||||
GAME,
|
||||
PLAYER_NAME,
|
||||
ALL_PLAYERS_GAME,
|
||||
ALL_PLAYERS_INFO,
|
||||
ALL_PLAYERS_CREATE,
|
||||
PLAY,
|
||||
WAIT
|
||||
}
|
||||
|
||||
private static Handler handler;
|
||||
public static String receivedInfo;
|
||||
public static String sentInfo;
|
||||
public static boolean msgReady;
|
||||
public static int currentPid;
|
||||
public static MStates msgState;
|
||||
public static UDPServer udpServer;
|
||||
public static UDPClient udpClient;
|
||||
public static float elapsedTime;
|
||||
//public static TCPServer tcpServer;
|
||||
//public static TCPClient tcpClient;
|
||||
|
||||
public static void init(Handler handler) {
|
||||
Connection.handler = handler;
|
||||
receivedInfo = "";
|
||||
sentInfo = "";
|
||||
msgReady = false;
|
||||
currentPid = 0;
|
||||
msgState = MStates.WAIT;
|
||||
elapsedTime = 0;
|
||||
}
|
||||
|
||||
public static void create(int type) {
|
||||
// 0 - UDPServer
|
||||
// 1 - UDPClient
|
||||
// 2 - TCPServer
|
||||
// 3 - TCPClient
|
||||
switch (type) {
|
||||
case 0:createUDPServer();break;
|
||||
case 1:createUDPClient();break;
|
||||
case 2:createTCPServer();break;
|
||||
case 3:createTCPClient();break;
|
||||
default:
|
||||
handler.debugger.msg = "No connection type.";
|
||||
}
|
||||
}
|
||||
|
||||
public static void update(float dt) {
|
||||
String m = new String(getReceivedInfo());
|
||||
setReceivedInfo("");
|
||||
if (!m.equalsIgnoreCase("")) {
|
||||
String[] message = m.split(" ");
|
||||
receive(message);
|
||||
}
|
||||
String s = "";
|
||||
elapsedTime += dt;
|
||||
if (elapsedTime >= 1f/30f) {
|
||||
s = send(msgState);
|
||||
elapsedTime -= 1f/30f;
|
||||
}
|
||||
if (!s.equalsIgnoreCase("")) {
|
||||
setSentInfo(s);
|
||||
}
|
||||
if (getMsgReady()) sendWhere();
|
||||
}
|
||||
|
||||
public static void receive(String[] message) {
|
||||
String type = message[0];
|
||||
if (type.equalsIgnoreCase("cs")) {
|
||||
createPlayer(currentPid = Player.playerid, message[1]);
|
||||
Player.playerid++;
|
||||
msgState = MStates.ALL_PLAYERS_CREATE;
|
||||
|
||||
} else if (type.equalsIgnoreCase("c")) {
|
||||
createPlayer(Integer.parseInt(message[1]), message[2]);
|
||||
|
||||
} else if (type.equalsIgnoreCase("r")) {
|
||||
removePlayer(currentPid = Integer.parseInt(message[1]));
|
||||
msgState = MStates.REMOVE_PLAYER;
|
||||
|
||||
} else if (type.equalsIgnoreCase("g")) {
|
||||
updatePlayer(Integer.parseInt(message[1]),
|
||||
Float.parseFloat(message[2]), Float.parseFloat(message[3]),
|
||||
Float.parseFloat(message[4]), Integer.parseInt(message[5]));
|
||||
|
||||
} else if (type.equalsIgnoreCase("p")) {
|
||||
updatePlayerName(Integer.parseInt(message[1]), message[2]);
|
||||
|
||||
} else if (type.equalsIgnoreCase("2")) { // 2 players
|
||||
int numOfPlayers = 2;
|
||||
if (message[1].equalsIgnoreCase("g")) {
|
||||
updateGamePlayers(numOfPlayers, message);
|
||||
|
||||
} else if (message[1].equalsIgnoreCase("p")) {
|
||||
updateNamePlayers(numOfPlayers, message);
|
||||
|
||||
} else if (message[1].equalsIgnoreCase("c")) {
|
||||
updateCreatePlayers(numOfPlayers, message);}
|
||||
|
||||
} else if (type.equalsIgnoreCase("3")) { // 3 players
|
||||
int numOfPlayers = 3;
|
||||
if (message[1].equalsIgnoreCase("g")) {
|
||||
updateGamePlayers(numOfPlayers, message);
|
||||
|
||||
} else if (message[1].equalsIgnoreCase("p")) {
|
||||
updateNamePlayers(numOfPlayers, message);
|
||||
|
||||
} else if (message[1].equalsIgnoreCase("c")) {
|
||||
updateCreatePlayers(numOfPlayers, message);}
|
||||
|
||||
} else if (type.equalsIgnoreCase("4")) {
|
||||
int numOfPlayers = 4;
|
||||
if (message[1].equalsIgnoreCase("g")) {
|
||||
updateGamePlayers(numOfPlayers, message);
|
||||
|
||||
} else if (message[1].equalsIgnoreCase("p")) {
|
||||
updateNamePlayers(numOfPlayers, message);
|
||||
|
||||
} else if (message[1].equalsIgnoreCase("c")) {
|
||||
updateCreatePlayers(numOfPlayers, message);
|
||||
|
||||
}
|
||||
} else if (type.equalsIgnoreCase("play")){// *** Something wrong here for android
|
||||
System.out.println("Playing game now.");
|
||||
ButtonEffects.update(ButtonEffects.PLAY_GAME);
|
||||
}
|
||||
}
|
||||
|
||||
public static String send(MStates mState) {
|
||||
String message = "";
|
||||
|
||||
DecimalFormat df = new DecimalFormat("#.#");
|
||||
|
||||
switch (mState) {
|
||||
case ALL_PLAYERS_GAME:
|
||||
message = handler.getPlayers().size + " g ";
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
message += handler.getPlayers().get(i).getPid() + " " +
|
||||
df.format(handler.getPlayers().get(i).getX()) + " " +
|
||||
df.format(handler.getPlayers().get(i).getY()) + " " +
|
||||
df.format(handler.getPlayers().get(i).getDir()) + " " +
|
||||
handler.getPlayers().get(i).getIdle();
|
||||
if (i+1 != handler.getPlayers().size) {
|
||||
message += " ";
|
||||
}
|
||||
}
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case ALL_PLAYERS_INFO:
|
||||
message = handler.getPlayers().size + " p ";
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
message += handler.getPlayers().get(i).getPid() + " " +
|
||||
handler.getPlayers().get(i).getName();
|
||||
if (i+1 != handler.getPlayers().size) {
|
||||
message += " ";
|
||||
}
|
||||
}
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case ALL_PLAYERS_CREATE:
|
||||
message = handler.getPlayers().size + " c ";
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
message += handler.getPlayers().get(i).getPid() + " " +
|
||||
handler.getPlayers().get(i).getName();
|
||||
if (i+1 != handler.getPlayers().size) {
|
||||
message += " ";
|
||||
}
|
||||
}
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case CREATE_PLAYER_FROM_CLIENT:
|
||||
message = "cs " + handler.getPlayers().get(0).getName();
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case CREATE_PLAYER_FROM_SERVER:
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
if (currentPid == handler.getPlayers().get(i).getPid()) {
|
||||
message = "c " + currentPid + " " + handler.getPlayers().get(i).getName();
|
||||
break;
|
||||
}
|
||||
}
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case REMOVE_PLAYER:
|
||||
message = "r " + currentPid;
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case REMOVE_SERVER_PLAYER:
|
||||
|
||||
break;
|
||||
case GAME:
|
||||
message = "g " + handler.getPlayers().get(0).getPid() + " " +
|
||||
df.format(handler.getPlayers().get(0).getX()) + " " +
|
||||
df.format(handler.getPlayers().get(0).getY()) + " " +
|
||||
df.format(handler.getPlayers().get(0).getDir()) + " " +
|
||||
handler.getPlayers().get(0).getIdle();
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case PLAYER_NAME:
|
||||
message = "p " + handler.getPlayers().get(0).getPid() + " " +
|
||||
handler.getPlayers().get(0).getName();
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case PLAY:
|
||||
message = "play";
|
||||
setMsgReady(true);
|
||||
break;
|
||||
case WAIT:
|
||||
if (!getMsgReady()) message = "";
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
if (State.getCurrentState() != handler.getGameState())
|
||||
msgState = MStates.WAIT;
|
||||
else if (State.getCurrentState() == handler.getGameState()) {
|
||||
if (udpServer != null)
|
||||
msgState = MStates.ALL_PLAYERS_GAME;
|
||||
else if (udpClient != null)
|
||||
msgState = MStates.GAME;
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private static void sendWhere() {
|
||||
try {
|
||||
if (udpServer != null) udpServer.updateSend();
|
||||
if (udpClient != null) udpClient.updateSend();
|
||||
} catch (IOException ex) {
|
||||
System.out.println("Could not send message.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void createUDPServer() {
|
||||
handler.debugger.msg = "UDP Server";
|
||||
udpServer = new UDPServer(handler, PORT);
|
||||
udpServer.start();
|
||||
}
|
||||
|
||||
private static void createUDPClient() {
|
||||
//handler.debugger.msg = "UDP Client";
|
||||
udpClient = new UDPClient(handler, PORT);
|
||||
udpClient.start();
|
||||
}
|
||||
|
||||
private static void createTCPServer() {
|
||||
handler.debugger.msg = "TCP Server";
|
||||
}
|
||||
|
||||
private static void createTCPClient() {
|
||||
handler.debugger.msg = "TCP Client";
|
||||
}
|
||||
|
||||
private static void createPlayer(int pid, String name) {
|
||||
boolean check = false;
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
if (handler.getPlayers().get(i).getName().equalsIgnoreCase(name)) {
|
||||
handler.getPlayers().get(i).setPid(pid);
|
||||
check = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!check)
|
||||
handler.getGameObjectHandler().addPlayer(new OnlinePlayer(handler, 0, 0, new TextureRegion(Assets.player2), pid, name));
|
||||
}
|
||||
|
||||
private static void removePlayer(int pid) {
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
if (handler.getPlayers().get(i).getPid() == pid) {
|
||||
handler.getGameObjectHandler().removePlayer(handler.getPlayers().get(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void updatePlayer(int pid, float x, float y, float dir, int idle) {
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
if (i == 0) continue;
|
||||
if (handler.getPlayers().get(i).getPid() == pid) {
|
||||
handler.getPlayers().get(i).setX(x);
|
||||
handler.getPlayers().get(i).setY(y);
|
||||
handler.getPlayers().get(i).setDir(dir);
|
||||
handler.getPlayers().get(i).setIdle(idle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void updatePlayerName(int pid, String name) {
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
if (handler.getPlayers().get(i).getPid() == pid) {
|
||||
handler.getPlayers().get(i).setName(name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateGamePlayers(int numOfPlayers, String[] message) {
|
||||
String[][] m = new String[numOfPlayers][5];
|
||||
int index = 2;
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
m[i][j] = message[index];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < numOfPlayers; i++) {
|
||||
updatePlayer(Integer.parseInt(m[i][0]),
|
||||
Float.parseFloat(m[i][1]), Float.parseFloat(m[i][2]),
|
||||
Float.parseFloat(m[i][3]), Integer.parseInt(m[i][4]));
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateNamePlayers(int numOfPlayers, String[] message) {
|
||||
String[][] m = new String[numOfPlayers][2];
|
||||
int index = 2;
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
m[i][j] = message[index];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < numOfPlayers; i++) {
|
||||
updatePlayerName(Integer.parseInt(m[i][0]),
|
||||
m[i][1]);
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateCreatePlayers(int numOfPlayers, String[] message) {
|
||||
String[][] m = new String[numOfPlayers][2];
|
||||
int index = 2;
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
m[i][j] = message[index];
|
||||
index++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < numOfPlayers; i++) {
|
||||
createPlayer(Integer.parseInt(m[i][0]),
|
||||
m[i][1]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void dispose() {
|
||||
System.out.println("Disposing of networks.");
|
||||
if (udpServer != null) {udpServer.stop();}
|
||||
if (udpClient != null) {udpClient.stop();}
|
||||
}
|
||||
|
||||
public static synchronized void setReceivedInfo(String m) {Connection.receivedInfo = m;}
|
||||
public static synchronized String getReceivedInfo() {return Connection.receivedInfo;}
|
||||
|
||||
public static synchronized void setSentInfo(String m) {Connection.sentInfo = m;}
|
||||
public static synchronized String getSentInfo() {return Connection.sentInfo;}
|
||||
|
||||
public static synchronized void setMsgReady(boolean ready) {Connection.msgReady = ready;}
|
||||
public static synchronized boolean getMsgReady() {return Connection.msgReady;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.jwinslow.game.net;
|
||||
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.gameobjects.OnlinePlayer;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
import javax.xml.crypto.Data;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/14/2018.
|
||||
*/
|
||||
|
||||
public class UDPClient implements Runnable {
|
||||
|
||||
enum States {
|
||||
CONNECTING,
|
||||
CONNECTED,
|
||||
GAME;
|
||||
}
|
||||
|
||||
private Handler handler;
|
||||
private Thread t;
|
||||
private InetAddress serverAddress;
|
||||
private int port;
|
||||
private States state;
|
||||
private DatagramSocket socket;
|
||||
private boolean bExit;
|
||||
|
||||
public UDPClient(Handler handler, int port) {
|
||||
this.handler = handler;
|
||||
this.port = port;
|
||||
state = States.CONNECTING;
|
||||
bExit = false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
socket = new DatagramSocket(port);
|
||||
serverAddress = InetAddress.getByName(handler.getConnectState().getIpAddressArea().getMessage());
|
||||
while (!bExit) {
|
||||
try {
|
||||
if (socket.isClosed()) socket = new DatagramSocket(port);
|
||||
System.out.println("Running UDP Client");
|
||||
updateReceive(socket);
|
||||
} catch (IOException ex) {
|
||||
handler.debugger.msg = "UDP Client stopped working.";
|
||||
} catch (RuntimeException ex) {
|
||||
handler.debugger.msg = "UDP Client stopped working.";
|
||||
}
|
||||
}
|
||||
socket.close();
|
||||
} catch (SocketException ex) {
|
||||
ex.printStackTrace();
|
||||
} catch (UnknownHostException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
t = new Thread(this);
|
||||
t.start();
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
System.out.println("Stopping UDP Client.");
|
||||
handler.debugger.msg = "Stopping UDP Client.";
|
||||
bExit = true;
|
||||
}
|
||||
|
||||
public void updateSend() throws IOException, RuntimeException {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
DatagramSocket s = new DatagramSocket(port+1);
|
||||
String data = "";
|
||||
if (Connection.getMsgReady()) {
|
||||
data = Connection.getSentInfo();
|
||||
Connection.setSentInfo("");
|
||||
Connection.setMsgReady(false);
|
||||
}
|
||||
if (data != null && !data.equalsIgnoreCase("")) {
|
||||
System.out.println("Client-Message sent is: " + data);
|
||||
s.send(new DatagramPacket(data.getBytes(), data.getBytes().length, serverAddress, port));
|
||||
handler.debugger.msg = serverAddress.getHostAddress() + " " + port;
|
||||
}
|
||||
s.close();
|
||||
} catch (SocketException ex) {
|
||||
System.out.println("Could not create socket. UDPClient.updateSend()");
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}).start();
|
||||
|
||||
}
|
||||
|
||||
public void updateReceive(DatagramSocket socket) throws IOException, RuntimeException {
|
||||
DatagramPacket request = new DatagramPacket(new byte[512], 512);
|
||||
socket.receive(request);
|
||||
String message = new String(request.getData(), 0, request.getLength());
|
||||
System.out.println("Client - Message received is: " + message);
|
||||
Connection.setReceivedInfo(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package com.jwinslow.game.net;
|
||||
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.gameobjects.OnlinePlayer;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/13/2018.
|
||||
*/
|
||||
|
||||
public class UDPServer implements Runnable {
|
||||
|
||||
enum States {
|
||||
CONNECT,
|
||||
GAME
|
||||
}
|
||||
|
||||
enum sendStates {
|
||||
CREATE_PLAYER,
|
||||
REMOVE_PLAYER,
|
||||
PLAYER_POS,
|
||||
PLAYER_NAME,
|
||||
WAIT
|
||||
}
|
||||
|
||||
private Handler handler;
|
||||
private Thread t;
|
||||
private int port;
|
||||
private Array<InetAddress> addresses;
|
||||
private InetAddress currentAddr;
|
||||
private States state;
|
||||
private InetAddress ipAddress;
|
||||
private DatagramSocket socket;
|
||||
private boolean bExit;
|
||||
|
||||
|
||||
public UDPServer(Handler handler, int port) {
|
||||
this.handler = handler;
|
||||
this.port = port;
|
||||
addresses = new Array<InetAddress>();
|
||||
state = States.CONNECT;
|
||||
bExit = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
try {
|
||||
socket = new DatagramSocket(port);
|
||||
while (!bExit) {
|
||||
try {
|
||||
if (socket.isClosed()) socket = new DatagramSocket(port);
|
||||
handler.debugger.msg = "Running UDP server";
|
||||
System.out.println("Running UDP server.");
|
||||
updateReceive(socket);
|
||||
} catch (IOException ex) {
|
||||
handler.debugger.msg = "UDP Server stopped working. IOException";
|
||||
ex.printStackTrace();
|
||||
} catch (RuntimeException ex) {
|
||||
handler.debugger.msg = "UDP Server stopped working. RuntimeException";
|
||||
ex.printStackTrace();
|
||||
}
|
||||
socket.close();
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
stop();
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
t = new Thread(this);
|
||||
t.start();
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
System.out.println("Stopping UDP Server.");
|
||||
handler.debugger.msg = "Stopping UDP Server.";
|
||||
bExit = true;
|
||||
}
|
||||
|
||||
public void updateReceive(DatagramSocket socket) throws IOException {
|
||||
DatagramPacket request = new DatagramPacket(new byte[512], 512);
|
||||
socket.receive(request);
|
||||
currentAddr = request.getAddress();
|
||||
if (!addresses.contains(currentAddr, false)) {
|
||||
addresses.add(currentAddr);
|
||||
}
|
||||
String message = new String(request.getData(), 0, request.getLength());
|
||||
System.out.println("Server-Message receieved is: " + message);
|
||||
Connection.setReceivedInfo(message);
|
||||
|
||||
}
|
||||
|
||||
public void updateSend() throws IOException, RuntimeException {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
DatagramSocket s;
|
||||
try {
|
||||
s = new DatagramSocket(port+1);
|
||||
String data = "";
|
||||
if (Connection.getMsgReady()) {
|
||||
data = Connection.getSentInfo();
|
||||
Connection.setSentInfo("");
|
||||
Connection.setMsgReady(false);
|
||||
}
|
||||
if (data != null && !data.equalsIgnoreCase("")) {
|
||||
System.out.println("Server-Message sent is: " + data);
|
||||
for (int i = 0; i < addresses.size; i++) {
|
||||
s.send(new DatagramPacket(data.getBytes(), data.getBytes().length, addresses.get(i), port));
|
||||
}
|
||||
}
|
||||
s.close();
|
||||
} catch (SocketException ex) {
|
||||
System.out.println("Could not create socket. UDPServer.updateSend()");
|
||||
ex.printStackTrace();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
|
||||
|
||||
}
|
||||
|
||||
//Getters and Setters
|
||||
public int getPort() {return port;}
|
||||
public States getState() {return state;}
|
||||
public InetAddress getIpAddress() {return ipAddress;}
|
||||
|
||||
public void setPort(int port) {this.port = port;}
|
||||
public void setState(States state) {this.state = state;}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.jwinslow.game.sprites;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.gameobjects.GameObject;
|
||||
|
||||
public class PlayerSprite extends Sprite{
|
||||
|
||||
//--- Propreties
|
||||
private Animation<TextureRegion> animLeft, animRight, animUp, animDown, animCurrent;
|
||||
private boolean manual;
|
||||
private float elapsedTime;
|
||||
|
||||
//--- Constructor
|
||||
public PlayerSprite(Handler handler, float x, float y, float width, float height, TextureRegion image, GameObject obj) {
|
||||
super(handler, x, y , width, height, image, obj);
|
||||
this.manual = false;
|
||||
createAnimation();
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
elapsedTime += dt;
|
||||
bounds.x = gameObj.getX();
|
||||
bounds.y = gameObj.getY();
|
||||
if (!manual)
|
||||
currentFrame = animCurrent.getKeyFrame(elapsedTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
batch.draw(currentFrame, bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
private void createAnimation() {
|
||||
TextureRegion[][] images = image.split(16, 16);
|
||||
frames = new TextureRegion[images.length * images[0].length];
|
||||
for (int i = 0; i < images.length; i++) {
|
||||
for (int j = 0; j < images[i].length; j++) {
|
||||
frames[(i*images.length)+j] = images[i][j];
|
||||
}
|
||||
}
|
||||
Array<TextureRegion> animFrames = new Array<TextureRegion>();
|
||||
for (int i = 0; i < images[0].length; i++) {
|
||||
animFrames.add(images[0][i]);
|
||||
}
|
||||
animDown = new Animation<TextureRegion>(0.1f, animFrames, PlayMode.LOOP);
|
||||
animFrames.clear();
|
||||
|
||||
for (int i = 0; i < images[1].length; i++) {
|
||||
animFrames.add(images[1][i]);
|
||||
}
|
||||
animUp = new Animation<TextureRegion>(0.1f, animFrames, PlayMode.LOOP);
|
||||
animFrames.clear();
|
||||
|
||||
for (TextureRegion t : images[2]) {
|
||||
animFrames.add(t);
|
||||
}
|
||||
animRight = new Animation<TextureRegion>(0.1f, animFrames, PlayMode.LOOP);
|
||||
animFrames.clear();
|
||||
|
||||
for (TextureRegion t : images[3]) {
|
||||
animFrames.add(t);
|
||||
}
|
||||
animLeft = new Animation<TextureRegion>(0.1f, animFrames, PlayMode.LOOP);
|
||||
animFrames.clear();
|
||||
|
||||
animCurrent = animDown;
|
||||
currentFrame = animCurrent.getKeyFrame(0);
|
||||
}
|
||||
|
||||
|
||||
//--- Getters and Setters
|
||||
public Animation<TextureRegion> getAnimDown() {return animDown;}
|
||||
public Animation<TextureRegion> getAnimUp() {return animUp;}
|
||||
public Animation<TextureRegion> getAnimRight() {return animRight;}
|
||||
public Animation<TextureRegion> getAnimLeft() {return animLeft;}
|
||||
public Animation<TextureRegion> getAnimCurrent() {return animCurrent;}
|
||||
public boolean getManual() {return manual;}
|
||||
|
||||
public void setAnimCurrent(Animation anim) {this.animCurrent = anim;}
|
||||
public void setManual(boolean manual) {this.manual = manual;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.jwinslow.game.sprites;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.Animation;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.gameobjects.GameObject;
|
||||
|
||||
public abstract class Sprite {
|
||||
|
||||
//--- Propreties
|
||||
protected Handler handler;
|
||||
protected TextureRegion image;
|
||||
protected TextureRegion currentFrame;
|
||||
protected TextureRegion[] frames;
|
||||
protected float x;
|
||||
protected float y;
|
||||
protected float width;
|
||||
protected float height;
|
||||
protected int currentFrameIndex;
|
||||
protected int numOfFrames;
|
||||
protected Rectangle bounds;
|
||||
protected GameObject gameObj;
|
||||
|
||||
//--- Constructor
|
||||
public Sprite(Handler handler, TextureRegion image) {
|
||||
this.handler = handler;
|
||||
this.image = image;
|
||||
this.currentFrame = new TextureRegion();
|
||||
this.frames = new TextureRegion[0];
|
||||
this.bounds = new Rectangle();
|
||||
}
|
||||
public Sprite(Handler handler, float x, float y, float width, float height, TextureRegion image, GameObject obj) {
|
||||
this.handler = handler;
|
||||
this.image = image;
|
||||
this.currentFrame = new TextureRegion();
|
||||
this.frames = new TextureRegion[0];
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
this.bounds = new Rectangle(x, y, width, height);
|
||||
this.gameObj = obj;
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public abstract void update(float dt);
|
||||
public abstract void render(SpriteBatch batch);
|
||||
public abstract void dispose();
|
||||
|
||||
//--- Getters and Setters
|
||||
public TextureRegion getImage() {return image;}
|
||||
public TextureRegion getCurrentFrame() {return currentFrame;}
|
||||
public TextureRegion[] getFrames() {return frames;}
|
||||
public float getX() {return x;}
|
||||
public float getY() {return y;}
|
||||
public float getWidth() {return width;}
|
||||
public float getHeight() {return height;}
|
||||
public int getCurrentFrameIndex() {return currentFrameIndex;}
|
||||
public int getNumOfFrames() {return numOfFrames;}
|
||||
|
||||
public void setImage(TextureRegion image) {this.image =image;}
|
||||
public void setCurrentFrame(TextureRegion frame) {this.currentFrame = frame;}
|
||||
public void setX(float x) {this.x = x;}
|
||||
public void setY(float y) {this.y = y;}
|
||||
public void setWidth(float width) {this.width = width;}
|
||||
public void setHeight(float height) {this.height = height;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package com.jwinslow.game.states;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.gameobjects.OnlinePlayer;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
import com.jwinslow.game.net.Connection;
|
||||
import com.jwinslow.game.ui.UIButton;
|
||||
import com.jwinslow.game.ui.UITextArea;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/8/2018.
|
||||
*/
|
||||
|
||||
public class ConnectState extends State {
|
||||
|
||||
private int connectionType;
|
||||
|
||||
private UIButton playButton, mainMenuButton;
|
||||
private UITextArea ipAddressArea, playersArea, playerNameArea;
|
||||
|
||||
public ConnectState(Handler handler, int connectionType) {
|
||||
super(handler);
|
||||
this.connectionType = connectionType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
if (connectionType == -1) {
|
||||
handler.getGameObjectHandler().addPlayer(new Player(handler, Assets.background.getWidth()/2, Assets.background.getHeight()/2,
|
||||
new TextureRegion(Assets.player1)));
|
||||
State.setCurrentState(handler.getGameState());
|
||||
handler.getConnectState().dispose();
|
||||
State.getCurrentState().init();
|
||||
return;
|
||||
}
|
||||
|
||||
Connection.init(handler);
|
||||
|
||||
boolean editText = false;
|
||||
String ipAddress = "IP Address here.";
|
||||
ButtonEffects be = ButtonEffects.CONNECT;
|
||||
boolean canBePressed = true;
|
||||
String buttonMessage = "No message";
|
||||
switch (connectionType) {
|
||||
case 0:
|
||||
editText = false;
|
||||
handler.getGameObjectHandler().addPlayer(new Player(handler, 0, 0,
|
||||
new TextureRegion(Assets.player1)));
|
||||
handler.getPlayers().get(0).setPid(Player.playerid);
|
||||
Player.playerid++;
|
||||
be = ButtonEffects.PLAY_GAME;
|
||||
buttonMessage = "Play";
|
||||
canBePressed = false;
|
||||
Connection.create(connectionType);
|
||||
break;
|
||||
case 1:
|
||||
editText = true;
|
||||
handler.getGameObjectHandler().addPlayer(new Player(handler, 0, 0,
|
||||
new TextureRegion(Assets.player1)));
|
||||
buttonMessage = "Connect";
|
||||
break;
|
||||
case 2: editText = false; break;
|
||||
case 3: editText = true; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
playButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()*3/4, Gdx.graphics.getHeight()/8, buttonMessage,
|
||||
be, 0.75f, 2.5f * (Gdx.graphics.getWidth()/ Main.WIDTH), true, canBePressed);
|
||||
mainMenuButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/4, Gdx.graphics.getHeight()/8, "Back",
|
||||
ButtonEffects.MAIN_MENU, 0.75f, 2.5f * (Gdx.graphics.getWidth()/ Main.WIDTH), true, true);
|
||||
|
||||
playerNameArea = new UITextArea(handler, new TextureRegion(Assets.textArea),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()*8/9, 1.25f, 0.2f,
|
||||
2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), 1, 12, "Enter Name", true);
|
||||
playerNameArea.setTitle("Enter Player Name:");
|
||||
playerNameArea.setHint("Name here");
|
||||
|
||||
ipAddressArea = new UITextArea(handler, new TextureRegion(Assets.textArea),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()*7/9, 1.25f, 0.2f,
|
||||
2f * (Gdx.graphics.getWidth()/Main.WIDTH), 7, 15, ipAddress, editText);
|
||||
ipAddressArea.setTitle("Enter IP Address:");
|
||||
ipAddressArea.setHint("Ex: 192.168.0.1");
|
||||
|
||||
playersArea = new UITextArea(handler, new TextureRegion(Assets.textArea),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()*4/9, 1.5f, 0.65f,
|
||||
2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), false);
|
||||
|
||||
handler.getStage().addActor(playButton);
|
||||
handler.getStage().addActor(mainMenuButton);
|
||||
handler.getStage().addActor(ipAddressArea);
|
||||
handler.getStage().addActor(playersArea);
|
||||
handler.getStage().addActor(playerNameArea);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
playButton.update(dt);
|
||||
mainMenuButton.update(dt);
|
||||
ipAddressArea.update(dt);
|
||||
playersArea.update(dt);
|
||||
playerNameArea.update(dt);
|
||||
|
||||
if (!playerNameArea.getMessage().equalsIgnoreCase("Enter Name") && handler.getPlayers().size != 0) {
|
||||
handler.getPlayers().get(0).setName(playerNameArea.getMessage());
|
||||
}
|
||||
|
||||
if (handler.getPlayers().size > 1) {
|
||||
playButton.setCanBePressed(true);
|
||||
}
|
||||
|
||||
String message = "Players:";
|
||||
for (int i = 0; i < handler.getPlayers().size; i++) {
|
||||
Player player = handler.getPlayers().get(i);
|
||||
message += "\n"+player.getName();
|
||||
}
|
||||
playersArea.setMessage(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
batch.draw(Assets.background, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
if (playButton != null) playButton.dispose();
|
||||
if (mainMenuButton != null) mainMenuButton.dispose();
|
||||
if (ipAddressArea != null) ipAddressArea.dispose();
|
||||
if (playersArea != null) playersArea.dispose();
|
||||
if (playerNameArea != null) playerNameArea.dispose();
|
||||
}
|
||||
|
||||
public void addOnlinePlayer(int pid, String name) {
|
||||
handler.getGameObjectHandler().addPlayer(new OnlinePlayer(handler, Assets.background.getWidth()/2, Assets.background.getHeight()/2,
|
||||
new TextureRegion(Assets.player2), pid, name));
|
||||
}
|
||||
|
||||
//Getters and Setters
|
||||
public int getConnectionType() {return connectionType;}
|
||||
public UIButton getPlayButton() {return playButton;}
|
||||
public UIButton getMainMenuButton() {return mainMenuButton;}
|
||||
public UITextArea getIpAddressArea() {return ipAddressArea;}
|
||||
public UITextArea getPlayersArea() {return playersArea;}
|
||||
public UITextArea getPlayerNameArea() {return playerNameArea;}
|
||||
|
||||
public void setConnectionType(int type) {this.connectionType = type;}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.jwinslow.game.states;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
import com.jwinslow.game.ui.PauseButton;
|
||||
import com.jwinslow.game.ui.PointerAnchor;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
|
||||
public class GameState extends State {
|
||||
|
||||
//--- Propreties
|
||||
private PointerAnchor pointer;
|
||||
private PauseButton pauseButton;
|
||||
|
||||
//--- Constructor
|
||||
public GameState(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
handler.getCamHandler().setObjOfFocus(handler.getPlayers().get(0));
|
||||
pointer = new PointerAnchor(handler, new TextureRegion(Assets.touchAnchor), 0, 0, handler.getPlayers().get(0));
|
||||
pauseButton = new PauseButton(handler, new TextureRegion(Assets.pauseButton));
|
||||
handler.getStage().addActor(pointer);
|
||||
handler.getStage().addActor(pauseButton);
|
||||
System.out.println("Creating player and adding to gameObjects" + handler.getGameObjectHandler().getGameObjects().size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
handler.getGameObjectHandler().update(dt);
|
||||
pointer.update(dt);
|
||||
pauseButton.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
batch.draw(Assets.background, 0, 0);
|
||||
handler.getGameObjectHandler().render(batch);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
//if (pointer != null) pointer.dispose(); //no need for this
|
||||
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public PointerAnchor getPointer() {return pointer;}
|
||||
public PauseButton getPauseButton() {return pauseButton;}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.jwinslow.game.states;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
|
||||
public class InitState extends State{
|
||||
|
||||
//--- Propreties
|
||||
|
||||
|
||||
//--- Constructor
|
||||
public InitState(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
@Override
|
||||
public void init() {
|
||||
Assets.loadAllTextures();
|
||||
|
||||
//handler.getCam().zoom = 0.5f;
|
||||
handler.getCam().position.x = handler.getCam().viewportWidth/2+5;//Added 5 because it start out of collision there.
|
||||
handler.getCam().position.y = handler.getCam().viewportHeight/2+5;
|
||||
|
||||
//State.setCurrentState(handler.getMenuState());
|
||||
//State.setCurrentState(handler.getGameState());
|
||||
//State.getCurrentState().init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
//handler.getMain().resize(Main.WIDTH, Main.HEIGHT);
|
||||
State.setCurrentState(handler.getMenuState());
|
||||
//State.setCurrentState(handler.getGameState());
|
||||
State.getCurrentState().init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.jwinslow.game.states;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.ui.UIButton;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
|
||||
public class MenuState extends State {
|
||||
|
||||
//--- Propreties
|
||||
private UIButton startGameButton, exitGameButton, playGameButton, backButton, changeConnectionButton;
|
||||
|
||||
//--- Constructor
|
||||
public MenuState(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
@Override
|
||||
public void init() {
|
||||
handler.getCam().position.set(Assets.background.getWidth()/2, Assets.background.getHeight()/2, 0);
|
||||
startGameButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2, "Start\nGame",
|
||||
ButtonEffects.START, 2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), true);
|
||||
exitGameButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/4, "Exit\nGame",
|
||||
ButtonEffects.EXIT_GAME, 2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), true);
|
||||
playGameButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2, "Play\nGame",
|
||||
ButtonEffects.START_GAME, 2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), false);
|
||||
backButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/4, "Back",
|
||||
ButtonEffects.BACK, 2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), false);
|
||||
changeConnectionButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()*3/4, "Create\nUDP Server",
|
||||
ButtonEffects.NETWORK_SWITCH, 2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), false);
|
||||
handler.getStage().addActor(startGameButton);
|
||||
handler.getStage().addActor(exitGameButton);
|
||||
handler.getStage().addActor(playGameButton);
|
||||
handler.getStage().addActor(backButton);
|
||||
handler.getStage().addActor(changeConnectionButton);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
startGameButton.update(dt);
|
||||
//handler.message = "" /*+ startGameButton.getRectangle().x //*/+ ", " + Controls.sx + ", " + Controls.sy;
|
||||
exitGameButton.update(dt);
|
||||
playGameButton.update(dt);
|
||||
backButton.update(dt);
|
||||
changeConnectionButton.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
|
||||
batch.draw(Assets.background, 0 , 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
if (startGameButton != null) startGameButton.dispose();
|
||||
if (exitGameButton != null) exitGameButton.dispose();
|
||||
if (playGameButton != null) playGameButton.dispose();
|
||||
if (backButton != null) backButton.dispose();
|
||||
if (changeConnectionButton != null) changeConnectionButton.dispose();
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public UIButton getStartGameButton() {return startGameButton;}
|
||||
public UIButton getExitGameButton() {return exitGameButton;}
|
||||
public UIButton getPlayGameButton() {return playGameButton;}
|
||||
public UIButton getBackButton() {return backButton;}
|
||||
public UIButton getChangeConnectionButton() {return changeConnectionButton;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.jwinslow.game.states;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.ui.UIButton;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
|
||||
/**
|
||||
* Created by jalen on 12/27/2017.
|
||||
*/
|
||||
|
||||
public class PauseState extends State {
|
||||
|
||||
private UIButton continueButton, mainMenuButton, exitGameButton;
|
||||
|
||||
public PauseState(Handler handler) {
|
||||
super(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
continueButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()*3/4, "Continue",
|
||||
ButtonEffects.CONTINUE_GAME, 2.5f * (Gdx.graphics.getWidth()/ Main.WIDTH), true);
|
||||
mainMenuButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2, "Main Menu",
|
||||
ButtonEffects.MAIN_MENU, 2.5f * (Gdx.graphics.getWidth()/ Main.WIDTH), true);
|
||||
exitGameButton = new UIButton(handler, new TextureRegion(Assets.button),
|
||||
Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/4, "Exit\nGame",
|
||||
ButtonEffects.EXIT_GAME, 2.5f * (Gdx.graphics.getWidth()/Main.WIDTH), true);
|
||||
handler.getStage().addActor(continueButton);
|
||||
handler.getStage().addActor(mainMenuButton);
|
||||
handler.getStage().addActor(exitGameButton);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
continueButton.update(dt);
|
||||
mainMenuButton.update(dt);
|
||||
exitGameButton.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(SpriteBatch batch) {
|
||||
|
||||
handler.message = "Pause.";
|
||||
batch.setColor(1, 1, 1, 1);
|
||||
batch.setColor(1, 1, 1, 0.5f);
|
||||
batch.draw(Assets.background, 0 , 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
handler.message = "";
|
||||
if (continueButton != null) continueButton.dispose();
|
||||
if (mainMenuButton != null) mainMenuButton.dispose();
|
||||
if (exitGameButton != null) exitGameButton.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.jwinslow.game.states;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
public abstract class State {
|
||||
|
||||
//--- Propreties
|
||||
protected static State currentState;
|
||||
protected Handler handler;
|
||||
|
||||
//--- Constructor
|
||||
public State(Handler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public abstract void init();
|
||||
|
||||
public abstract void update(float dt);
|
||||
|
||||
public abstract void render(SpriteBatch batch);
|
||||
|
||||
public abstract void dispose();
|
||||
|
||||
//--- Getters and Setters
|
||||
public static State getCurrentState() {return State.currentState;}
|
||||
|
||||
public static void setCurrentState(State state) {State.currentState = state;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.jwinslow.game.ui;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
|
||||
/**
|
||||
* Created by jalen on 12/27/2017.
|
||||
*/
|
||||
|
||||
public class PauseButton extends UIObject{
|
||||
|
||||
|
||||
public PauseButton(Handler handler, TextureRegion image) {
|
||||
super(handler, image, Gdx.graphics.getWidth()-Gdx.graphics.getWidth()/8, Gdx.graphics.getHeight()-Gdx.graphics.getWidth()/8);
|
||||
bounds = new Rectangle(x, y, Gdx.graphics.getWidth()/8, Gdx.graphics.getWidth()/8);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if (bounds.contains(Controls.jx, Controls.jy)) {
|
||||
ButtonEffects.update(ButtonEffects.PAUSE_GAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
batch.draw(image, bounds.x, bounds.y , bounds.width, bounds.height);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.jwinslow.game.ui;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.Main;
|
||||
import com.jwinslow.game.gameobjects.Player;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
|
||||
public class PointerAnchor extends Actor {
|
||||
|
||||
//--- Propreties
|
||||
private Handler handler;
|
||||
private TextureRegion image;
|
||||
private float x, y;
|
||||
private Rectangle bounds;
|
||||
//private Player player;
|
||||
|
||||
//--- Constructor
|
||||
public PointerAnchor(Handler handler, TextureRegion image, float x, float y, Player player) {
|
||||
this.image = image;
|
||||
this.handler = handler;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
bounds = new Rectangle(x, y, Gdx.graphics.getWidth()/10, Gdx.graphics.getWidth()/10);
|
||||
//this.player = player;
|
||||
setVisible(false);
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public void update(float dt) {
|
||||
if (Controls.sx != 0 && Controls.sy != 0) {
|
||||
x = Controls.sx;
|
||||
y = Controls.sy;
|
||||
bounds.x = x;
|
||||
bounds.y = y;
|
||||
setVisible(true);
|
||||
} else {
|
||||
x = -1;
|
||||
y = -1;
|
||||
bounds.x = x;
|
||||
bounds.y = y;
|
||||
setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
//System.out.println("drawing anchor");
|
||||
Color color = getColor();
|
||||
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
|
||||
batch.draw(image, bounds.x - bounds.width/2, bounds.y - bounds.height/2, bounds.width, bounds.height);
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.jwinslow.game.ui;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.ButtonEffects;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
import com.jwinslow.game.utils.Fonts;
|
||||
|
||||
public class UIButton extends UIObject {
|
||||
|
||||
//--- Propreties
|
||||
private static float timer = 0;
|
||||
|
||||
private String message;
|
||||
private ButtonEffects buttonEffect;
|
||||
private TextureRegion[] images;
|
||||
private BitmapFont font;
|
||||
private float alpha;
|
||||
private boolean canBePressed;
|
||||
|
||||
//--- Constructor
|
||||
public UIButton(Handler handler, TextureRegion image, float x, float y, String message, ButtonEffects buttonEffect, float fontScale, boolean visible) {
|
||||
this(handler, image, x, y, message, buttonEffect, 1, fontScale, visible, true);
|
||||
}
|
||||
public UIButton(Handler handler, TextureRegion image, float x, float y, String message, ButtonEffects buttonEffect, float buttonScale, float fontScale, boolean visible, boolean canBePressed) {
|
||||
super(handler, image, x, y);
|
||||
this.bounds = new Rectangle(x, y, (Gdx.graphics.getWidth()*5/8)*buttonScale, (Gdx.graphics.getHeight()*5/32)*buttonScale);
|
||||
this.bounds.x = this.x-this.bounds.width/2;
|
||||
this.bounds.y = y - bounds.height/2;
|
||||
this.message = message;
|
||||
this.buttonEffect = buttonEffect;
|
||||
createImages();
|
||||
super.setVisible(visible);
|
||||
this.font = new BitmapFont(Gdx.files.internal(Assets.pixelFontFile1));//Fonts.buttonFont;
|
||||
font.getData().scale(fontScale);
|
||||
font.setColor(Color.BLACK);
|
||||
font.getData().down = -32 * (fontScale/2.5f);
|
||||
alpha = 1;
|
||||
this.canBePressed = canBePressed;
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if (timer > 0) timer -= dt;
|
||||
if (timer <= 0 && canBePressed && isVisible() && bounds.contains(Controls.jx, Controls.jy)) {
|
||||
ButtonEffects.update(buttonEffect);
|
||||
timer = 1;
|
||||
}
|
||||
if (canBePressed) {
|
||||
alpha = 1;
|
||||
} else alpha = 0.5f;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
batch.setColor(1, 1, 1, alpha);
|
||||
batch.draw(images[0], bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
font.draw(batch, message, x, y+bounds.height/2, 0, Align.center, true);
|
||||
batch.setColor(1, 1, 1, 1);
|
||||
}
|
||||
|
||||
private void createImages() {
|
||||
TextureRegion[][] t = image.split(image.getRegionWidth(), image.getRegionHeight()/3);
|
||||
images = new TextureRegion[t.length * t[0].length];
|
||||
for (int j = 0; j < t.length; j++) {
|
||||
for (int i = 0; i < t[j].length; i++) {
|
||||
images[j] = t[j][i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
font.dispose();
|
||||
remove();
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public String getMessage() {return message;}
|
||||
public ButtonEffects getButtonEffect() {return buttonEffect;}
|
||||
public boolean isCanBePressed() {return canBePressed;}
|
||||
|
||||
public void setMessage(String message) {this.message = message;}
|
||||
public void setButtonEffect(ButtonEffects buttonEffect) {this.buttonEffect = buttonEffect;}
|
||||
public void setCanBePressed(boolean pressed) {this.canBePressed = pressed;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.jwinslow.game.ui;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/6/2018.
|
||||
*/
|
||||
|
||||
public class UIDebug extends UIObject {
|
||||
|
||||
private BitmapFont font;
|
||||
public String msg;
|
||||
|
||||
public UIDebug(Handler handler) {
|
||||
super(handler, null, 0, Gdx.graphics.getHeight());
|
||||
font = new BitmapFont(Gdx.files.internal("customPixelFont1.fnt"));
|
||||
font.setColor(Color.RED);
|
||||
font.getData().scale(2);
|
||||
msg = "";
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if (getZIndex() < handler.getStage().getActors().size) {
|
||||
setZIndex(getZIndex()+1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch , float parentAlpha) {
|
||||
font.draw(batch, msg, x, y);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
font.dispose();
|
||||
remove();
|
||||
}
|
||||
|
||||
//Getters and Setters
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.jwinslow.game.ui;
|
||||
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
public abstract class UIObject extends Actor {
|
||||
|
||||
//--- Propreties
|
||||
protected Handler handler;
|
||||
protected TextureRegion image;
|
||||
protected float x, y;
|
||||
protected Rectangle bounds;
|
||||
|
||||
//--- Constructor
|
||||
public UIObject(Handler handler, TextureRegion image, float x, float y) {
|
||||
this.handler = handler;
|
||||
this.image = image;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public abstract void update(float dt);
|
||||
@Override
|
||||
public abstract void draw(Batch batch, float parentAlpha);
|
||||
|
||||
public boolean insideBounds(float x, float y) {
|
||||
return bounds.contains(x, y);
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public TextureRegion getImage() {return image;}
|
||||
@Override public float getX() {return x;}
|
||||
@Override public float getY() {return y;}
|
||||
public Rectangle getRectangle() {return bounds;}
|
||||
|
||||
public void setImage(TextureRegion image) {this.image = image;}
|
||||
@Override public void setX(float x) {this.x = x;}
|
||||
@Override public void setY(float y) {this.y = y;}
|
||||
public void setRectangle(Rectangle bounds) {this.bounds = bounds;}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.jwinslow.game.ui;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.utils.Assets;
|
||||
import com.jwinslow.game.utils.Controls;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/12/2018.
|
||||
*/
|
||||
|
||||
public class UITextArea extends UIObject{
|
||||
|
||||
public static final int MAX_CHAR_LIMIT = 256, MIN_CHAR_LIMIT = 0;
|
||||
|
||||
private String message;
|
||||
private boolean editable;
|
||||
private BitmapFont font;
|
||||
private float fontX, fontY;
|
||||
private int maxCharLimit, minCharLimit;
|
||||
private int alignment;
|
||||
private String title, defaultMessage, hint;
|
||||
|
||||
//Constructors
|
||||
public UITextArea(Handler handler, TextureRegion image, float x, float y, float widthScale, float heightScale, float fontScale, boolean editable) {
|
||||
this(handler, image, x, y, widthScale, heightScale, fontScale, MIN_CHAR_LIMIT, MAX_CHAR_LIMIT, Align.center, "", editable);
|
||||
}
|
||||
|
||||
public UITextArea(Handler handler, TextureRegion image, float x, float y, float widthScale, float heightScale,
|
||||
float fontScale, String message, boolean editable) {
|
||||
this(handler, image, x, y, widthScale, heightScale, fontScale, MIN_CHAR_LIMIT, MAX_CHAR_LIMIT, Align.center, message, editable);
|
||||
}
|
||||
|
||||
public UITextArea(Handler handler, TextureRegion image, float x, float y, float widthScale, float heightScale,
|
||||
float fontScale, int minCharLimit, int maxCharLimit, String message, boolean editable) {
|
||||
this(handler, image, x, y, widthScale, heightScale, fontScale, minCharLimit, maxCharLimit, Align.center, message, editable);
|
||||
}
|
||||
|
||||
public UITextArea(Handler handler, TextureRegion image, float x, float y, float widthScale, float heightScale,
|
||||
float fontScale, int minCharLimit, int maxCharLimit, int alignment, String message, boolean editable) {
|
||||
super(handler, image, x, y);
|
||||
bounds = new Rectangle(x, y, (Gdx.graphics.getWidth()/2)*widthScale, (Gdx.graphics.getHeight()/2)*heightScale);
|
||||
this.bounds.x = x - bounds.width/2;
|
||||
this.bounds.y = y - bounds.height/2;
|
||||
this.message = message;
|
||||
this.editable = editable;
|
||||
setVisible(true);
|
||||
|
||||
font = new BitmapFont(Gdx.files.internal(Assets.pixelFontFile1));
|
||||
font.getData().scale(fontScale);
|
||||
font.setColor(Color.BLACK);
|
||||
font.getData().down = -32 * (fontScale/2.5f);
|
||||
this.fontX = this.x;
|
||||
this.fontY = this.y+this.bounds.height/2;
|
||||
this.minCharLimit = minCharLimit;
|
||||
this.maxCharLimit = maxCharLimit;
|
||||
this.alignment = alignment;
|
||||
|
||||
this.title = "";
|
||||
this.defaultMessage = "";
|
||||
this.hint = "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
if (editable && !message.equalsIgnoreCase("") && !messageWithinLimits()) {
|
||||
message = "Invalid";
|
||||
}
|
||||
if (editable && bounds.contains(Controls.jx, Controls.jy)) {
|
||||
handler.getTil().setCurrentArea(this);
|
||||
Gdx.input.getTextInput(handler.getTil(), title, defaultMessage, hint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
batch.draw(image, bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
|
||||
font.draw(batch, message, fontX, fontY, 0, alignment, true);
|
||||
}
|
||||
|
||||
public boolean messageWithinLimits() {
|
||||
char[] msg = message.toCharArray();
|
||||
return (msg.length >= minCharLimit && msg.length <= maxCharLimit);
|
||||
}
|
||||
|
||||
public void dispose() {
|
||||
font.dispose();
|
||||
remove();
|
||||
}
|
||||
|
||||
//Getters and Setters
|
||||
public String getMessage() {return message;}
|
||||
public boolean isEditable() {return editable;}
|
||||
public float getFontX() {return fontX;}
|
||||
public float getFontY() {return fontY;}
|
||||
public int getMinCharLimit() {return minCharLimit;}
|
||||
public int getMaxCharLimit() {return maxCharLimit;}
|
||||
public int getAlignment() {return alignment;}
|
||||
public String getTitle() {return title;}
|
||||
public String getDefaultMessage() {return defaultMessage;}
|
||||
public String getHint() {return hint;}
|
||||
|
||||
public void setMessage(String message) {this.message = message;}
|
||||
public void setEditable(boolean editable) {this.editable = editable;}
|
||||
public void setFontX(float x) {this.fontX = x;}
|
||||
public void setFontY(float y) {this.fontY = y;}
|
||||
public void setMinCharLimit(int limit) {this.minCharLimit = limit;}
|
||||
public void setMaxCharLimit(int limit) {this.maxCharLimit = limit;}
|
||||
public void setAlignment(int align) {this.alignment = align;}
|
||||
public void setTitle(String title) {this.title = title;}
|
||||
public void setDefaultMessage(String message) {this.defaultMessage = message;}
|
||||
public void setHint(String hint) {this.hint = hint;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.jwinslow.game.utils;
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
|
||||
public class Assets {
|
||||
|
||||
//--- Propreties
|
||||
public static Texture player1;
|
||||
public static Texture player2;
|
||||
public static Texture touchAnchor;
|
||||
public static Texture background;
|
||||
public static Texture button;
|
||||
public static String pixelFontFile1;
|
||||
public static Texture pauseButton;
|
||||
public static Texture textArea;
|
||||
|
||||
//--- Constructor
|
||||
public Assets() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public static void loadAllTextures() {
|
||||
player1 = new Texture("b&w_DodgeGame_man1.png");
|
||||
player2 = new Texture("b&w_DodgeGame_woman1.png");
|
||||
touchAnchor = new Texture("touch_start.png");
|
||||
background = new Texture("b&w_DodgeGame_background2.png");
|
||||
button = new Texture("b&w_DodgeGame_buttons1.png");
|
||||
pixelFontFile1 = "customPixelFont1.fnt";
|
||||
pauseButton = new Texture("pauseButton.png");
|
||||
textArea = new Texture("b&w_textArea.png");
|
||||
}
|
||||
|
||||
public static void disposeAllTextures() {
|
||||
player1.dispose();
|
||||
player2.dispose();
|
||||
touchAnchor.dispose();
|
||||
background.dispose();
|
||||
button.dispose();
|
||||
pauseButton.dispose();
|
||||
textArea.dispose();
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.jwinslow.game.utils;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Button;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.net.Connection;
|
||||
import com.jwinslow.game.states.State;
|
||||
|
||||
public enum ButtonEffects {
|
||||
|
||||
//--- Propreties
|
||||
START,
|
||||
PLAY_GAME,
|
||||
START_GAME,
|
||||
NETWORK_SWITCH,
|
||||
EXIT_GAME,
|
||||
BACK,
|
||||
PAUSE_GAME,
|
||||
CONTINUE_GAME,
|
||||
MAIN_MENU,
|
||||
CONNECT;
|
||||
|
||||
|
||||
private static Handler handler;
|
||||
|
||||
|
||||
//--- Constructor
|
||||
|
||||
|
||||
//--- Methods
|
||||
public static void init(Handler handler) {
|
||||
ButtonEffects.handler = handler;
|
||||
}
|
||||
|
||||
public static void update(ButtonEffects buttonEffect) {
|
||||
switch (buttonEffect) {
|
||||
case START:
|
||||
handler.getMenuState().getStartGameButton().setVisible(false);
|
||||
handler.getMenuState().getExitGameButton().setVisible(false);
|
||||
handler.getMenuState().getBackButton().setVisible(true);
|
||||
handler.getMenuState().getChangeConnectionButton().setVisible(true);
|
||||
handler.getMenuState().getPlayGameButton().setVisible(true);
|
||||
break;
|
||||
case PLAY_GAME:
|
||||
if (Connection.udpServer != null) Connection.msgState = Connection.MStates.PLAY;
|
||||
State.setCurrentState(handler.getGameState());
|
||||
handler.getConnectState().dispose();
|
||||
State.getCurrentState().init();
|
||||
break;
|
||||
case START_GAME:
|
||||
String m2 = handler.getMenuState().getChangeConnectionButton().getMessage();
|
||||
int type = -1;
|
||||
State.setCurrentState(handler.getConnectState());
|
||||
if (m2.equalsIgnoreCase("Create\nUDP Server")) {type = 0;}
|
||||
else if (m2.equalsIgnoreCase("Create\nUDP Client")) {type = 1;}
|
||||
else if (m2.equalsIgnoreCase("Create\nTCP Server")) {type = 2;}
|
||||
else if (m2.equalsIgnoreCase("Create\nTCP Client")) {type = 3;}
|
||||
else {
|
||||
//State.setCurrentState(handler.getGameState());
|
||||
}
|
||||
handler.getConnectState().setConnectionType(type);
|
||||
handler.getMenuState().dispose();
|
||||
State.getCurrentState().init();
|
||||
break;
|
||||
case NETWORK_SWITCH:
|
||||
String s = handler.getMenuState().getChangeConnectionButton().getMessage();
|
||||
if (s.equalsIgnoreCase("Create\nUDP Server"))
|
||||
s = "Create\nUDP Client";
|
||||
else if (s.equalsIgnoreCase("Create\nUDP Client")) {
|
||||
s = "Create\nTCP Server";
|
||||
} else if (s.equalsIgnoreCase("Create\nTCP Server"))
|
||||
s = "Create\nTCP Client";
|
||||
else if (s.equalsIgnoreCase("Create\nTCP Client"))
|
||||
s = "Don't\nConnect";
|
||||
else if (s.equalsIgnoreCase("Don't\nConnect"))
|
||||
s = "Create\nUDP Server";
|
||||
handler.getMenuState().getChangeConnectionButton().setMessage(s);
|
||||
break;
|
||||
case EXIT_GAME:
|
||||
Gdx.app.exit();
|
||||
break;
|
||||
case BACK:
|
||||
handler.getMenuState().getStartGameButton().setVisible(true);
|
||||
handler.getMenuState().getExitGameButton().setVisible(true);
|
||||
handler.getMenuState().getBackButton().setVisible(false);
|
||||
handler.getMenuState().getChangeConnectionButton().setVisible(false);
|
||||
handler.getMenuState().getPlayGameButton().setVisible(false);
|
||||
break;
|
||||
case PAUSE_GAME:
|
||||
handler.getGameState().getPointer().setVisible(false);
|
||||
handler.getGameState().getPauseButton().setVisible((false));
|
||||
State.setCurrentState(handler.getPauseState());
|
||||
State.getCurrentState().init();
|
||||
break;
|
||||
case CONTINUE_GAME:
|
||||
handler.message = "";
|
||||
State.getCurrentState().dispose();
|
||||
handler.getGameState().getPointer().setVisible(true);
|
||||
handler.getGameState().getPauseButton().setVisible(true);
|
||||
State.setCurrentState(handler.getGameState());
|
||||
break;
|
||||
case MAIN_MENU:
|
||||
State.getCurrentState().dispose();
|
||||
handler.getGameObjectHandler().dispose();
|
||||
handler.getPlayers().clear();
|
||||
handler.getCamHandler().setObjOfFocus(null);
|
||||
handler.getCamHandler().reset();
|
||||
State.setCurrentState(handler.getMenuState());
|
||||
State.getCurrentState().init();
|
||||
break;
|
||||
case CONNECT:
|
||||
Connection.create(handler.getConnectState().getConnectionType());
|
||||
try {
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException ex) {ex.printStackTrace();}
|
||||
Connection.msgState = Connection.MStates.CREATE_PLAYER_FROM_CLIENT;
|
||||
handler.getConnectState().getPlayButton().setVisible(false);
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public static void setHandler(Handler handler) {ButtonEffects.handler = handler;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.jwinslow.game.utils;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.gameobjects.GameObject;
|
||||
|
||||
public class CameraHandler {
|
||||
|
||||
//--- Propreties
|
||||
public static final float CAM_SPEED = 1;
|
||||
|
||||
private float vecX, vecY;
|
||||
private Handler handler;
|
||||
private Rectangle bounds;
|
||||
private OrthographicCamera cam;
|
||||
private GameObject objOfFocus;
|
||||
|
||||
//--- Constructor
|
||||
public CameraHandler(Handler handler, OrthographicCamera cam) {
|
||||
this.handler = handler;
|
||||
this.cam = cam;
|
||||
bounds = new Rectangle(cam.position.x-cam.viewportWidth/2, cam.position.y-cam.viewportHeight/2,
|
||||
cam.viewportWidth, cam.viewportHeight);
|
||||
objOfFocus = null;
|
||||
}
|
||||
public CameraHandler(Handler handler, OrthographicCamera cam, GameObject objOfFocus) {
|
||||
this.handler = handler;
|
||||
this.cam = cam;
|
||||
bounds = new Rectangle(cam.position.x-cam.viewportWidth/2, cam.position.y-cam.viewportHeight/2,
|
||||
cam.viewportWidth, cam.viewportHeight);
|
||||
this.objOfFocus = objOfFocus;
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public void update(float dt) { //Actually updating the camera takes place in the Main class.
|
||||
updateFocus();
|
||||
bounds.x = cam.position.x-cam.viewportWidth/2;
|
||||
bounds.y = cam.position.y-cam.viewportHeight/2;
|
||||
outOfAreaCollision();
|
||||
if (Gdx.input.isKeyJustPressed(Keys.NUM_1)) printData();
|
||||
}
|
||||
|
||||
public void resetBounds() {
|
||||
bounds.x = cam.position.x-cam.viewportWidth/2;
|
||||
bounds.y = cam.position.y-cam.viewportHeight/2;
|
||||
bounds.width = cam.viewportWidth;
|
||||
bounds.height = cam.viewportHeight;
|
||||
}
|
||||
|
||||
public void resetBoundsPos() {
|
||||
bounds.x = cam.position.x-cam.viewportWidth/2;
|
||||
bounds.y = cam.position.y-cam.viewportHeight/2;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
cam.zoom = 1;
|
||||
if (objOfFocus != null) {
|
||||
cam.position.x = objOfFocus.getX();
|
||||
cam.position.y = objOfFocus.getY();
|
||||
} else {
|
||||
cam.position.x = Assets.background.getWidth()/2;
|
||||
cam.position.y = Assets.background.getHeight()/2;
|
||||
}
|
||||
}
|
||||
|
||||
private void updateFocus() {
|
||||
if (objOfFocus != null) {
|
||||
cam.position.x = objOfFocus.getX()+objOfFocus.getBounds().width/2;
|
||||
cam.position.y = objOfFocus.getY()+objOfFocus.getBounds().height/2;
|
||||
if (Gdx.input.isKeyJustPressed(Keys.ENTER)) objOfFocus = null;
|
||||
} else handleCam();
|
||||
}
|
||||
|
||||
private void handleCam() {
|
||||
|
||||
if (Gdx.input.isKeyPressed(Keys.I)) {vecY = CAM_SPEED;}
|
||||
if (Gdx.input.isKeyPressed(Keys.K)) {vecY = -CAM_SPEED;}
|
||||
if (Gdx.input.isKeyPressed(Keys.J)) {vecX = -CAM_SPEED;}
|
||||
if (Gdx.input.isKeyPressed(Keys.L)) {vecX = CAM_SPEED;}
|
||||
cam.translate(vecX, vecY);
|
||||
if (Gdx.input.isKeyPressed(Keys.U)) {cam.zoom -= 0.01; resetBounds();}
|
||||
if (Gdx.input.isKeyPressed(Keys.O)) {cam.zoom += 0.01; resetBounds();}
|
||||
if (Gdx.input.isKeyPressed(Keys.ENTER)) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
public void outOfAreaCollision() {
|
||||
if (bounds.x + bounds.width + CAM_SPEED > Assets.background.getWidth()) {
|
||||
cam.position.x = Assets.background.getWidth()-cam.viewportWidth/2;
|
||||
bounds.x = cam.position.x-cam.viewportWidth/2;
|
||||
}
|
||||
|
||||
if (bounds.x - CAM_SPEED < 0) {
|
||||
//System.out.println("Hello");
|
||||
cam.position.x = cam.viewportWidth/2;
|
||||
bounds.x = cam.position.x-cam.viewportWidth/2;
|
||||
}
|
||||
if (bounds.y + bounds.height + CAM_SPEED > Assets.background.getHeight()) {
|
||||
cam.position.y = Assets.background.getHeight()-cam.viewportHeight/2;
|
||||
bounds.y = cam.position.y-cam.viewportHeight/2;
|
||||
}
|
||||
|
||||
if (bounds.y - CAM_SPEED < 0) {
|
||||
cam.position.y = cam.viewportHeight/2;
|
||||
bounds.y = cam.position.y-cam.viewportHeight/2;
|
||||
}
|
||||
}
|
||||
|
||||
public void printData() {
|
||||
String message = "Camera:\n\tX: " + cam.position.x + " Y: " + cam.position.y +
|
||||
"\n\tWidth: " + cam.viewportWidth + " Height: " + cam.viewportHeight;
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public Rectangle getBounds() {return bounds;}
|
||||
public OrthographicCamera getCam() {return cam;}
|
||||
public GameObject getObjOfFocus() {return objOfFocus;}
|
||||
|
||||
public void setBounds(Rectangle bounds) {this.bounds = bounds;}
|
||||
public void setCam(OrthographicCamera cam) {this.cam = cam;}
|
||||
public void setObjOfFocus(GameObject obj) {this.objOfFocus = obj;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.jwinslow.game.utils;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.jwinslow.game.Main;
|
||||
|
||||
public class Controls {
|
||||
|
||||
//--- Propreties
|
||||
public static final int KEYBOARD = 0, TOUCH = 1;
|
||||
|
||||
private static int controlType;
|
||||
public static boolean up, down, left, right;
|
||||
private static int key_up, key_down, key_left, key_right;
|
||||
public static float sx, sy, ex, ey;
|
||||
public static float jx, jy;
|
||||
private static boolean touched;
|
||||
|
||||
//--- Constructor
|
||||
|
||||
//--- Methods
|
||||
public static void initControls() {
|
||||
key_up = Keys.W;
|
||||
key_down = Keys.S;
|
||||
key_left = Keys.A;
|
||||
key_right = Keys.D;
|
||||
}
|
||||
|
||||
public static void update() {
|
||||
switch (controlType) {
|
||||
case KEYBOARD:
|
||||
updateKeyboard();
|
||||
updateTouch();
|
||||
break;
|
||||
case TOUCH:
|
||||
updateTouch();
|
||||
break;
|
||||
default:
|
||||
controlType = KEYBOARD;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateKeyboard() {
|
||||
up = Gdx.input.isKeyPressed(key_up);
|
||||
down = Gdx.input.isKeyPressed(key_down);
|
||||
left = Gdx.input.isKeyPressed(key_left);
|
||||
right = Gdx.input.isKeyPressed(key_right);
|
||||
}
|
||||
|
||||
private static void updateTouch() {
|
||||
if (Gdx.input.isTouched()) {
|
||||
if (!touched) {
|
||||
sx = Gdx.input.getX();
|
||||
sy = Gdx.graphics.getHeight()-Gdx.input.getY();
|
||||
ex = sx;
|
||||
ey = sy;
|
||||
touched = true;
|
||||
} else {
|
||||
ex = Gdx.input.getX();
|
||||
ey = Gdx.graphics.getHeight()-Gdx.input.getY();
|
||||
}
|
||||
} else {
|
||||
sx = 0; sy = 0;
|
||||
ex = 0; ey = 0;
|
||||
touched = false;
|
||||
}
|
||||
|
||||
if (Gdx.input.justTouched()) {
|
||||
jx = Gdx.input.getX();
|
||||
jy = Gdx.graphics.getHeight()-Gdx.input.getY();
|
||||
} else {
|
||||
jx = -1;
|
||||
jy = -1;
|
||||
}
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
public static int getControllerType() {return controlType;}
|
||||
|
||||
public static void setControllerType(int controlType) {Controls.controlType = controlType;}
|
||||
public static void setKey_up(int key) {Controls.key_up = key;}
|
||||
public static void setKey_down(int key) {Controls.key_down = key;}
|
||||
public static void setKey_left(int key) {Controls.key_left = key;}
|
||||
public static void setKey_right(int key) {Controls.key_right = key;}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.jwinslow.game.utils;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.jwinslow.game.Handler;
|
||||
|
||||
public class Fonts {
|
||||
|
||||
//--- Propreties
|
||||
private static Handler handler;
|
||||
private static String message;
|
||||
|
||||
public static final BitmapFont
|
||||
font1 = new BitmapFont(Gdx.files.internal("customFont4.fnt")),
|
||||
pixelFont1 = new BitmapFont(Gdx.files.internal("customPixelFont1.fnt")),
|
||||
buttonFont = new BitmapFont(Gdx.files.internal("customPixelFont1.fnt"));
|
||||
|
||||
//--- Constructor
|
||||
public Fonts(Handler handler) {
|
||||
Fonts.handler = handler;
|
||||
}
|
||||
|
||||
|
||||
//--- Methods
|
||||
public static void init(Handler handler) {
|
||||
Fonts.handler = handler;
|
||||
message = "";
|
||||
//font1 = new BitmapFont(Gdx.files.internal("customFont4.fnt"));
|
||||
font1.setColor(Color.RED);
|
||||
pixelFont1.setColor(Color.RED);
|
||||
pixelFont1.getData().scale(1);
|
||||
|
||||
}
|
||||
|
||||
public static void write(Batch batch, BitmapFont font, String message, float x, float y) {
|
||||
font.draw(batch, message, x, y);
|
||||
}
|
||||
|
||||
public static void write(Batch batch, BitmapFont font, float x, float y) {
|
||||
font.draw(batch, message, x, y);
|
||||
}
|
||||
|
||||
public static void dispose() {
|
||||
font1.dispose();
|
||||
pixelFont1.dispose();
|
||||
}
|
||||
|
||||
//--- Getters and Setters
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.jwinslow.game.utils;
|
||||
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.jwinslow.game.Handler;
|
||||
import com.jwinslow.game.ui.UITextArea;
|
||||
|
||||
/**
|
||||
* Created by jalen on 1/12/2018.
|
||||
*/
|
||||
|
||||
public class TextInputListener implements Input.TextInputListener {
|
||||
|
||||
private Handler handler;
|
||||
private String input;
|
||||
private UITextArea currentArea;
|
||||
|
||||
public TextInputListener(Handler handler) {
|
||||
this.handler = handler;
|
||||
this.input = "";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void input(String text) {
|
||||
currentArea.setMessage(text);
|
||||
currentArea = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void canceled() {
|
||||
input = "";
|
||||
}
|
||||
|
||||
public String getInput() {return input;}
|
||||
public UITextArea getCurrentArea() {return currentArea;}
|
||||
|
||||
public void setCurrentArea(UITextArea ta) {this.currentArea = ta;}
|
||||
}
|
||||
Reference in New Issue
Block a user