Improved Discord RPC, refactored Input and fixed net crash

This commit is contained in:
Anuken
2018-01-03 20:15:05 -05:00
parent 0063e32f6f
commit 169b9cddc5
29 changed files with 373 additions and 330 deletions

View File

@@ -1,11 +1,8 @@
package io.anuke.mindustry.input;
import static io.anuke.mindustry.Vars.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.core.GameState;
import io.anuke.mindustry.core.GameState.State;
@@ -20,6 +17,8 @@ import io.anuke.ucore.scene.ui.layout.Unit;
import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
public class AndroidInput extends InputHandler{
public float lmousex, lmousey;
public float mousex, mousey;
@@ -38,7 +37,7 @@ public class AndroidInput extends InputHandler{
@Override public float getCursorEndY(){ return Gdx.input.getY(0); }
@Override public float getCursorX(){ return mousex; }
@Override public float getCursorY(){ return mousey; }
@Override public boolean drawPlace(){ return (placing && !brokeBlock) || (player.placeMode.pan && player.recipe != null); }
@Override public boolean drawPlace(){ return (placing && !brokeBlock) || (placeMode.pan && recipe != null); }
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button){
@@ -47,10 +46,10 @@ public class AndroidInput extends InputHandler{
return false;
}
if(placing && pointer == 0 && !player.placeMode.pan && !breaking()){
player.placeMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
}else if(pointer == 0 && !player.breakMode.pan && breaking() && drawPlace()){
player.breakMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
if(placing && pointer == 0 && !placeMode.pan && !breaking()){
placeMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
}else if(pointer == 0 && !breakMode.pan && breaking() && drawPlace()){
breakMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
}
placing = false;
return false;
@@ -64,7 +63,7 @@ public class AndroidInput extends InputHandler{
lmousex = screenX;
lmousey = screenY;
if((!player.placeMode.pan || breaking()) && pointer == 0){
if((!placeMode.pan || breaking()) && pointer == 0){
mousex = screenX;
mousey = screenY;
}
@@ -105,18 +104,18 @@ public class AndroidInput extends InputHandler{
public void breakBlock(){
Tile tile = selected();
player.breaktime += Timers.delta();
breaktime += Timers.delta();
if(player.breaktime >= tile.block().breaktime){
if(breaktime >= tile.block().breaktime){
brokeBlock = true;
breakBlock(tile.x, tile.y, true);
player.breaktime = 0f;
breaktime = 0f;
}
}
@Override
public void update(){
enableHold = player.breakMode == PlaceMode.holdDelete;
enableHold = breakMode == PlaceMode.holdDelete;
float scl = Settings.getInt("sensitivity")/100f * Unit.dp.scl(1f);
float xa = Inputs.getAxis("move_x");
@@ -127,9 +126,9 @@ public class AndroidInput extends InputHandler{
player.x += xa * 4f;
player.y += ya * 4f;
player.placerot += Inputs.getAxis("rotate_alt");
player.placerot += Inputs.getAxis("rotate");
player.placerot = Mathf.mod(player.placerot, 4);
rotation += Inputs.getAxis("rotate_alt");
rotation += Inputs.getAxis("rotate");
rotation = Mathf.mod(rotation, 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()){
@@ -146,11 +145,11 @@ public class AndroidInput extends InputHandler{
return;
if(warmup > warmupDelay && validBreak(sel.x, sel.y)){
player.breaktime += Timers.delta();
breaktime += Timers.delta();
if(player.breaktime > selected().block().breaktime){
if(breaktime > selected().block().breaktime){
breakBlock();
player.breaktime = 0;
breaktime = 0;
}
}
@@ -158,7 +157,7 @@ public class AndroidInput extends InputHandler{
mousey = ly;
}else{
warmup = 0;
player.breaktime = 0;
breaktime = 0;
mousex = Mathf.clamp(mousex, 0, Gdx.graphics.getWidth());
mousey = Mathf.clamp(mousey, 0, Gdx.graphics.getHeight());
@@ -167,17 +166,17 @@ public class AndroidInput extends InputHandler{
@Override
public boolean tryPlaceBlock(int x, int y, boolean sound){
if(player.recipe != null &&
validPlace(x, y, player.recipe.result) && cursorNear() &&
Vars.control.hasItems(player.recipe.requirements)){
if(recipe != null &&
validPlace(x, y, recipe.result) && cursorNear() &&
Vars.control.hasItems(recipe.requirements)){
placeBlock(x, y, player.recipe.result, player.placerot, true, sound);
placeBlock(x, y, recipe.result, rotation, true, sound);
for(ItemStack stack : player.recipe.requirements){
for(ItemStack stack : recipe.requirements){
Vars.control.removeItem(stack);
}
if(!Vars.control.hasItems(player.recipe.requirements)){
if(!Vars.control.hasItems(recipe.requirements)){
Cursors.restoreCursor();
}
return true;
@@ -186,6 +185,6 @@ public class AndroidInput extends InputHandler{
}
public boolean breaking(){
return player.recipe == null;
return recipe == null;
}
}

View File

@@ -1,10 +1,7 @@
package io.anuke.mindustry.input;
import static io.anuke.mindustry.Vars.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.core.GameState;
import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.resource.Weapon;
@@ -16,6 +13,8 @@ import io.anuke.ucore.core.Timers;
import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*;
public class DesktopInput extends InputHandler{
int mousex, mousey;
int endx, endy;
@@ -34,14 +33,14 @@ public class DesktopInput extends InputHandler{
if(player.isDead()) return;
if(Inputs.keyRelease("select")){
player.placeMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
placeMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
}
if(Inputs.keyRelease("break") && !beganBreak){
player.breakMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
breakMode.released(getBlockX(), getBlockY(), getBlockEndX(), getBlockEndY());
}
if((Inputs.keyTap("select") && player.recipe != null) || Inputs.keyTap("break")){
if((Inputs.keyTap("select") && recipe != null) || Inputs.keyTap("break")){
Vector2 vec = Graphics.world(Gdx.input.getX(), Gdx.input.getY());
mousex = (int)vec.x;
mousey = (int)vec.y;
@@ -60,23 +59,23 @@ public class DesktopInput extends InputHandler{
}
if(!rotated) {
player.placerot += Inputs.getAxis("rotate_alt");
rotation += Inputs.getAxis("rotate_alt");
rotated = true;
}
if(!Inputs.getAxisActive("rotate_alt")) rotated = false;
if(!rotatedAlt) {
player.placerot += Inputs.getAxis("rotate");
rotation += Inputs.getAxis("rotate");
rotatedAlt = true;
}
if(!Inputs.getAxisActive("rotate")) rotatedAlt = false;
player.placerot = Mathf.mod(player.placerot, 4);
rotation = Mathf.mod(rotation, 4);
if(Inputs.keyDown("break")){
player.breakMode = PlaceMode.areaDelete;
breakMode = PlaceMode.areaDelete;
}else{
player.breakMode = PlaceMode.hold;
breakMode = PlaceMode.hold;
}
for(int i = 1; i <= 6 && i <= control.getWeapons().size; i ++){
@@ -105,41 +104,41 @@ public class DesktopInput extends InputHandler{
beganBreak = false;
}
if(player.recipe != null && Inputs.keyTap("break")){
if(recipe != null && Inputs.keyTap("break")){
beganBreak = true;
player.recipe = null;
recipe = null;
Cursors.restoreCursor();
}
//block breaking
if(enableHold && Inputs.keyDown("break") && cursor != null && validBreak(tilex(), tiley())){
player.breaktime += Timers.delta();
if(player.breaktime >= cursor.getBreakTime()){
breaktime += Timers.delta();
if(breaktime >= cursor.getBreakTime()){
breakBlock(cursor.x, cursor.y, true);
player.breaktime = 0f;
breaktime = 0f;
}
}else{
player.breaktime = 0f;
breaktime = 0f;
}
}
public int tilex(){
return (player.recipe != null && player.recipe.result.isMultiblock() &&
player.recipe.result.width % 2 == 0) ?
return (recipe != null && recipe.result.isMultiblock() &&
recipe.result.width % 2 == 0) ?
Mathf.scl(Graphics.mouseWorld().x, tilesize) : Mathf.scl2(Graphics.mouseWorld().x, tilesize);
}
public int tiley(){
return (player.recipe != null && player.recipe.result.isMultiblock() &&
player.recipe.result.height % 2 == 0) ?
return (recipe != null && recipe.result.isMultiblock() &&
recipe.result.height % 2 == 0) ?
Mathf.scl(Graphics.mouseWorld().y, tilesize) : Mathf.scl2(Graphics.mouseWorld().y, tilesize);
}
public int currentWeapon(){
int i = 0;
for(Weapon weapon : control.getWeapons()){
if(player.weapon == weapon)
if(weapon == weapon)
return i;
i ++;
}

View File

@@ -31,14 +31,14 @@ public class GestureHandler extends GestureAdapter{
public boolean tap (float x, float y, int count, int button) {
if(ui.hasMouse() || input.brokeBlock) return false;
if(!player.placeMode.pan || player.recipe == null){
if(!control.getInput().placeMode.pan || control.getInput().recipe == null){
input.mousex = x;
input.mousey = y;
if(player.recipe == null)
player.breakMode.tapped(input.getBlockX(), input.getBlockY());
if(control.getInput().recipe == null)
control.getInput().breakMode.tapped(input.getBlockX(), input.getBlockY());
else
player.placeMode.tapped(input.getBlockX(), input.getBlockY());
control.getInput().placeMode.tapped(input.getBlockX(), input.getBlockY());
}
return false;
}
@@ -47,13 +47,13 @@ public class GestureHandler extends GestureAdapter{
public boolean pan(float x, float y, float deltaX, float deltaY){
if(Vars.control.showCursor() && !Inputs.keyDown("select")) return false;
if(!Vars.control.showCursor() && !(player.recipe != null && Vars.control.hasItems(player.recipe.requirements) && player.placeMode.lockCamera) &&
!(player.recipe == null && player.breakMode.lockCamera)){
if(!Vars.control.showCursor() && !(control.getInput().recipe != null && Vars.control.hasItems(control.getInput().recipe.requirements) && control.getInput().placeMode.lockCamera) &&
!(control.getInput().recipe == null && control.getInput().breakMode.lockCamera)){
float dx = deltaX*Core.camera.zoom/Core.cameraScale, dy = deltaY*Core.camera.zoom/Core.cameraScale;
player.x -= dx;
player.y += dy;
player.targetAngle = Mathf.atan2(dx, -dy);
}else if(player.placeMode.lockCamera && (player.placeMode.pan && player.recipe != null)){
}else if(control.getInput().placeMode.lockCamera && (control.getInput().placeMode.pan && control.getInput().recipe != null)){
input.mousex += deltaX;
input.mousey += deltaY;
}
@@ -63,7 +63,7 @@ public class GestureHandler extends GestureAdapter{
@Override
public boolean pinch (Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
if(player.recipe == null && !player.breakMode.lockCamera)
if(control.getInput().recipe == null && !control.getInput().breakMode.lockCamera)
return false;
if(pinch1.x < 0){

View File

@@ -28,6 +28,12 @@ import io.anuke.ucore.util.Tmp;
import static io.anuke.mindustry.Vars.*;
public abstract class InputHandler extends InputAdapter{
public float breaktime = 0;
public Recipe recipe;
public int rotation;
public PlaceMode placeMode = android ? PlaceMode.cursor : PlaceMode.hold;
public PlaceMode breakMode = android ? PlaceMode.none : PlaceMode.holdDelete;
public abstract void update();
public abstract float getCursorX();
public abstract float getCursorY();
@@ -50,17 +56,17 @@ public abstract class InputHandler extends InputAdapter{
}
public boolean tryPlaceBlock(int x, int y, boolean sound){
if(player.recipe != null &&
validPlace(x, y, player.recipe.result) && !ui.hasMouse() && cursorNear() &&
Vars.control.hasItems(player.recipe.requirements)){
if(recipe != null &&
validPlace(x, y, recipe.result) && !ui.hasMouse() && cursorNear() &&
Vars.control.hasItems(recipe.requirements)){
placeBlock(x, y, player.recipe.result, player.placerot, true, sound);
placeBlock(x, y, recipe.result, rotation, true, sound);
for(ItemStack stack : player.recipe.requirements){
for(ItemStack stack : recipe.requirements){
Vars.control.removeItem(stack);
}
if(!Vars.control.hasItems(player.recipe.requirements)){
if(!Vars.control.hasItems(recipe.requirements)){
Cursors.restoreCursor();
}
return true;
@@ -77,7 +83,7 @@ public abstract class InputHandler extends InputAdapter{
}
public boolean round2(){
return !(player.recipe != null && player.recipe.result.isMultiblock() && player.recipe.result.height % 2 == 0);
return !(recipe != null && recipe.result.isMultiblock() && recipe.result.height % 2 == 0);
}
public boolean validPlace(int x, int y, Block type){
@@ -116,7 +122,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.placerot)){
|| (rotation != -1 && rotation != this.rotation)){
return false;
}
}else if(Vars.control.getTutorial().active()){
@@ -160,7 +166,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.placerot)){
|| (rotation != -1 && rotation != this.rotation)){
return false;
}
}else{

View File

@@ -1,14 +1,11 @@
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.graphics.Color;
import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.ui.fragments.ToolFragment;
import io.anuke.mindustry.world.Block;
@@ -20,6 +17,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 enum PlaceMode{
cursor{
{
@@ -32,23 +31,23 @@ public enum PlaceMode{
float x = tilex * Vars.tilesize;
float y = tiley * Vars.tilesize;
boolean valid = control.getInput().validPlace(tilex, tiley, player.recipe.result) && (android || control.getInput().cursorNear());
boolean valid = control.getInput().validPlace(tilex, tiley, control.getInput().recipe.result) && (android || control.getInput().cursorNear());
Vector2 offset = player.recipe.result.getPlaceOffset();
Vector2 offset = control.getInput().recipe.result.getPlaceOffset();
float si = MathUtils.sin(Timers.time() / 6f) + 1.5f;
Draw.color(valid ? Colors.get("place") : Colors.get("placeInvalid"));
Draw.thickness(2f);
Draw.linecrect(x + offset.x, y + offset.y, tilesize * player.recipe.result.width + si,
tilesize * player.recipe.result.height + si);
Draw.linecrect(x + offset.x, y + offset.y, tilesize * control.getInput().recipe.result.width + si,
tilesize * control.getInput().recipe.result.height + si);
player.recipe.result.drawPlace(tilex, tiley, player.placerot, valid);
control.getInput().recipe.result.drawPlace(tilex, tiley, control.getInput().rotation, valid);
Draw.thickness(2f);
if(player.recipe.result.rotate){
if(control.getInput().recipe.result.rotate){
Draw.color(Colors.get("placeRotate"));
Tmp.v1.set(7, 0).rotate(player.placerot * 90);
Tmp.v1.set(7, 0).rotate(control.getInput().rotation * 90);
Draw.line(x, y, x + Tmp.v1.x, y + Tmp.v1.y);
}
@@ -95,12 +94,12 @@ public enum PlaceMode{
if(tile.isLinked())
tile = tile.getLinked();
Vector2 offset = tile.block().getPlaceOffset();
float fract = player.breaktime / tile.getBreakTime();
float fract = control.getInput().breaktime / tile.getBreakTime();
if(Inputs.buttonDown(Buttons.RIGHT)){
Draw.color(Color.YELLOW, Color.SCARLET, fract);
Draw.linecrect(tile.worldx() + offset.x, tile.worldy() + offset.y, tile.block().width * Vars.tilesize, tile.block().height * Vars.tilesize);
}else if(android && player.breaktime > 0){
}else if(android && control.getInput().breaktime > 0){
Draw.color(Colors.get("breakStart"), Colors.get("break"), fract);
Draw.polygon(25, tile.worldx() + offset.x, tile.worldy() + offset.y, 4 + (1f - fract) * 26);
}
@@ -254,7 +253,7 @@ public enum PlaceMode{
}
float t = Vars.tilesize;
Block block = player.recipe.result;
Block block = control.getInput().recipe.result;
Vector2 offset = block.getPlaceOffset();
process(tilex, tiley, endx, endy);
@@ -296,15 +295,15 @@ public enum PlaceMode{
int px = tx + cx * Mathf.sign(ex - tx),
py = ty + cy * Mathf.sign(ey - ty);
if(!control.getInput().validPlace(px, py, player.recipe.result)
|| !control.hasItems(player.recipe.requirements, amount)){
if(!control.getInput().validPlace(px, py, control.getInput().recipe.result)
|| !control.hasItems(control.getInput().recipe.requirements, amount)){
Draw.linecrect(px * t + offset.x, py * t + offset.y, t*block.width, t*block.height);
}
amount ++;
}
}
if(player.recipe.result.rotate){
if(control.getInput().recipe.result.rotate){
float cx = tx * t, cy = ty * t;
Draw.color(Colors.get("placeRotate"));
Tmp.v1.set(7, 0).rotate(rotation * 90);
@@ -317,7 +316,7 @@ public enum PlaceMode{
public void released(int tilex, int tiley, int endx, int endy){
process(tilex, tiley, endx, endy);
player.placerot = this.rotation;
control.getInput().rotation = this.rotation;
boolean first = true;
for(int x = 0; x <= Math.abs(this.endx - this.tilex); x ++){
@@ -356,7 +355,7 @@ public enum PlaceMode{
else if(endy < tiley)
rotation = 3;
else
rotation = player.placerot;
rotation = control.getInput().rotation;
if(endx < tilex){
int t = endx;