Skip to content

Commit

Permalink
Websocket update
Browse files Browse the repository at this point in the history
  • Loading branch information
Liza12345677564 committed Mar 27, 2024
1 parent 678151c commit bc228b6
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 146 deletions.
63 changes: 29 additions & 34 deletions core/src/com/aqwsxlostfly/packandgo/Main.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.aqwsxlostfly.packandgo;

import com.aqwsxlostfly.packandgo.Screens.GameSc;

import com.aqwsxlostfly.packandgo.Screens.WaitingSc;
import com.aqwsxlostfly.packandgo.client.ws.NewWebSocket;
import com.aqwsxlostfly.packandgo.client.ws.WebSocketClient;
import com.aqwsxlostfly.packandgo.client.ws.WebSocketListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.nio.charset.StandardCharsets;

public class Main extends Game {

public static WebSocketClient webSocketClient;

public static NewWebSocket newWebSocket;
public NewWebSocket webSocketClient;

public static SpriteBatch batch;
public static Texture img;
Expand All @@ -29,48 +30,45 @@ public class Main extends Game {
public static int screenHeight;
public static int record;

private void getWebsocket(){
WebSocketClient webSocketClient = new WebSocketClient("192.168.170.252", 8867);

webSocketClient.addWebSocketListener(new WebSocketListener() {
private WebSocketListener getWebsocketListener(){
WebSocketListener webSocketListener = new WebSocketListener() {
@Override
public void onMessageReceived(String message) {
Gdx.app.log("MESSAGE RECEIVED", message);
Gdx.app.log("MESSAGE RECEIVED",message);
}

@Override
public void onConnect(ServerHandshake handshake) {
Gdx.app.log("CONNECT","OnConnect");

}

@Override
public void onConnect(String message) {
Gdx.app.log("CONNECTION CREATED", message);
public void onClose(int code, String reason) {
Gdx.app.log("CLOSE CONNECT","onClose reason: "+ reason);

}

@Override
public void onClose(String message) {
Gdx.app.log("CONNECTION CLOSED", message);
public void onError(Exception ex) {
Gdx.app.error("ERROR CONNECT","onError: " + ex.getMessage());
}
});
};

Main.webSocketClient = webSocketClient;
return webSocketListener;
}

@Override
public void create () {
// try {
// getWebsocket();
//
// webSocketClient.connect();
//
// webSocketClient.sendMessage("HELLO SERVER");
//
// } catch (Exception e){
// Gdx.app.error("CONNECTION FAILED", e.getMessage());
// }

try {
String wsUri = "ws://192.168.170.252:8867/ws";
NewWebSocket webSocketClient = new NewWebSocket(new URI(wsUri));
Gdx.app.log("INFO","CONNECT PROCESS");
String wsUri = "ws://192.168.113.145:8867/ws";
webSocketClient = new NewWebSocket(new URI(wsUri), getWebsocketListener());
webSocketClient.connect();
webSocketClient.send("CONNECT WAITING SCREEN");
} catch (Exception e) {
e.printStackTrace();

Gdx.app.error("ERROR SOCKET CONNECT","ERROR" + e.getMessage());
}

batch = new SpriteBatch();
Expand All @@ -86,7 +84,7 @@ public void create () {
record=read();
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
setScreen(new GameSc(this));
setScreen(new WaitingSc(this));

}

Expand All @@ -95,9 +93,6 @@ public void create () {
public void dispose () {
batch.dispose();
img.dispose();
if (webSocketClient != null) {
webSocketClient.close();
}
}
public static void write(String str){
FileHandle file = Gdx.files.local("rec.txt");
Expand Down
3 changes: 3 additions & 0 deletions core/src/com/aqwsxlostfly/packandgo/Screens/GameSc.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class GameSc implements Screen {
public static Player player;
public static ArrayList<Bullet> bullets;
public static ArrayList<Enemy> enemies;
public static ArrayList<Player> players;
public static Wave wave;


Expand Down Expand Up @@ -135,11 +136,13 @@ public void loadHeroes(){
joy = new Joystick(Main.circle, Main.capibara, new Point2D(((Main.screenHeight/3)/2+(Main.screenHeight/3)/4), (Main.screenHeight/3)/2+(Main.screenHeight/3)/4), Main.screenHeight/3);
joyBullet = new Joystick(Main.circle, Main.capibara, new Point2D(Main.screenWidth-((Main.screenHeight/3)/2+(Main.screenHeight/3)/4), (Main.screenHeight/3)/2+(Main.screenHeight/3)/4), Main.screenHeight/3);
player = new Player(Main.capibara, new Point2D(Main.screenWidth/2, Main.screenHeight/2), 10F, (float) (Main.screenHeight/5), 5);

bullets = new ArrayList<Bullet>();
enemies = new ArrayList<Enemy>();
wave = new Wave(4,1,5);
bulletGenerator= new BulletGenerator();
hud = new GameHud();

}

public void multitouch(float x, float y, boolean isDownTouch, int pointer){
Expand Down
200 changes: 200 additions & 0 deletions core/src/com/aqwsxlostfly/packandgo/Screens/WaitingSc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package com.aqwsxlostfly.packandgo.Screens;

import com.aqwsxlostfly.packandgo.Heroes.Bullet;
import com.aqwsxlostfly.packandgo.Heroes.Enemy;
import com.aqwsxlostfly.packandgo.Heroes.Player;
import com.aqwsxlostfly.packandgo.Main;
import com.aqwsxlostfly.packandgo.Tools.BulletGenerator;
import com.aqwsxlostfly.packandgo.Tools.GameHud;
import com.aqwsxlostfly.packandgo.Tools.Joystick;
import com.aqwsxlostfly.packandgo.Tools.Point2D;
import com.aqwsxlostfly.packandgo.Tools.Wave;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;

import java.util.ArrayList;

public class WaitingSc implements Screen {


Main main;
Joystick joy;
Joystick joyBullet;
public static Player player;
public static ArrayList<Bullet> bullets;

public static ArrayList<Player> players;

BitmapFont font;
GlyphLayout textWaiting, textJoin;

public static BulletGenerator bulletGenerator;

public WaitingSc(Main main){
this.main = main;
FreeTypeFontGenerator gen = new FreeTypeFontGenerator((Gdx.files.internal(("font.ttf"))));
FreeTypeFontGenerator.FreeTypeFontParameter p = new FreeTypeFontGenerator.FreeTypeFontParameter();
p.size = Main.screenWidth / 30;
p.color = new Color(Color.RED);
font = gen.generateFont(p);
textWaiting = new GlyphLayout();
textWaiting.setText(font, "WAITING OTHER PLAYER");
textJoin = new GlyphLayout();
textJoin.setText(font, "PLAYER HAS JOINED");
}
@Override
public void show() {


Gdx.input.setInputProcessor(new InputProcessor() {
@Override
public boolean keyDown(int keycode) {
return false;
}

@Override
public boolean keyUp(int keycode) {
return false;
}

@Override
public boolean keyTyped(char character) {
return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
screenY = Main.screenHeight - screenY;
multitouch((int)screenX, (int)screenY, true, pointer);
return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenY = Main.screenHeight - screenY;
multitouch((int)screenX, (int)screenY, false, pointer);
return false;
}

@Override
public boolean touchCancelled(int screenX, int screenY, int pointer, int button) {
screenY = Main.screenHeight - screenY;
multitouch((int)screenX, (int)screenY, false, pointer);
return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
screenY = Main.screenHeight - screenY;
multitouch((int)screenX, (int)screenY, true, pointer);
return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}

@Override
public boolean scrolled(float amountX, float amountY) {
return false;
}
});

loadHeroes();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameUpdate();
Main.batch.begin();

gameRender(Main.batch);
Main.batch.end();


}
@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}

public void loadHeroes() {
joy = new Joystick(Main.circle, Main.capibara, new Point2D(((Main.screenHeight / 3) / 2 + (Main.screenHeight / 3) / 4), (Main.screenHeight / 3) / 2 + (Main.screenHeight / 3) / 4), Main.screenHeight / 3);
joyBullet = new Joystick(Main.circle, Main.capibara, new Point2D(Main.screenWidth - ((Main.screenHeight / 3) / 2 + (Main.screenHeight / 3) / 4), (Main.screenHeight / 3) / 2 + (Main.screenHeight / 3) / 4), Main.screenHeight / 3);
player = new Player(Main.capibara, new Point2D(Main.screenWidth / 2, Main.screenHeight / 2), 10F, (float) (Main.screenHeight / 5), 5);
players = new ArrayList<Player>();
players.add(player);
bullets = new ArrayList<Bullet>();
bulletGenerator = new BulletGenerator();
}

public void multitouch(float x, float y, boolean isDownTouch, int pointer) {
for (int i = 0; i < 3; i++) {
joy.update(x, y, isDownTouch, pointer);
joyBullet.update(x, y, isDownTouch, pointer);
}
}

public void gameUpdate() {

player.setDirection(joy.getDir());
for (int i = 0; i < players.size(); i++) {
players.get(i).update();
}



bulletGenerator.update(joyBullet);
for (int i = 0; i < bullets.size(); i++) {
bullets.get(i).update();
if (bullets.get(i).isOut) {
bullets.remove(i--);
}
}
}

public void gameRender(SpriteBatch batch) {
if (players.size() < 2) {
font.draw(batch, textWaiting, Main.screenWidth / 2 - textWaiting.width / 2, Main.screenHeight -Main.screenHeight/10);
} else {
font.draw(batch, textJoin, Main.screenWidth / 2 - textWaiting.width / 2, Main.screenHeight -Main.screenHeight/10 );
}
for (int i = 0; i < players.size(); i++) {
players.get(i).draw(batch);
}
joy.draw(batch);
joyBullet.draw(batch);
for (int i = bullets.size() - 1; i >= 0; i--) {
bullets.get(i).draw(batch);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.aqwsxlostfly.packandgo.Heroes.Bullet;
import com.aqwsxlostfly.packandgo.Main;
import com.aqwsxlostfly.packandgo.Screens.GameSc;
import com.aqwsxlostfly.packandgo.Screens.WaitingSc;

public class BulletGenerator {
boolean isFire;

public void update(Joystick joy) {
isFire = joy.getDir().getX() != 0 || joy.getDir().getY() != 0;
if (isFire) GameSc.bullets.add(new Bullet(Main.bullet, GameSc.player.position,
25, GameSc.player.radius / 8, joy.getDir()));
if (isFire) WaitingSc.bullets.add(new Bullet(Main.bullet, WaitingSc.player.position,
25, WaitingSc.player.radius / 8, joy.getDir()));


}
Expand Down
13 changes: 10 additions & 3 deletions core/src/com/aqwsxlostfly/packandgo/client/ws/NewWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,35 @@
import java.net.URI;

public class NewWebSocket extends WebSocketClient {
private WebSocketListener webSocketListener;

public NewWebSocket(URI serverUri, WebSocketListener webSocketListener) {

public NewWebSocket(URI serverUri) {
super(serverUri);
this.webSocketListener = webSocketListener;

}

@Override
public void onOpen(ServerHandshake handshake) {
Gdx.app.log("CONNECTION CREATED", "kdsnjv");
webSocketListener.onConnect(handshake);
Gdx.app.log("CONNECTION CREATED", "On connect");
}

@Override
public void onClose(int code, String reason, boolean remote) {
webSocketListener.onClose(code,reason);
Gdx.app.log("CONNECTION CLOSED", " code: " + code + " reason: " + reason);
}

@Override
public void onMessage(String message) {
webSocketListener.onMessageReceived(message);
Gdx.app.log("MESSAGE RECEIVED", message);
}

@Override
public void onError(Exception ex) {
ex.printStackTrace();
webSocketListener.onError(ex);
}
}
Loading

0 comments on commit bc228b6

Please sign in to comment.