Basic multiplayer placing/breaking/movement (broken)

This commit is contained in:
Anuken
2017-12-31 14:23:13 -05:00
parent eecd0f6d02
commit 36e357819b
29 changed files with 508 additions and 197 deletions

View File

@@ -67,7 +67,7 @@ public class Vars{
//only if smoothCamera
public static boolean snapCamera = true;
//turret and enemy shoot speed inverse multiplier
//turret and enemy shootInternal speed inverse multiplier
public static final float multiplier = android ? 3 : 2;
public static final int tilesize = 8;

View File

@@ -1,16 +1,11 @@
package io.anuke.mindustry.core;
import static io.anuke.mindustry.Vars.*;
import java.util.Arrays;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
@@ -24,6 +19,7 @@ import io.anuke.mindustry.input.AndroidInput;
import io.anuke.mindustry.input.DesktopInput;
import io.anuke.mindustry.input.InputHandler;
import io.anuke.mindustry.io.Saves;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.resource.Item;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.resource.Weapon;
@@ -41,6 +37,10 @@ import io.anuke.ucore.util.Input;
import io.anuke.ucore.util.InputProxy;
import io.anuke.ucore.util.Mathf;
import java.util.Arrays;
import static io.anuke.mindustry.Vars.*;
public class Control extends Module{
Tutorial tutorial = new Tutorial();
boolean hiscore = false;
@@ -52,7 +52,7 @@ public class Control extends Module{
public final EntityGroup<TileEntity> tileGroup = Entities.addGroup(TileEntity.class, false);
public final EntityGroup<Bullet> bulletGroup = Entities.addGroup(Bullet.class);
public final EntityGroup<Shield> shieldGroup = Entities.addGroup(Shield.class);
public final EntityGroup<Player> playerGroup = Entities.addGroup(Player.class);
public final EntityGroup<Player> playerGroup = Entities.addGroup(Player.class).enableMapping();
Array<EnemySpawn> spawns;
int wave = 1;
@@ -128,7 +128,7 @@ public class Control extends Module{
Core.atlas = new Atlas("sprites.atlas");
Sounds.load("shoot.ogg", "place.ogg", "explosion.ogg", "enemyshoot.ogg",
Sounds.load("shoot.ogg", "place.ogg", "explosion.ogg", "enemyshoot.ogg",
"corexplode.ogg", "break.ogg", "spawn.ogg", "flame.ogg", "die.ogg",
"respawn.ogg", "purchase.ogg", "flame2.ogg", "bigshot.ogg", "laser.ogg", "lasershot.ogg",
"ping.ogg", "tesla.ogg", "waveend.ogg", "railgun.ogg", "blast.ogg", "bang2.ogg");
@@ -142,7 +142,7 @@ public class Control extends Module{
"move_y", new Axis(Input.S, Input.W),
"select", Input.MOUSE_LEFT,
"break", Input.MOUSE_RIGHT,
"shoot", Input.MOUSE_LEFT,
"shootInternal", Input.MOUSE_LEFT,
"zoom_hold", Input.CONTROL_LEFT,
"zoom", new Axis(Input.SCROLL),
"menu", Gdx.app.getType() == ApplicationType.Android ? Input.BACK : Input.ESCAPE,
@@ -166,7 +166,7 @@ public class Control extends Module{
"cursor_y", new Axis(Input.CONTROLLER_R_STICK_VERTICAL_AXIS),
"select", Input.CONTROLLER_R_BUMPER,
"break", Input.CONTROLLER_L_BUMPER,
"shoot", Input.CONTROLLER_R_TRIGGER,
"shootInternal", Input.CONTROLLER_R_TRIGGER,
"zoom_hold", Input.ANY_KEY,
"zoom", new Axis(Input.CONTROLLER_DPAD_DOWN, Input.CONTROLLER_DPAD_UP),
"menu", Input.CONTROLLER_X,
@@ -194,6 +194,7 @@ public class Control extends Module{
}
player = new Player();
player.isLocal = true;
spawns = WaveCreator.getSpawns();
@@ -322,6 +323,10 @@ public class Control extends Module{
}
public void runWave(){
if(Net.client() && Net.active()){
return;
}
Sounds.play("spawn");
if(lastUpdated < wave + 1){
@@ -347,6 +352,8 @@ public class Control extends Module{
enemy.tier = spawn.tier(wave, fl);
Effects.effect(Fx.spawn, enemy);
enemy.add(enemyGroup);
Vars.netServer.handleEnemySpawn(enemy);
enemies ++;
}catch (Exception e){

View File

@@ -1,28 +1,31 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.Gdx;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.io.SaveIO;
import io.anuke.mindustry.net.Net;
import com.badlogic.gdx.Gdx;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Packets.Connect;
import io.anuke.mindustry.net.Packets.Disconnect;
import io.anuke.mindustry.net.Packets.EntityDataPacket;
import io.anuke.mindustry.net.Packets.WorldData;
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.ucore.UCore;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.modules.Module;
import java.util.Arrays;
public class NetClient extends Module {
boolean connecting = false;
boolean gotEntities = false;
float playerSyncTime = 3;
public NetClient(){
Net.handle(Connect.class, packet -> {
connecting = true;
gotEntities = false;
Gdx.app.postRunnable(() -> {
Vars.ui.hideLoading();
Vars.ui.showLoading("$text.connecting.data");
@@ -55,24 +58,98 @@ public class NetClient extends Module {
Net.handle(EntityDataPacket.class, data -> {
Gdx.app.postRunnable(() -> {
Timers.run(10f, () -> {
Timers.run(10f, () -> { //TODO hack
Vars.control.playerGroup.remap(Vars.player, data.playerid);
for (Player player : data.players) {
if (player.id != data.playerid) {
player.add();
}
}
UCore.log("Recieved entities: " + Arrays.toString(data.players) + " player ID: " + data.playerid);
gotEntities = true;
});
});
});
Net.handle(SyncPacket.class, packet -> {
if(!gotEntities) return;
for(int i = 0; i < packet.ids.length; i ++){
int id = packet.ids[i];
if(id != Vars.player.id){
Player player = Vars.control.playerGroup.getByID(id);
player.getInterpolator().type.read(player, packet.data[i]);
}
}
});
Net.handle(ShootPacket.class, packet -> {
Player player = Vars.control.playerGroup.getByID(packet.playerid);
Weapon weapon = Weapon.values()[packet.weaponid];
weapon.shoot(player, packet.x, packet.y, packet.rotation);
});
Net.handleServer(PlacePacket.class, packet -> {
Vars.control.input.placeBlockInternal(packet.x, packet.y, Block.getByID(packet.block), packet.rotation, true, false);
});
Net.handleServer(BreakPacket.class, packet -> {
Vars.control.input.breakBlockInternal(packet.x, packet.y, false);
});
Net.handleServer(StateSyncPacket.class, packet -> {
//TODO replace with arraycopy()
for(int i = 0; i < packet.items.length; i ++){
Vars.control.items[i] = packet.items[i];
}
Vars.control.setWaveData(packet.enemies, packet.wave, packet.countdown);
});
}
public void update(){
if(!Net.client()) return;
if(!GameState.is(State.menu) && Net.active()){
sync();
}else if(!connecting){
Net.disconnect();
}
}
public void handleShoot(Weapon weapon, float x, float y, float angle){
ShootPacket packet = new ShootPacket();
packet.weaponid = (byte)weapon.ordinal();
packet.x = x;
packet.y = y;
packet.rotation = angle;
Net.send(packet, SendMode.udp);
}
public 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 void handleBreak(int x, int y){
BreakPacket packet = new BreakPacket();
packet.x = (short)x;
packet.y = (short)y;
Net.send(packet, SendMode.tcp);
}
void sync(){
if(Timers.get("syncPlayer", playerSyncTime)){
PositionPacket packet = new PositionPacket();
packet.data = Vars.player.getInterpolator().type.write(Vars.player);
Net.send(packet, SendMode.tcp); //TODO udp instead?
}
}
}

View File

@@ -1,27 +1,33 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.utils.IntMap;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.enemies.Enemy;
import io.anuke.mindustry.io.SaveIO;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.Packets.Connect;
import io.anuke.mindustry.net.Packets.EntityDataPacket;
import io.anuke.mindustry.net.Packets.SyncPacket;
import io.anuke.mindustry.net.Packets.WorldData;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.resource.Recipe;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.mindustry.world.Block;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.Effects.Effect;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.modules.Module;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Arrays;
public class NetServer extends Module{
IntMap<Player> connections = new IntMap<>();
float serverSyncTime = 4;
float serverSyncTime = 4, itemSyncTime = 20, blockSyncTime = 120;
public NetServer(){
@@ -44,15 +50,70 @@ public class NetServer extends Module{
Player player = new Player();
player.clientid = packet.id;
player.set(Vars.player.x, Vars.player.y);
player.placerot = Vars.player.placerot;
player.add();
connections.put(player.id, player);
connections.put(packet.id, player);
dp.playerid = player.id;
dp.players = Vars.control.playerGroup.all().toArray(Player.class);
UCore.log("Sending entities: " + Arrays.toString(dp.players));
Net.sendTo(packet.id, dp, SendMode.tcp);
});
});
Net.handleServer(PositionPacket.class, pos -> {
Player player = connections.get(Net.getLastConnection());
player.getInterpolator().type.read(player, pos.data);
});
Net.handleServer(ShootPacket.class, packet -> {
Player player = connections.get(Net.getLastConnection());
Weapon weapon = Weapon.values()[packet.weaponid];
weapon.shoot(player, packet.x, packet.y, packet.rotation);
packet.playerid = player.id;
Net.sendExcept(Net.getLastConnection(), packet, SendMode.udp);
});
Net.handleServer(PlacePacket.class, packet -> {
Vars.control.input.placeBlockInternal(packet.x, packet.y, Block.getByID(packet.block), packet.rotation, true, false);
packet.playerid = connections.get(Net.getLastConnection()).id;
Recipe recipe = Recipe.getByResult(Block.getByID(packet.block));
if(recipe != null){
for(ItemStack stack : recipe.requirements){
Vars.control.removeItem(stack);
}
}
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
});
Net.handleServer(BreakPacket.class, packet -> {
Vars.control.input.breakBlockInternal(packet.x, packet.y, false);
packet.playerid = connections.get(Net.getLastConnection()).id;
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp);
});
}
//TODO decide whether to use effects
public void sendEffect(Effect effect, Color color, float x, float y, float rotation){
EffectPacket packet = new EffectPacket();
packet.id = effect.id;
packet.color = Color.rgba8888(color);
packet.x = x;
packet.y = y;
packet.rotation = rotation;
Net.send(packet, SendMode.udp);
}
public void handleEnemySpawn(Enemy enemy){
}
public void update(){
@@ -66,12 +127,51 @@ public class NetServer extends Module{
}
void sync(){
if(Timers.get("serverSync", serverSyncTime)){
SyncPacket packet = new SyncPacket();
int amount = Vars.control.playerGroup.amount();
packet.ids = new int[amount];
packet.data = new float[amount][0];
int index = 0;
for(Player player : Vars.control.playerGroup.all()){
float[] out = player.getInterpolator().type.write(player);
packet.data[index] = out;
packet.ids[index] = player.id;
index ++;
}
Net.send(packet, SendMode.udp);
}
if(Timers.get("serverItemSync", itemSyncTime)){
StateSyncPacket packet = new StateSyncPacket();
packet.items = Vars.control.items;
packet.countdown = Vars.control.getWaveCountdown();
packet.enemies = Vars.control.getEnemiesRemaining();
packet.wave = Vars.control.getWave();
Net.send(packet, SendMode.udp);
}
if(Timers.get("serverBlockSync", blockSyncTime)){
BlockSyncPacket packet = new BlockSyncPacket();
}
}
public void sendBlockSync(int client){
BlockSyncPacket packet = new BlockSyncPacket();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
DataOutputStream stream = new DataOutputStream(bs);
packet.stream = new ByteArrayInputStream(bs.toByteArray());
}
}

View File

@@ -1,8 +1,5 @@
package io.anuke.mindustry.core;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.core.Core.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap.Format;
@@ -12,7 +9,6 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.FloatArray;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player;
@@ -25,7 +21,6 @@ import io.anuke.mindustry.world.SpawnPoint;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Blocks;
import io.anuke.mindustry.world.blocks.ProductionBlocks;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.*;
import io.anuke.ucore.entities.DestructibleEntity;
import io.anuke.ucore.entities.EffectEntity;
@@ -39,6 +34,10 @@ import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.core.Core.batch;
import static io.anuke.ucore.core.Core.camera;
public class Renderer extends RendererModule{
private final static int chunksize = 32;
private final static float shieldHitDuration = 18f;
@@ -419,8 +418,11 @@ public class Renderer extends RendererModule{
drawHealth(entity);
}
if(!Vars.android && Vars.showPlayer && !player.isDead())
drawHealth(player);
if(!Vars.android && Vars.showPlayer) {
for(Player player : Vars.control.playerGroup.all()){
if(!player.isDead()) drawHealth(player);
}
}
}
}

View File

@@ -1,22 +1,12 @@
package io.anuke.mindustry.core;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.scene.actions.Actions.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.ControllerAdapter;
import com.badlogic.gdx.controllers.Controllers;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.IntSet;
import com.badlogic.gdx.utils.IntSet.IntSetIterator;
import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState.State;
@@ -28,17 +18,13 @@ import io.anuke.mindustry.ui.fragments.*;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.Blocks;
import io.anuke.mindustry.world.blocks.types.Configurable;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.*;
import io.anuke.ucore.core.Inputs.DeviceType;
import io.anuke.ucore.function.Consumer;
import io.anuke.ucore.function.Listenable;
import io.anuke.ucore.function.VisibilityProvider;
import io.anuke.ucore.modules.SceneModule;
import io.anuke.ucore.scene.Element;
import io.anuke.ucore.scene.Skin;
import io.anuke.ucore.scene.builders.build;
import io.anuke.ucore.scene.builders.field;
import io.anuke.ucore.scene.builders.label;
import io.anuke.ucore.scene.builders.table;
import io.anuke.ucore.scene.event.Touchable;
@@ -51,9 +37,11 @@ import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Strings;
import javax.tools.Tool;
import java.io.IOException;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.ucore.scene.actions.Actions.*;
public class UI extends SceneModule{
Table loadingtable, configtable;
MindustrySettingsDialog prefs;
@@ -325,7 +313,7 @@ public class UI extends SceneModule{
placefrag.build();
loadingtable = new table("loadDim"){{
get().setTouchable(Touchable.enabled);
touchable(Touchable.enabled);
get().addImage("white").growX()
.height(3f).pad(4f).growX().get().setColor(Colors.get("accent"));
row();

View File

@@ -1,9 +1,5 @@
package io.anuke.mindustry.entities;
import static io.anuke.mindustry.Vars.*;
import com.badlogic.gdx.Input.Buttons;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.input.PlaceMode;
@@ -18,19 +14,23 @@ import io.anuke.ucore.entities.DestructibleEntity;
import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
public class Player extends DestructibleEntity implements Syncable{
private static final float speed = 1.1f;
private static final float dashSpeed = 1.8f;
public transient Weapon weapon;
public transient Weapon weapon = Weapon.blaster;
public Mech mech = Mech.standard;
public float angle;
public transient int clientid;
public transient boolean isLocal = false;
public transient Interpolator<Player> inter = new Interpolator<>(SyncType.player);
public transient float breaktime = 0;
public transient Recipe recipe;
public transient int rotation;
public transient int placerot;
public transient PlaceMode placeMode = android ? PlaceMode.cursor : PlaceMode.hold;
public transient PlaceMode breakMode = android ? PlaceMode.none : PlaceMode.holdDelete;
@@ -41,7 +41,12 @@ public class Player extends DestructibleEntity implements Syncable{
maxhealth = 100;
heal();
}
@Override
public Interpolator getInterpolator() {
return inter;
}
@Override
public void damage(int amount){
if(!Vars.debug && !Vars.android)
@@ -50,14 +55,26 @@ public class Player extends DestructibleEntity implements Syncable{
@Override
public void onDeath(){
remove();
if(isLocal){
remove();
}else{
set(-9999, -9999);
}
Effects.effect(Fx.explosion, this);
Effects.shake(4f, 5f, this);
Effects.sound("die", this);
Vars.control.setRespawnTime(respawnduration);
ui.fadeRespawn(true);
//TODO respawning doesn't work for multiplayer
if(isLocal) {
Vars.control.setRespawnTime(respawnduration);
ui.fadeRespawn(true);
}else{
Timers.run(respawnduration, () -> {
heal();
set(Vars.control.getCore().worldx(), Vars.control.getCore().worldy());
});
}
}
@Override
@@ -74,6 +91,10 @@ public class Player extends DestructibleEntity implements Syncable{
@Override
public void update(){
if(!isLocal){
if(!isDead()) inter.update(this);
return;
}
float speed = Inputs.keyDown("dash") ? Player.dashSpeed : Player.speed;
@@ -95,11 +116,11 @@ public class Player extends DestructibleEntity implements Syncable{
vector.y += ya*speed;
vector.x += xa*speed;
boolean shooting = !Inputs.keyDown("dash") && Inputs.keyDown("shoot") && recipe == null
boolean shooting = !Inputs.keyDown("dash") && Inputs.keyDown("shootInternal") && recipe == null
&& !ui.hasMouse() && !control.getInput().onConfigurable();
if(shooting && Timers.get(this, "reload", weapon.reload)){
weapon.shoot(this);
weapon.shoot(this, x, y, Angles.mouseAngle(x, y));
Sounds.play(weapon.shootsound);
}

View File

@@ -127,9 +127,9 @@ public class AndroidInput extends InputHandler{
player.x += xa * 4f;
player.y += ya * 4f;
player.rotation += Inputs.getAxis("rotate_alt");
player.rotation += Inputs.getAxis("rotate");
player.rotation = Mathf.mod(player.rotation, 4);
player.placerot += Inputs.getAxis("rotate_alt");
player.placerot += Inputs.getAxis("rotate");
player.placerot = Mathf.mod(player.placerot, 4);
if(enableHold && Gdx.input.isTouched(0) && Mathf.near2d(lmousex, lmousey, Gdx.input.getX(0), Gdx.input.getY(0), Unit.dp.scl(50))
&& !ui.hasMouse()){
@@ -171,7 +171,7 @@ public class AndroidInput extends InputHandler{
validPlace(x, y, player.recipe.result) && cursorNear() &&
Vars.control.hasItems(player.recipe.requirements)){
placeBlock(x, y, player.recipe.result, player.rotation, true, sound);
placeBlock(x, y, player.recipe.result, player.placerot, true, sound);
for(ItemStack stack : player.recipe.requirements){
Vars.control.removeItem(stack);

View File

@@ -3,8 +3,6 @@ package io.anuke.mindustry.input;
import static io.anuke.mindustry.Vars.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.core.GameState;
@@ -12,14 +10,10 @@ import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.mindustry.world.Tile;
import io.anuke.mindustry.world.blocks.types.Configurable;
import io.anuke.ucore.UCore;
import io.anuke.ucore.core.Graphics;
import io.anuke.ucore.core.Inputs;
import io.anuke.ucore.core.Inputs.DeviceType;
import io.anuke.ucore.core.KeyBinds;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.InputProxy;
import io.anuke.ucore.util.Mathf;
public class DesktopInput extends InputHandler{
@@ -66,18 +60,18 @@ public class DesktopInput extends InputHandler{
}
if(!rotated) {
player.rotation += Inputs.getAxis("rotate_alt");
player.placerot += Inputs.getAxis("rotate_alt");
rotated = true;
}
if(!Inputs.getAxisActive("rotate_alt")) rotated = false;
if(!rotatedAlt) {
player.rotation += Inputs.getAxis("rotate");
player.placerot += Inputs.getAxis("rotate");
rotatedAlt = true;
}
if(!Inputs.getAxisActive("rotate")) rotatedAlt = false;
player.rotation = Mathf.mod(player.rotation, 4);
player.placerot = Mathf.mod(player.placerot, 4);
if(Inputs.keyDown("break")){
player.breakMode = PlaceMode.areaDelete;

View File

@@ -1,15 +1,13 @@
package io.anuke.mindustry.input;
import static io.anuke.mindustry.Vars.*;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.resource.ItemStack;
import io.anuke.mindustry.resource.Recipe;
import io.anuke.mindustry.world.Block;
@@ -27,6 +25,8 @@ import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
import static io.anuke.mindustry.Vars.*;
public abstract class InputHandler extends InputAdapter{
public abstract void update();
public abstract float getCursorX();
@@ -54,7 +54,7 @@ public abstract class InputHandler extends InputAdapter{
validPlace(x, y, player.recipe.result) && !ui.hasMouse() && cursorNear() &&
Vars.control.hasItems(player.recipe.requirements)){
placeBlock(x, y, player.recipe.result, player.rotation, true, sound);
placeBlock(x, y, player.recipe.result, player.placerot, true, sound);
for(ItemStack stack : player.recipe.requirements){
Vars.control.removeItem(stack);
@@ -116,7 +116,7 @@ public abstract class InputHandler extends InputAdapter{
Block block = Vars.control.getTutorial().getPlaceBlock();
if(type != block || point.x != x - control.getCore().x || point.y != y - control.getCore().y
|| (rotation != -1 && rotation != Vars.player.rotation)){
|| (rotation != -1 && rotation != Vars.player.placerot)){
return false;
}
}else if(Vars.control.getTutorial().active()){
@@ -160,7 +160,7 @@ public abstract class InputHandler extends InputAdapter{
Block block = Vars.control.getTutorial().getPlaceBlock();
if(block != Blocks.air || point.x != x - control.getCore().x || point.y != y - control.getCore().y
|| (rotation != -1 && rotation != Vars.player.rotation)){
|| (rotation != -1 && rotation != Vars.player.placerot)){
return false;
}
}else{
@@ -172,18 +172,27 @@ public abstract class InputHandler extends InputAdapter{
}
public void placeBlock(int x, int y, Block result, int rotation, boolean effects, boolean sound){
placeBlockInternal(x, y, result, rotation, effects, sound);
if(Net.active()){
Vars.netClient.handlePlace(x, y, result, rotation);
}
}
public void placeBlockInternal(int x, int y, Block result, int rotation, boolean effects, boolean sound){
Tile tile = world.tile(x, y);
//just in case
if(tile == null)
return;
tile.setBlock(result, rotation);
if(result.isMultiblock()){
int offsetx = -(result.width-1)/2;
int offsety = -(result.height-1)/2;
for(int dx = 0; dx < result.width; dx ++){
for(int dy = 0; dy < result.height; dy ++){
int worldx = dx + offsetx + x;
@@ -193,47 +202,55 @@ public abstract class InputHandler extends InputAdapter{
if(toplace != null)
toplace.setLinked((byte)(dx + offsetx), (byte)(dy + offsety));
}
if(effects) Effects.effect(Fx.place, worldx * Vars.tilesize, worldy * Vars.tilesize);
}
}
}else{
if(effects) Effects.effect(Fx.place, x * Vars.tilesize, y * Vars.tilesize);
}
if(effects && sound) Sounds.play("place");
result.placed(tile);
}
public void breakBlock(int x, int y, boolean sound){
breakBlockInternal(x, y, sound);
if(Net.active()){
Vars.netClient.handleBreak(x, y);
}
}
public void breakBlockInternal(int x, int y, boolean sound){
Tile tile = world.tile(x, y);
if(tile == null) return;
Block block = tile.isLinked() ? tile.getLinked().block() : tile.block();
Recipe result = null;
for(Recipe recipe : Recipe.values()){
if(recipe.result == block){
result = recipe;
break;
}
}
if(result != null){
for(ItemStack stack : result.requirements){
Vars.control.addItem(stack.item, (int)(stack.amount * Vars.breakDropAmount));
}
}
if(tile.block().drops != null){
Vars.control.addItem(tile.block().drops.item, tile.block().drops.amount);
}
//Effects.shake(3f, 1f, player);
if(sound) Sounds.play("break");
if(!tile.block().isMultiblock() && !tile.isLinked()){
tile.setBlock(Blocks.air);
Effects.effect(Fx.breakBlock, tile.worldx(), tile.worldy());

View File

@@ -20,8 +20,6 @@ import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Tmp;
import javax.tools.Tool;
public enum PlaceMode{
cursor{
{
@@ -45,12 +43,12 @@ public enum PlaceMode{
Draw.linecrect(x + offset.x, y + offset.y, tilesize * player.recipe.result.width + si,
tilesize * player.recipe.result.height + si);
player.recipe.result.drawPlace(tilex, tiley, player.rotation, valid);
player.recipe.result.drawPlace(tilex, tiley, player.placerot, valid);
Draw.thickness(2f);
if(player.recipe.result.rotate){
Draw.color(Colors.get("placeRotate"));
Tmp.v1.set(7, 0).rotate(player.rotation * 90);
Tmp.v1.set(7, 0).rotate(player.placerot * 90);
Draw.line(x, y, x + Tmp.v1.x, y + Tmp.v1.y);
}
@@ -319,7 +317,7 @@ public enum PlaceMode{
public void released(int tilex, int tiley, int endx, int endy){
process(tilex, tiley, endx, endy);
player.rotation = this.rotation;
player.placerot = this.rotation;
boolean first = true;
for(int x = 0; x <= Math.abs(this.endx - this.tilex); x ++){
@@ -358,7 +356,7 @@ public enum PlaceMode{
else if(endy < tiley)
rotation = 3;
else
rotation = player.rotation;
rotation = player.placerot;
if(endx < tilex){
int t = endx;

View File

@@ -71,7 +71,7 @@ import io.anuke.ucore.entities.Entities;
* Tile type (boolean)- whether the block has a tile entity attached
* Block ID - the block ID (byte)
* (the following only applies to tile entity blocks)
* Block rotation (byte)
* Block placerot (byte)
* Block health (int)
* Amount of items (byte)
* (item list)
@@ -285,7 +285,7 @@ public class SaveIO{
stream.writeInt(tile.block().id); //block ID
if(tile.entity != null){
stream.writeByte(tile.getRotation()); //rotation
stream.writeByte(tile.getRotation()); //placerot
stream.writeInt(tile.entity.health); //health
int amount = 0;
for(int i = 0; i < tile.entity.items.length; i ++){

View File

@@ -1,18 +1,15 @@
package io.anuke.mindustry.net;
import java.io.IOException;
import java.io.InputStream;
import java.util.stream.Stream;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.IntMap;
import com.badlogic.gdx.utils.ObjectMap;
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.function.Consumer;
import java.io.IOException;
//TODO stub
public class Net{
private static boolean server;
@@ -22,6 +19,7 @@ public class Net{
private static ClientProvider clientProvider;
private static ServerProvider serverProvider;
private static int lastConnection = -1;
private static IntMap<StreamBuilder> streams = new IntMap<>();
/**Connect to an address.*/
@@ -65,6 +63,11 @@ public class Net{
serverProvider.sendTo(id, object, mode);
}
/**Send an object to everyone EXCEPT certain client. Server-side only*/
public static void sendExcept(int id, Object object, SendMode mode){
serverProvider.sendExcept(id, object, mode);
}
/**Send a stream to a specific client. Server-side only.*/
public static void sendStream(int id, Streamable stream){
serverProvider.sendStream(id, stream);
@@ -114,13 +117,19 @@ public class Net{
}
/**Call to handle a packet being recieved for the server.*/
public static void handleServerReceived(Object object){
public static void handleServerReceived(Object object, int connection){
if(serverListeners.get(object.getClass()) != null){
lastConnection = connection;
serverListeners.get(object.getClass()).accept(object);
}else{
Gdx.app.error("Mindustry::Net", "Unhandled packet type: '" + object.getClass() + "'!");
}
}
/**Returns the last connection that sent a packet to this server.*/
public static int getLastConnection(){
return lastConnection;
}
/**Whether the net is active, e.g. whether this is a multiplayer game.*/
public static boolean active(){

View File

@@ -1,7 +1,5 @@
package io.anuke.mindustry.net;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.Player;
/**Class for storing all packets.*/
@@ -27,6 +25,45 @@ public class Packets {
}
public static class SyncPacket{
public int[] ids;
public float[][] data;
}
public static class BlockSyncPacket extends Streamable{
}
public static class StateSyncPacket {
public int[] items;
public float countdown;
public int enemies, wave;
}
public static class PositionPacket{
public float[] data;
}
public static class EffectPacket{
public int id;
public float x, y, rotation;
public int color;
}
public static class ShootPacket{
public byte weaponid;
public float x, y, rotation;
public int playerid;
}
public static class PlacePacket{
public int playerid;
public byte rotation;
public short x, y;
public int block;
}
public static class BreakPacket{
public int playerid;
public short x, y;
}
}

View File

@@ -3,13 +3,10 @@ package io.anuke.mindustry.net;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.net.Packets.EntityDataPacket;
import io.anuke.mindustry.net.Packets.SyncPacket;
import io.anuke.mindustry.net.Packets.WorldData;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.net.Streamable.StreamBegin;
import io.anuke.mindustry.net.Streamable.StreamChunk;
import io.anuke.mindustry.resource.Mech;
import io.anuke.mindustry.resource.Weapon;
import io.anuke.ucore.entities.Entity;
public class Registrator {
@@ -21,8 +18,18 @@ public class Registrator {
WorldData.class,
SyncPacket.class,
EntityDataPacket.class,
PositionPacket.class,
ShootPacket.class,
PlacePacket.class,
BreakPacket.class,
StateSyncPacket.class,
BlockSyncPacket.class,
Class.class,
byte[].class,
float[].class,
float[][].class,
int[].class,
Entity[].class,
Player[].class,
Array.class,

View File

@@ -1,4 +1,57 @@
package io.anuke.mindustry.net;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.entities.Player;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.util.Mathf;
public interface Syncable {
public Interpolator<?> getInterpolator();
public abstract class SyncType<T extends Entity>{
public abstract float[] write(T entity);
public abstract void read(T entity, float[] data);
public abstract void update(T entity, Interpolator interpolator);
public static final SyncType<Player> player = new SyncType<Player>() {
@Override
public float[] write(Player entity) {
return new float[]{entity.x, entity.y, entity.angle, entity.health};
}
@Override
public void read(Player entity, float[] data) {
entity.getInterpolator().target.set(data[0], data[1]);
entity.getInterpolator().targetrot = data[2];
entity.health = (int)data[3];
}
@Override
public void update(Player entity, Interpolator interpolator) {
Interpolator i = entity.getInterpolator();
if(i.target.dst(entity.x, entity.y) > 16){
entity.set(i.target.x, i.target.y);
}
entity.x = Mathf.lerpDelta(entity.x, i.target.x, 0.4f);
entity.y = Mathf.lerpDelta(entity.y, i.target.y, 0.4f);
entity.angle = Mathf.lerpAngDelta(entity.angle, i.targetrot, 0.6f);
}
};
}
public static class Interpolator<T extends Entity> {
public SyncType<T> type;
public Vector2 target = new Vector2();
public float targetrot;
public Interpolator(SyncType<T> type){
this.type = type;
}
public void update(T entity){
this.type.update(entity, this);
}
}
}

View File

@@ -1,11 +1,13 @@
package io.anuke.mindustry.resource;
import static io.anuke.mindustry.resource.Section.*;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.blocks.*;
import io.anuke.mindustry.world.blocks.DefenseBlocks;
import io.anuke.mindustry.world.blocks.DistributionBlocks;
import io.anuke.mindustry.world.blocks.ProductionBlocks;
import io.anuke.mindustry.world.blocks.WeaponBlocks;
import static io.anuke.mindustry.resource.Section.*;
public enum Recipe{
stonewall(defense, DefenseBlocks.stonewall, stack(Item.stone, 2)),
@@ -94,6 +96,15 @@ public enum Recipe{
private static ItemStack stack(Item item, int amount){
return new ItemStack(item, amount);
}
public static Recipe getByResult(Block block){
for(Recipe recipe : Recipe.values()){
if(recipe.result == block){
return recipe;
}
}
return null;
}
public static Array<Recipe> getBy(Section section, Array<Recipe> r){
for(Recipe recipe : Recipe.values()){

View File

@@ -3,15 +3,14 @@ package io.anuke.mindustry.resource;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Bullet;
import io.anuke.mindustry.entities.BulletType;
import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.graphics.Fx;
import io.anuke.mindustry.net.Net;
import io.anuke.ucore.core.Effects;
import io.anuke.ucore.entities.Entity;
import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Mathf;
@@ -22,35 +21,33 @@ public enum Weapon{
}
@Override
public void shoot(Player p){
super.shoot(p);
Effects.effect(Fx.shoot3, p.x + vector.x, p.y+vector.y);
public void shootInternal(Player p, float x, float y, float rotation){
super.shootInternal(p, x, y, rotation);
Effects.effect(Fx.shoot3, x + vector.x, y+vector.y);
}
},
triblaster(13, BulletType.shot, stack(Item.iron, 40)){
@Override
public void shoot(Player p){
float ang = mouseAngle(p);
public void shootInternal(Player p, float x, float y, float rotation){
float space = 12;
bullet(p, p.x, p.y, ang);
bullet(p, p.x, p.y, ang+space);
bullet(p, p.x, p.y, ang-space);
bullet(p, x, y, rotation);
bullet(p, x, y, rotation + space);
bullet(p, x, y, rotation - space);
Effects.effect(Fx.shoot, p.x + vector.x, p.y+vector.y);
Effects.effect(Fx.shoot, x + vector.x, y + vector.y);
}
},
multigun(6, BulletType.multishot, stack(Item.iron, 60), stack(Item.steel, 20)){
@Override
public void shoot(Player p){
float ang = mouseAngle(p);
public void shootInternal(Player p, float x, float y, float rotation){
MathUtils.random.setSeed(Gdx.graphics.getFrameId());
bullet(p, p.x, p.y, ang + Mathf.range(8));
bullet(p, x, y, rotation + Mathf.range(8));
Effects.effect(Fx.shoot2, p.x + vector.x, p.y+vector.y);
Effects.effect(Fx.shoot2, x + vector.x, y + vector.y);
}
},
flamer(5, BulletType.flame, stack(Item.steel, 60), stack(Item.iron, 120)){
@@ -60,12 +57,10 @@ public enum Weapon{
}
@Override
public void shoot(Player p){
float ang = mouseAngle(p);
//????
public void shootInternal(Player p, float x, float y, float rotation){
MathUtils.random.setSeed(Gdx.graphics.getFrameId());
bullet(p, p.x, p.y, ang + Mathf.range(12));
bullet(p, x, y, rotation + Mathf.range(12));
}
},
railgun(40, BulletType.sniper, stack(Item.steel, 60), stack(Item.iron, 60)){
@@ -75,11 +70,9 @@ public enum Weapon{
}
@Override
public void shoot(Player p){
float ang = mouseAngle(p);
bullet(p, p.x, p.y, ang);
Effects.effect(Fx.railshoot, p.x + vector.x, p.y+vector.y);
public void shootInternal(Player p, float x, float y, float rotation){
bullet(p, x, y, rotation);
Effects.effect(Fx.railshoot, x + vector.x, y + vector.y);
}
},
mortar(100, BulletType.shell, stack(Item.titanium, 40), stack(Item.steel, 60)){
@@ -89,11 +82,10 @@ public enum Weapon{
}
@Override
public void shoot(Player p){
float ang = mouseAngle(p);
bullet(p, p.x, p.y, ang);
Effects.effect(Fx.mortarshoot, p.x + vector.x, p.y+vector.y);
Effects.shake(2f, 2f, Vars.player);
public void shootInternal(Player p, float x, float y, float rotation){
bullet(p, x, y, rotation);
Effects.effect(Fx.mortarshoot, x + vector.x, y + vector.y);
Effects.shake(2f, 2f, p);
}
};
public float reload;
@@ -102,7 +94,7 @@ public enum Weapon{
public boolean unlocked;
public ItemStack[] requirements;
public final String description;
Vector2 vector = new Vector2();
public String localized(){
@@ -116,17 +108,21 @@ public enum Weapon{
this.description = Bundles.getNotNull("weapon."+name()+".description");
}
public void shoot(Player p){
bullet(p, p.x, p.y, mouseAngle(p));
void shootInternal(Player p, float x, float y, float rotation){
bullet(p, x, y, rotation);
}
float mouseAngle(Entity owner){
return Angles.mouseAngle(owner.x, owner.y);
public void shoot(Player p, float x, float y, float angle){
shootInternal(p, x, y, angle);
if(Net.active() && p == Vars.player){
Vars.netClient.handleShoot(this, x, y, angle);
}
}
void bullet(Entity owner, float x, float y, float angle){
vector.set(3, 0).rotate(mouseAngle(owner));
new Bullet(type, owner, x+vector.x, y+vector.y, angle).add();
vector.set(3, 0).rotate(angle);
new Bullet(type, owner, x + vector.x, y + vector.y, angle).add();
}
private static ItemStack stack(Item item, int amount){

View File

@@ -10,7 +10,6 @@ import io.anuke.mindustry.core.GameState;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.input.PlaceMode;
import io.anuke.ucore.core.Core;
import io.anuke.ucore.core.Timers;
import io.anuke.ucore.scene.actions.Actions;
import io.anuke.ucore.scene.builders.*;
import io.anuke.ucore.scene.event.Touchable;
@@ -19,8 +18,6 @@ import io.anuke.ucore.scene.ui.ImageButton;
import io.anuke.ucore.scene.ui.layout.Table;
import io.anuke.ucore.util.Mathf;
import javax.xml.soap.Text;
public class PlacementFragment implements Fragment{
boolean shown = false;
Table breaktable, next;
@@ -100,9 +97,9 @@ public class PlacementFragment implements Fragment{
}
new imagebutton("icon-arrow", 14*3, ()->{
player.rotation = Mathf.mod(player.rotation + 1, 4);
player.placerot = Mathf.mod(player.placerot + 1, 4);
}).imageColor(color).visible(() -> player.recipe != null).update(image ->{
image.getImage().setRotation(player.rotation*90);
image.getImage().setRotation(player.placerot *90);
image.getImage().setOrigin(Align.center);
});

View File

@@ -33,8 +33,8 @@ public class ToolFragment implements Fragment{
});
tools.addIButton("icon-rotate", isize, () -> {
player.rotation ++;
player.rotation %= 4;
player.placerot++;
player.placerot %= 4;
});
tools.addIButton("icon-check", isize, () -> {

View File

@@ -221,7 +221,7 @@ public class Block{
}
public void draw(Tile tile){
//note: multiblocks do not support rotation
//note: multiblocks do not support placerot
if(!isMultiblock()){
Draw.rect(variants > 0 ? (name() + Mathf.randomSeed(tile.id(), 1, variants)) : name(),
tile.worldx(), tile.worldy(), rotate ? tile.getRotation() * 90 : 0);

View File

@@ -17,7 +17,7 @@ public class Tile{
/**Packed block data. Left is floor, right is block.*/
private short blocks;
/**Packed data. Left is rotation, right is extra data, packed into two half-bytes: left is dump, right is extra.*/
/**Packed data. Left is placerot, right is extra data, packed into two half-bytes: left is dump, right is extra.*/
private short data;
/**The coordinates of the core tile this is linked to, in the form of two bytes packed into one.
* This is relative to the block it is linked to; negate coords to find the link.*/
@@ -53,7 +53,7 @@ public class Tile{
return Bits.getLeftByte(blocks);
}
/**Return relative rotation to a coordinate. Returns -1 if the coordinate is not near this tile.*/
/**Return relative placerot to a coordinate. Returns -1 if the coordinate is not near this tile.*/
public int relativeTo(int cx, int cy){
if(x == cx && y == cy - 1) return 1;
if(x == cx && y == cy + 1) return 3;

View File

@@ -172,7 +172,7 @@ public class WeaponBlocks{
shootEffect = Fx.chainshot;
}
//TODO specify turret shoot effect in turret instead of doing it manually
//TODO specify turret shootInternal effect in turret instead of doing it manually
@Override
protected void shoot(Tile tile){
TurretEntity entity = tile.entity();

View File

@@ -1,13 +1,8 @@
package io.anuke.mindustry.world.blocks.types.defense;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Bullet;
import io.anuke.mindustry.entities.BulletType;
@@ -27,6 +22,10 @@ import io.anuke.ucore.util.Angles;
import io.anuke.ucore.util.Mathf;
import io.anuke.ucore.util.Strings;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class Turret extends Block{
static final int targetInterval = 15;
static boolean drawDebug = false;