Refactored almost every class, somehow didn't break game yet

This commit is contained in:
Anuken
2018-01-27 23:42:42 -05:00
parent 78c8dc4902
commit 35b6b41f24
110 changed files with 1648 additions and 1463 deletions

View File

@@ -4,14 +4,12 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.ObjectMap;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.net.Packet.ImportantPacket;
import io.anuke.mindustry.net.Packets.KickReason;
import io.anuke.mindustry.net.Streamable.StreamBegin;
import io.anuke.mindustry.net.Streamable.StreamBuilder;
import io.anuke.mindustry.net.Streamable.StreamChunk;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.function.BiConsumer;
import io.anuke.ucore.function.Consumer;
@@ -23,6 +21,7 @@ public class Net{
private static boolean server;
private static boolean active;
private static boolean clientLoaded;
private static ObjectMap<Class<?>, Consumer> listeners = new ObjectMap<>();
private static ObjectMap<Class<?>, Consumer> clientListeners = new ObjectMap<>();
private static ObjectMap<Class<?>, BiConsumer<Integer, Object>> serverListeners = new ObjectMap<>();
private static ClientProvider clientProvider;
@@ -40,8 +39,6 @@ public class Net{
clientProvider.connect(ip, port);
active = true;
server = false;
Timers.runTask(60f, Mindustry.platforms::updateRPC);
}
/**Host a server at an address*/
@@ -49,8 +46,6 @@ public class Net{
serverProvider.host(port);
active = true;
server = true;
Timers.runTask(60f, Mindustry.platforms::updateRPC);
}
/**Closes the server.*/
@@ -115,9 +110,14 @@ public class Net{
public static void setServerProvider(ServerProvider provider){
Net.serverProvider = provider;
}
/**Registers a client listener for when an object is recieved.*/
/**Registers a common listener for when an object is recieved. Fired on both client and serve.r*/
public static <T> void handle(Class<T> type, Consumer<T> listener){
listeners.put(type, listener);
}
/**Registers a client listener for when an object is recieved.*/
public static <T> void handleClient(Class<T> type, Consumer<T> listener){
clientListeners.put(type, listener);
}
@@ -142,9 +142,11 @@ public class Net{
streams.remove(builder.id);
handleClientReceived(builder.build());
}
}else if(clientListeners.get(object.getClass()) != null){
}else if(clientListeners.get(object.getClass()) != null ||
listeners.get(object.getClass()) != null){
if(clientLoaded || object instanceof ImportantPacket){
clientListeners.get(object.getClass()).accept(object);
if(clientListeners.get(object.getClass()) != null) clientListeners.get(object.getClass()).accept(object);
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);
}else{
UCore.log("Recieved " + object, "but ignoring data, as client is not loaded.");
}
@@ -155,8 +157,9 @@ public class Net{
/**Call to handle a packet being recieved for the server.*/
public static void handleServerReceived(int connection, Object object){
if(serverListeners.get(object.getClass()) != null){
serverListeners.get(object.getClass()).accept(connection, object);
if(serverListeners.get(object.getClass()) != null || listeners.get(object.getClass()) != null){
if(serverListeners.get(object.getClass()) != null) serverListeners.get(object.getClass()).accept(connection, object);
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);
}else{
Gdx.app.error("Mindustry::Net", "Unhandled packet type: '" + object.getClass() + "'!");
}
@@ -184,12 +187,12 @@ public class Net{
/**Whether this is a server or not.*/
public static boolean server(){
return server;
return server && active;
}
/**Whether this is a client or not.*/
public static boolean client(){
return !server;
return !server && active;
}
public static void dispose(){

View File

@@ -0,0 +1,135 @@
package io.anuke.mindustry.net;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.enemies.Enemy;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.entities.Entity;
import static io.anuke.mindustry.Vars.*;
public class NetEvents {
public static void handleFriendlyFireChange(boolean enabled){
FriendlyFireChangePacket packet = new FriendlyFireChangePacket();
packet.enabled = enabled;
netCommon.sendMessage(enabled ? "[accent]Friendly fire enabled." : "[accent]Friendly fire disabled.");
Net.send(packet, SendMode.tcp);
}
public static void handleGameOver(){
Net.send(new GameOverPacket(), SendMode.tcp);
state.gameOver = true;
Timers.runTask(30f, () -> state.set(State.menu));
}
public static void handleBullet(BulletType type, Entity owner, float x, float y, float angle, short damage){
BulletPacket packet = new BulletPacket();
packet.x = x;
packet.y = y;
packet.angle = angle;
packet.damage = damage;
packet.owner = owner.id;
packet.type = type.id;
Net.send(packet, SendMode.udp);
}
public static void handleEnemyDeath(Enemy enemy){
EnemyDeathPacket packet = new EnemyDeathPacket();
packet.id = enemy.id;
state.gameOver = true;
Net.send(packet, SendMode.tcp);
}
public static void handleBlockDestroyed(TileEntity entity){
BlockDestroyPacket packet = new BlockDestroyPacket();
packet.position = entity.tile.packedPosition();
Net.send(packet, SendMode.tcp);
}
public static void handleBlockDamaged(TileEntity entity){
BlockUpdatePacket packet = new BlockUpdatePacket();
packet.health = (int)entity.health;
packet.position = entity.tile.packedPosition();
Net.send(packet, SendMode.udp);
}
public static void handlePlayerDeath(){
PlayerDeathPacket packet = new PlayerDeathPacket();
packet.id = Vars.player.id;
Net.send(packet, SendMode.tcp);
}
public static void handleBlockConfig(Tile tile, byte data){
BlockConfigPacket packet = new BlockConfigPacket();
packet.data = data;
packet.position = tile.packedPosition();
Net.send(packet, SendMode.tcp);
}
public static void handleBlockTap(Tile tile){
BlockTapPacket packet = new BlockTapPacket();
packet.position = tile.packedPosition();
Net.send(packet, SendMode.tcp);
}
public static void handleWeaponSwitch(){
WeaponSwitchPacket packet = new WeaponSwitchPacket();
packet.left = Vars.player.weaponLeft.id;
packet.right = Vars.player.weaponRight.id;
packet.playerid = Vars.player.id;
Net.send(packet, SendMode.tcp);
}
public static void handleUpgrade(Weapon weapon){
UpgradePacket packet = new UpgradePacket();
packet.id = weapon.id;
Net.send(packet, SendMode.tcp);
}
public static void handleSendMessage(String message){
ChatPacket packet = new ChatPacket();
packet.text = message;
packet.name = Vars.player.name;
packet.id = Vars.player.id;
Net.send(packet, SendMode.tcp);
ui.chatfrag.addMessage(packet.text, netCommon.colorizeName(Vars.player.id, Vars.player.name));
}
public static void handleShoot(Weapon weapon, float x, float y, float angle){
ShootPacket packet = new ShootPacket();
packet.weaponid = weapon.id;
packet.x = x;
packet.y = y;
packet.rotation = angle;
packet.playerid = Vars.player.id;
Net.send(packet, SendMode.udp);
}
public static void handlePlace(int x, int y, Block block, int rotation){
PlacePacket packet = new PlacePacket();
packet.x = (short)x;
packet.y = (short)y;
packet.rotation = (byte)rotation;
packet.playerid = Vars.player.id;
packet.block = block.id;
Net.send(packet, SendMode.tcp);
}
public static void handleBreak(int x, int y){
BreakPacket packet = new BreakPacket();
packet.x = (short)x;
packet.y = (short)y;
Net.send(packet, SendMode.tcp);
}
}

View File

@@ -4,7 +4,6 @@ import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.utils.ByteArray;
import com.badlogic.gdx.utils.TimeUtils;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.game.GameMode;
import io.anuke.mindustry.resource.Upgrade;
import io.anuke.mindustry.resource.Weapon;
@@ -20,6 +19,8 @@ import io.anuke.ucore.entities.Entities;
import java.io.*;
import static io.anuke.mindustry.Vars.*;
public class NetworkIO {
public static void writeMap(Pixmap map, OutputStream os){
@@ -97,20 +98,20 @@ public class NetworkIO {
stream.writeLong(TimeUtils.millis()); //timestamp
//--GENERAL STATE--
stream.writeByte(Vars.control.getMode().ordinal()); //gamemode
stream.writeByte(Vars.world.getMap().custom ? -1 : Vars.world.getMap().id); //map ID
stream.writeByte(state.mode.ordinal()); //gamemode
stream.writeByte(world.getMap().custom ? -1 : world.getMap().id); //map ID
stream.writeInt(Vars.control.getWave()); //wave
stream.writeFloat(Vars.control.getWaveCountdown()); //wave countdown
stream.writeInt(Vars.control.enemyGroup.amount()); //enemy amount
stream.writeInt(state.wave); //wave
stream.writeFloat(state.wavetime); //wave countdown
stream.writeInt(state.enemies); //enemy amount
stream.writeBoolean(Vars.control.isFriendlyFire()); //friendly fire state
stream.writeBoolean(state.friendlyFire); //friendly fire state
stream.writeInt(playerID); //player remap ID
//--INVENTORY--
for(int i = 0; i < Vars.control.getItems().length; i ++){ //items
stream.writeInt(Vars.control.getItems()[i]);
for(int i = 0; i < state.inventory.getItems().length; i ++){ //items
stream.writeInt(state.inventory.getItems()[i]);
}
stream.writeByte(upgrades.size); //upgrade data
@@ -122,14 +123,14 @@ public class NetworkIO {
//--MAP DATA--
//seed
stream.writeInt(Vars.world.getSeed());
stream.writeInt(world.getSeed());
int totalblocks = 0;
int totalrocks = 0;
for(int x = 0; x < Vars.world.width(); x ++){
for(int y = 0; y < Vars.world.height(); y ++){
Tile tile = Vars.world.tile(x, y);
for(int x = 0; x < world.width(); x ++){
for(int y = 0; y < world.height(); y ++){
Tile tile = world.tile(x, y);
if(tile.breakable()){
if(tile.block() instanceof Rock){
@@ -145,9 +146,9 @@ public class NetworkIO {
stream.writeInt(totalrocks);
//write all rocks
for(int x = 0; x < Vars.world.width(); x ++) {
for (int y = 0; y < Vars.world.height(); y++) {
Tile tile = Vars.world.tile(x, y);
for(int x = 0; x < world.width(); x ++) {
for (int y = 0; y < world.height(); y++) {
Tile tile = world.tile(x, y);
if (tile.block() instanceof Rock) {
stream.writeInt(tile.packedPosition());
@@ -158,13 +159,13 @@ public class NetworkIO {
//tile amount
stream.writeInt(totalblocks);
for(int x = 0; x < Vars.world.width(); x ++){
for(int y = 0; y < Vars.world.height(); y ++){
Tile tile = Vars.world.tile(x, y);
for(int x = 0; x < world.width(); x ++){
for(int y = 0; y < world.height(); y ++){
Tile tile = world.tile(x, y);
if(tile.breakable() && !(tile.block() instanceof Rock)){
stream.writeInt(x + y*Vars.world.width()); //tile pos
stream.writeInt(x + y*world.width()); //tile pos
//TODO will break if block number gets over BYTE_MAX
stream.writeByte(tile.block().id); //block ID
@@ -227,51 +228,53 @@ public class NetworkIO {
int enemies = stream.readInt();
boolean friendlyfire = stream.readBoolean();
Vars.control.setWaveData(enemies, wave, wavetime);
Vars.control.setMode(GameMode.values()[mode]);
Vars.control.setFriendlyFire(friendlyfire);
state.enemies = enemies;
state.wave = wave;
state.wavetime = wavetime;
state.mode = GameMode.values()[mode];
state.friendlyFire = friendlyfire;
int pid = stream.readInt();
//inventory
for(int i = 0; i < Vars.control.getItems().length; i ++){
Vars.control.getItems()[i] = stream.readInt();
for(int i = 0; i < state.inventory.getItems().length; i ++){
state.inventory.getItems()[i] = stream.readInt();
}
Vars.ui.hudfrag.updateItems();
ui.hudfrag.updateItems();
Vars.control.getWeapons().clear();
Vars.control.getWeapons().add(Weapon.blaster);
control.upgrades().getWeapons().clear();
control.upgrades().getWeapons().add(Weapon.blaster);
byte weapons = stream.readByte();
for(int i = 0; i < weapons; i ++){
Vars.control.getWeapons().add((Weapon) Upgrade.getByID(stream.readByte()));
control.upgrades().getWeapons().add((Weapon) Upgrade.getByID(stream.readByte()));
}
Vars.player.weaponLeft = Vars.player.weaponRight = Vars.control.getWeapons().peek();
Vars.ui.hudfrag.updateWeapons();
player.weaponLeft = player.weaponRight = control.upgrades().getWeapons().peek();
ui.hudfrag.updateWeapons();
Entities.clear();
Vars.player.id = pid;
Vars.player.add();
player.id = pid;
player.add();
//map
int seed = stream.readInt();
Vars.world.loadMap(Vars.world.maps().getMap(mapid), seed);
Vars.renderer.clearTiles();
world.loadMap(world.maps().getMap(mapid), seed);
renderer.clearTiles();
Vars.player.set(Vars.world.getCore().worldx(), Vars.world.getCore().worldy());
player.set(world.getCore().worldx(), world.getCore().worldy());
for(int x = 0; x < Vars.world.width(); x ++){
for(int y = 0; y < Vars.world.height(); y ++){
Tile tile = Vars.world.tile(x, y);
for(int x = 0; x < world.width(); x ++){
for(int y = 0; y < world.height(); y ++){
Tile tile = world.tile(x, y);
//remove breakables like rocks
if(tile.breakable()){
Vars.world.tile(x, y).setBlock(Blocks.air);
world.tile(x, y).setBlock(Blocks.air);
}
}
}
@@ -280,7 +283,7 @@ public class NetworkIO {
for(int i = 0; i < rocks; i ++){
int pos = stream.readInt();
Tile tile = Vars.world.tile(pos % Vars.world.width(), pos / Vars.world.width());
Tile tile = world.tile(pos % world.width(), pos / world.width());
Block result = WorldGenerator.rocks.get(tile.floor());
if(result != null) tile.setBlock(result);
}
@@ -291,7 +294,7 @@ public class NetworkIO {
int pos = stream.readInt();
byte blockid = stream.readByte();
Tile tile = Vars.world.tile(pos % Vars.world.width(), pos / Vars.world.width());
Tile tile = world.tile(pos % world.width(), pos / world.width());
tile.setBlock(Block.getByID(blockid));
if(tile.block() == Blocks.blockpart){