Commit 1a6901c0 authored by Petter Selfors Rølvåg's avatar Petter Selfors Rølvåg
Browse files

The Return of the Template Pattern

parent c4df1844
......@@ -12,6 +12,7 @@ import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.controllers.NetworkController;
import com.mygdx.game.controllers.PlayerController;
import com.mygdx.game.views.CinematicView;
import com.mygdx.game.views.GameViewManager;
import com.mygdx.game.views.LoadingView;
import java.util.ArrayList;
......@@ -35,13 +36,10 @@ public class SpaceCheckersClient extends ApplicationAdapter {
playerController = new PlayerController(networkController);
// Initialize Views..
SpriteBatch sb = new SpriteBatch();
AssetManager assetManager = new AssetManager(new InternalFileHandleResolver());
Skin skin = new Skin(Gdx.files.internal("UISkins/glassy/skin/glassy-ui.json"));
skin.getFont("font").getData().setScale(1.4F);
gvm = new GameViewManager();
gvm.push(new LoadingView(gvm, playerController, assetManager, stage, skin));
}
......@@ -50,12 +48,6 @@ public class SpaceCheckersClient extends ApplicationAdapter {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
float dt = Gdx.graphics.getDeltaTime();
gvm.handleInput(dt);
gvm.update(dt);
gvm.render(dt);
if(gvm.getStartFadeIn() && !gvm.getStartFadeOut()) gvm.fadeIn(dt);
else if(gvm.getStartFadeOut() && !gvm.getStartFadeIn()) gvm.fadeOut(dt);
}
}
......@@ -22,7 +22,7 @@ public class NetworkController {
this.client = new Client();
client.start();
try {
String IP4_LAN_ADDRESS = "192.168.1.65";
String IP4_LAN_ADDRESS = "192.168.87.23";
client.connect(10000, IP4_LAN_ADDRESS, 54555);
} catch (IOException e) {
......
package com.mygdx.game.singletons
class AssetLoader {
}
\ No newline at end of file
......@@ -16,6 +16,8 @@ import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.mygdx.game.controllers.PlayerController;
import javax.swing.text.View;
public abstract class AbstractView extends ApplicationAdapter {
......@@ -27,7 +29,6 @@ public abstract class AbstractView extends ApplicationAdapter {
protected final Stage stage;
protected Skin skin;
protected final int Help_Guides = 12;
protected final int row_height = Gdx.graphics.getHeight() / Help_Guides;
protected final int col_width = Gdx.graphics.getWidth() / Help_Guides;
......@@ -66,19 +67,53 @@ public abstract class AbstractView extends ApplicationAdapter {
fadeOverlayTexture = new Texture(tablePixmap);
}
public abstract void handleInput(float dt);
public abstract void update(float dt);
public abstract void render(float dt);
public abstract void fadeIn(float dt);
public abstract void fadeOut(float dt);
public abstract void dispose();
public abstract void setState();
// Override when necessary
protected void handleInput(float dt) {
public boolean getStartFadeIn(){
return startFadeIn;
}
protected void render(float dt) {
Gdx.gl.glClearColor(1, 1, 1, 1);
handleInput(dt);
update(dt);
stage.draw();
if(getStartFadeIn() && !getStartFadeOut()) fadeIn(dt);
else if(getStartFadeOut() && !getStartFadeIn()) fadeOut(dt);
}
public boolean getStartFadeOut(){
return startFadeout;
protected void fadeIn(float dt) {
stage.getBatch().begin();
Color c = Color.WHITE;
stage.getBatch().setColor(c.r, c.g, c.b, fadeInAlpha);
fadeInAlpha -= dt/fadeInLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().end();
if(fadeInAlpha <= 0){
startFadeIn = false;
}
}
protected void fadeOut(float dt) {
stage.getBatch().begin();
Color c = Color.WHITE;
stage.getBatch().setColor(c.r, c.g, c.b, fadeOutAlpha);
fadeOutAlpha += dt/fadeOutLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().setColor(c);
stage.getBatch().end();
if(fadeOutAlpha >= 1){
stage.clear();
startFadeout = false;
setState();
}
}
@Override
public void dispose() {
stage.dispose();
}
protected void setFadeColor(Color c){
......@@ -87,6 +122,12 @@ public abstract class AbstractView extends ApplicationAdapter {
tablePixmap.fill();
fadeOverlayTexture = new Texture(tablePixmap);
}
public boolean getStartFadeIn(){
return startFadeIn;
}
public boolean getStartFadeOut(){
return startFadeout;
}
protected void drawGrid(){
if(shapeRenderer == null){
......
......@@ -40,12 +40,14 @@ public class CinematicView extends AbstractView {
private final float keyframe3;
private final float keyframe4;
private final Music cinematicSound;
private float fadeInVolume;
private final AnimatedSprite cloudSprite;
private float updateSpritesTimer;
private float fadeInVolume;
private float timer = 0;
private final Camera cam;
private float cloudSpriteAlpha = 1;
boolean onlyOnce1 = true;
boolean onlyOnce2 = true;
boolean onlyOnce3 = true;
......@@ -105,58 +107,9 @@ public class CinematicView extends AbstractView {
keyframe4 = 3.3f*1.92f;
}
public CinematicView(GameViewManager gvm, PlayerController playerController, AssetManager assetManager, Stage stage, Skin skin){
this(gvm, playerController, assetManager, stage, skin, new ArrayList<AnimatedSprite>());
AnimatedSprite asianGirl = new AnimatedSprite(
(TextureAtlas) assetManager.get(CharacterAssets.AsianGirlAtlas.path, CharacterAssets.AsianGirlAtlas.classType),
new TextureRegion ( (Texture) assetManager.get(MenuAssets.RED_CIRCLE_SMALL.path, MenuAssets.RED_CIRCLE_SMALL.classType)),
"AsianGirl",
3);
AnimatedSprite regularGuy = new AnimatedSprite(
(TextureAtlas) assetManager.get(CharacterAssets.RegularGuyAtlas.path, CharacterAssets.RegularGuyAtlas.classType),
new TextureRegion ( (Texture) assetManager.get(MenuAssets.RED_CIRCLE_SMALL.path, MenuAssets.RED_CIRCLE_SMALL.classType)),
"RegularGuy",
3);
int xPad = 10;
int remX = 0;
int remY = 0;
asianGirl.setPosition(lobbyImage.getX()-xPad+remX*((lobbyImage.getWidth()-xPad)/4), lobbyImage.getY()-remY*(lobbyImage.getHeight()/7.5f));
asianGirl.setScale(0.3f+0.025f*remY, 0.3f+0.025f*remY);
asianGirl.setOrigin(0, 0);
remX = 1;
remY = 0;
regularGuy.setPosition(lobbyImage.getX()-xPad+remX*((lobbyImage.getWidth()-xPad)/4), lobbyImage.getY()-remY*(lobbyImage.getHeight()/7.5f));
regularGuy.setScale(0.3f+0.025f*remY, 0.3f+0.025f*remY);
regularGuy.setOrigin(0, 0);
lobbyAvatars.add(asianGirl);
lobbyAvatars.add(regularGuy);
stage.addActor(backgroundImage);
stage.addActor(lobbyImage);
stage.addActor(exhaustImage);
stage.addActor(rocketImage);
}
@Override
public void handleInput(float dt) {
}
float cloudSpriteAlpha = 1;
@Override
public void update(float dt) {
if(startFadeIn){
}
else{
if(!startFadeIn){
updateSpritesTimer+= dt;
float updateAvatarFrequency = 0.120f;
if(0<= timer && timer <= keyframe1){
......@@ -214,20 +167,33 @@ public class CinematicView extends AbstractView {
}
}
@Override
public void setState() {
// Initialize model and set PlayView
Game model = new Game(new GameState(new GameMode(new DefaultRules(), new DefaultBoard())), playerController.getLobby().getPlayersID());
PlayView playView = new PlayView(gvm, playerController, assetManager, stage, skin, lobbyAvatars, model.getBoardImagePath());
GameController gameController = new GameController(model, playView, playerController);
playerController.setGameController(gameController);
playView.setGameController(gameController);
gvm.set(playView);
}
@Override
public void render(float dt) {
update(dt);
stage.draw();
stage.getBatch().begin();
stage.getBatch().begin();
for(AnimatedSprite animatedSprite : lobbyAvatars){
animatedSprite.draw(stage.getBatch());
}
countDownAnimationSprite.draw(stage.getBatch());
cloudSprite.draw(stage.getBatch());
stage.getBatch().end();
if(getStartFadeIn() && !getStartFadeOut()) fadeIn(dt);
else if(getStartFadeOut() && !getStartFadeIn()) fadeOut(dt);
}
@Override
......@@ -244,37 +210,9 @@ public class CinematicView extends AbstractView {
countDownAnimationSprite.setAlpha(1);
cinematicSound.play();
startFadeIn = false;
}
fadeInVolume += dt/fadeOutLength;
}
@Override
public void fadeOut(float dt) {
stage.getBatch().begin();
Color c = stage.getBatch().getColor();
stage.getBatch().setColor(c.r, c.g, c.b, fadeOutAlpha);
fadeOutAlpha += dt/fadeOutLength;
stage.getBatch().draw(fadeOverlayTexture, cam.position.x-cam.viewportWidth/2, cam.position.y-cam.viewportHeight/2, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().setColor(c);
stage.getBatch().end();
if(fadeOutAlpha >= 1){
stage.clear();
startFadeout = false;
// Initialize model and set PlayView
Game model = new Game(new GameState(new GameMode(new DefaultRules(), new DefaultBoard())), playerController.getLobby().getPlayersID());
PlayView playView = new PlayView(gvm, playerController, assetManager, stage, skin, lobbyAvatars, model.getBoardImagePath());
GameController gameController = new GameController(model, playView, playerController);
playerController.setGameController(gameController);
playView.setGameController(gameController);
gvm.set(playView);
}
fadeInVolume += dt/fadeOutLength;
}
@Override
public void dispose() {
stage.dispose();
}
}
......@@ -24,32 +24,7 @@ public class GameViewManager {
views.push(abstractView);
}
public void handleInput(float dt){
views.peek().handleInput(dt);
}
public void update(float dt){
views.peek().update(dt);
}
public void render(float dt){
views.peek().render(dt);
}
public void fadeIn(float dt){
views.peek().fadeIn(dt);
}
public void fadeOut(float dt){
views.peek().fadeOut(dt);
}
public boolean getStartFadeIn(){
return views.peek().getStartFadeIn();
}
public boolean getStartFadeOut(){
return views.peek().getStartFadeOut();
}
}
......@@ -2,6 +2,7 @@ package com.mygdx.game.views;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
......@@ -41,10 +42,6 @@ public class LoadingView extends AbstractView {
}
}
@Override
public void handleInput(float dt) {
}
@Override
public void update(float dt) {
......@@ -57,42 +54,7 @@ public class LoadingView extends AbstractView {
}
@Override
public void render(float dt) {
stage.draw();
}
@Override
public void fadeIn(float dt) {
stage.getBatch().begin();
Color c = Color.WHITE;
stage.getBatch().setColor(c.r, c.g, c.b, fadeInAlpha);
fadeInAlpha -= dt/fadeInLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().end();
if(fadeInAlpha <= 0){
startFadeIn = false;
}
}
@Override
public void fadeOut(float dt) {
stage.getBatch().begin();
Color c = Color.WHITE;
stage.getBatch().setColor(c.r, c.g, c.b, fadeOutAlpha);
fadeOutAlpha += dt/fadeOutLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().setColor(c);
stage.getBatch().end();
if(fadeOutAlpha >= 1){
stage.clear();
startFadeout = false;
gvm.set(new MenuView(gvm, playerController, assetManager, stage, skin));
}
}
@Override
public void dispose() {
stage.dispose();
public void setState() {
gvm.set(new MenuView(gvm, playerController, assetManager, stage, skin));
}
}
......@@ -55,7 +55,6 @@ public class LobbyView extends AbstractView {
private float updateAvatarTimer = 0;
private float refreshLobbyTimer = 1;
private float fadeOutVolume = 0;
private final Image backgroundImage;
private final AnimatedSprite mrServer;
......@@ -99,7 +98,6 @@ public class LobbyView extends AbstractView {
} else {
this.currentSong = currentSong;
}
fadeOutVolume = this.currentSong.getVolume();
buttonClickAudio = (Sound) assetManager.get(MenuAssets.BUTTON_CLICK_AUDIO.path, MenuAssets.BUTTON_CLICK_AUDIO.classType);
this.camera = stage.getCamera();
......@@ -208,7 +206,6 @@ public class LobbyView extends AbstractView {
backgroundImage.setOrigin(0,0);
stage.addActor(backgroundImage);
stage.addActor(outputLabel);
stage.addActor(rocketImage);
stage.addActor(exhaustImage);
stage.addActor(lobbyImage);
......@@ -556,7 +553,7 @@ public class LobbyView extends AbstractView {
Label label = new Label("Player"+i+":\t" + player.getPlayerName() + "\t", skin, "black");
label.setColor(Color.BLACK);
label.scaleBy(2f);
label.setPosition(lobbyListBubbleImage.getX()+xpad, lobbyListBubbleImage.getY()+lobbyListBubbleImage.getHeight() - (i + 1) * (label.getHeight() * 2.5F)); //(0.9f-label.getHeight()*i-i*0.05f));
label.setPosition(lobbyListBubbleImage.getX()+xpad, lobbyListBubbleImage.getY()+lobbyListBubbleImage.getHeight() - (i + 1) * (label.getHeight() * 2.5F));
stage.addActor(label);
lobbyLabels.add(label);
......@@ -608,15 +605,13 @@ public class LobbyView extends AbstractView {
}
}
@Override
public void dispose() {
stage.dispose();
}
@Override
public void render(float dt) {
Gdx.gl.glClearColor(1, 1, 1, 1);
handleInput(dt);
update(dt);
stage.draw();
stage.getBatch().begin();
......@@ -626,42 +621,9 @@ public class LobbyView extends AbstractView {
mrServer.draw(stage.getBatch());
stage.getBatch().end();
}
@Override
public void fadeIn(float dt) {
stage.getBatch().begin();
Color c = stage.getBatch().getColor();
stage.getBatch().setColor(c.r, c.g, c.b, fadeInAlpha);
fadeInAlpha -= dt/fadeInLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().setColor(c);
stage.getBatch().end();
if(fadeInAlpha <= 0){
startFadeIn = false;
}
}
@Override
public void fadeOut(float dt) {
stage.getBatch().begin();
Color c = stage.getBatch().getColor();
stage.getBatch().setColor(c.r, c.g, c.b, fadeOutAlpha);
fadeOutAlpha += dt/fadeOutLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().setColor(c);
stage.getBatch().end();
if(fadeOutAlpha >= 1){
currentSong.stop();
stage.clear();
startFadeout = false;
setFadeColor(Color.BLACK);
gvm.set(new CinematicView(gvm, playerController, assetManager, stage, skin, lobbyAvatars));
}
fadeOutVolume -= dt/fadeOutLength;
currentSong.setVolume(fadeOutVolume);
if(getStartFadeIn() && !getStartFadeOut()) fadeIn(dt);
else if(getStartFadeOut() && !getStartFadeIn()) fadeOut(dt);
}
@Override
......@@ -672,7 +634,7 @@ public class LobbyView extends AbstractView {
}
if(Gdx.input.isTouched()){
float touchSensitivity = 0.1f;
float camXPos = camera.position.x + (lastTouch[0]-Gdx.input.getX())* touchSensitivity; //camera.position.x + (Gdx.input.getX()-lastTouch[0])*sensitivity;
float camXPos = camera.position.x + (lastTouch[0]-Gdx.input.getX())* touchSensitivity;
float camYPos = camera.position.y + (-stage.getViewport().getScreenHeight()+Gdx.input.getY()+lastTouch[1])* touchSensitivity;
float a = MathUtils.clamp(camXPos, camera.viewportWidth/2, backgroundImage.getWidth()-camera.viewportWidth/2);
......@@ -685,13 +647,10 @@ public class LobbyView extends AbstractView {
@Override
public void update(float dt) {
stage.getCamera().update();
assetManager.update();
updateAvatarTimer+= dt;
refreshLobbyTimer += dt;
//update every 250ms
float updateAvatarFrequency = 0.120f;
if(updateAvatarTimer-1F/Gdx.graphics.getFramesPerSecond()/2F>= updateAvatarFrequency){
for(AnimatedSprite animatedSprite : lobbyAvatars){
......@@ -712,4 +671,11 @@ public class LobbyView extends AbstractView {
startFadeout = true;
}
}
@Override
public void setState() {
currentSong.stop();
setFadeColor(Color.BLACK);
gvm.set(new CinematicView(gvm, playerController, assetManager, stage, skin, lobbyAvatars));
}
}
......@@ -60,7 +60,6 @@ public class MenuView extends AbstractView {
for(LobbyAssets asset : LobbyAssets.values()) {
assetManager.load(asset.path, asset.classType);
}
shapeRenderer = new ShapeRenderer(); // Used for draw debugging
availableAvatarsHashMap = Constants.AVAILABLEAVATARSHASHMAP;
......@@ -165,6 +164,7 @@ public class MenuView extends AbstractView {
outputLabel.setText("Please enter a valid name. Has to be between 3 and 10 characters.");
}
return true;
}
});
usernameTextField.addListener(new InputListener(){
......@@ -183,6 +183,7 @@ public class MenuView extends AbstractView {
}
}
});
stage.addActor(backgroundImage);
stage.addActor(connectionBarImage);
stage.addActor(connectionBarRedCircle);
......@@ -206,9 +207,6 @@ public class MenuView extends AbstractView {
playerController.updateIndexAvatar(currentIndexAvatar);
startFadeout = true;
}
else{
outputLabel.setText("Please enter a valid name. Has to be between 3 and 10 characters.");
}
}
}
......@@ -218,58 +216,19 @@ public class MenuView extends AbstractView {
));
}
@Override
public void update(float dt) {
assetManager.update();
stage.act(dt);
}
@Override
public void render(float dt) {
Gdx.gl.glClearColor(1, 1, 1, 1);
if(playerController.getPlayer().getID() != -1){
stage.addActor(connectionBarGreenCircle);
connectionBarRedCircle.remove();
}
stage.draw();
}
@Override
public void fadeIn(float dt) {
stage.getBatch().begin();
Color c = Color.WHITE;
stage.getBatch().setColor(c.r, c.g, c.b, fadeInAlpha);
fadeInAlpha -= dt/fadeInLength;
stage.getBatch().draw(fadeOverlayTexture, 0, 0, col_width*Help_Guides, row_height*Help_Guides);
stage.getBatch().setColor(c);