Done with desktop breaking

This commit is contained in:
Anuken
2018-05-30 13:43:49 -04:00
parent 9d05cfba36
commit e3cbe118c5
2 changed files with 66 additions and 22 deletions

View File

@@ -3,9 +3,11 @@ package io.anuke.mindustry.input;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons; import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player; import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.graphics.Palette; import io.anuke.mindustry.graphics.Palette;
import io.anuke.mindustry.input.PlaceUtils.NormalizeDrawResult;
import io.anuke.mindustry.input.PlaceUtils.NormalizeResult; import io.anuke.mindustry.input.PlaceUtils.NormalizeResult;
import io.anuke.mindustry.world.Block; import io.anuke.mindustry.world.Block;
import io.anuke.mindustry.world.Tile; import io.anuke.mindustry.world.Tile;
@@ -20,6 +22,7 @@ import io.anuke.ucore.scene.utils.Cursors;
import io.anuke.ucore.util.Mathf; import io.anuke.ucore.util.Mathf;
import static io.anuke.mindustry.Vars.*; import static io.anuke.mindustry.Vars.*;
import static io.anuke.mindustry.input.DesktopInput.PlaceMode.*;
public class DesktopInput extends InputHandler{ public class DesktopInput extends InputHandler{
//controller info //controller info
@@ -30,8 +33,8 @@ public class DesktopInput extends InputHandler{
/**Position where the player started dragging a line.*/ /**Position where the player started dragging a line.*/
private int selectX, selectY; private int selectX, selectY;
/**Wehther selecting mode is active.*/ /**Whether selecting mode is active.*/
private boolean selecting; private PlaceMode mode;
/**Animation scale for line.*/ /**Animation scale for line.*/
private float selectScale; private float selectScale;
@@ -85,14 +88,14 @@ public class DesktopInput extends InputHandler{
if(cursor == null) return; if(cursor == null) return;
//draw selection //draw selection
if(selecting){ if(mode == placing) {
NormalizeResult result = PlaceUtils.normalizeArea(selectX, selectY, cursor.x, cursor.y, rotation, true, maxLength); NormalizeResult result = PlaceUtils.normalizeArea(selectX, selectY, cursor.x, cursor.y, rotation, true, maxLength);
for(int i = 0; i <= result.getLength(); i += recipe.result.size){ for (int i = 0; i <= result.getLength(); i += recipe.result.size) {
int x = selectX + i * Mathf.sign(cursor.x - selectX) * Mathf.bool(result.isX()); int x = selectX + i * Mathf.sign(cursor.x - selectX) * Mathf.bool(result.isX());
int y = selectY + i * Mathf.sign(cursor.y - selectY) * Mathf.bool(!result.isX()); int y = selectY + i * Mathf.sign(cursor.y - selectY) * Mathf.bool(!result.isX());
if(i + recipe.result.size > result.getLength() && recipe.result.rotate){ if (i + recipe.result.size > result.getLength() && recipe.result.rotate) {
Draw.color(!validPlace(x, y, recipe.result, result.rotation) ? Palette.remove : Palette.placeRotate); Draw.color(!validPlace(x, y, recipe.result, result.rotation) ? Palette.remove : Palette.placeRotate);
Draw.grect("place-arrow", x * tilesize + recipe.result.offset(), Draw.grect("place-arrow", x * tilesize + recipe.result.offset(),
y * tilesize + recipe.result.offset(), result.rotation * 90 - 90); y * tilesize + recipe.result.offset(), result.rotation * 90 - 90);
@@ -102,7 +105,28 @@ public class DesktopInput extends InputHandler{
} }
Draw.reset(); Draw.reset();
}else if(isPlacing()){ }else if(mode == breaking){
NormalizeDrawResult result = PlaceUtils.normalizeDrawArea(Blocks.air, selectX, selectY, cursor.x, cursor.y, false, maxLength, 1f);
NormalizeResult dresult = PlaceUtils.normalizeArea(selectX, selectY, cursor.x, cursor.y, rotation, false, maxLength);
Draw.color(Palette.remove);
Draw.alpha(0.6f);
//Fill.crect(result.x, result.y, result.x2 - result.x, result.y2 - result.y);
Draw.alpha(1f);
for(int x = dresult.x; x <= dresult.x2; x ++){
for(int y = dresult.y; y <= dresult.y2; y ++){
Tile tile = world.tile(x, y);
if(tile == null || !validBreak(tile.x, tile.y)) continue;
tile = tile.target();
Lines.poly(tile.drawx(), tile.drawy(), 4, tile.block().size * tilesize/2f, 45 + 15);
}
}
Lines.rect(result.x, result.y, result.x2 - result.x, result.y2 - result.y);
}else if(isPlacing()){
if(recipe.result.rotate){ if(recipe.result.rotate){
Draw.color(!validPlace(cursor.x, cursor.y, recipe.result, rotation) ? Palette.remove : Palette.placeRotate); Draw.color(!validPlace(cursor.x, cursor.y, recipe.result, rotation) ? Palette.remove : Palette.placeRotate);
Draw.grect("place-arrow", cursor.worldx() + recipe.result.offset(), Draw.grect("place-arrow", cursor.worldx() + recipe.result.offset(),
@@ -119,8 +143,8 @@ public class DesktopInput extends InputHandler{
if(player.isDead() || state.is(State.menu) || ui.hasDialog()) return; if(player.isDead() || state.is(State.menu) || ui.hasDialog()) return;
//deslect if not placing //deslect if not placing
if(!isPlacing()){ if(!isPlacing() && mode == placing){
selecting = false; mode = none;
} }
if(isPlacing()){ if(isPlacing()){
@@ -165,12 +189,22 @@ public class DesktopInput extends InputHandler{
if(cursor == null) return false; if(cursor == null) return false;
if(isPlacing()) { //if left, begin placing or tap a tile
if(button == Buttons.LEFT) {
if (isPlacing()) {
selectX = cursor.x;
selectY = cursor.y;
mode = placing;
} else {
tileTapped(cursor);
}
}else if(button == Buttons.RIGHT){
selectX = cursor.x; selectX = cursor.x;
selectY = cursor.y; selectY = cursor.y;
selecting = true; mode = breaking;
}else { }else if(button == Buttons.MIDDLE){
tileTapped(cursor); recipe = null;
mode = none;
} }
return false; return false;
@@ -183,11 +217,11 @@ public class DesktopInput extends InputHandler{
Tile cursor = tileAt(screenX, screenY); Tile cursor = tileAt(screenX, screenY);
if(cursor == null){ if(cursor == null){
selecting = false; mode = none;
return false; return false;
} }
if(selecting){ if(mode == placing){
NormalizeResult result = PlaceUtils.normalizeArea(selectX, selectY, cursor.x, cursor.y, rotation, true, maxLength); NormalizeResult result = PlaceUtils.normalizeArea(selectX, selectY, cursor.x, cursor.y, rotation, true, maxLength);
for(int i = 0; i <= result.getLength(); i += recipe.result.size){ for(int i = 0; i <= result.getLength(); i += recipe.result.size){
@@ -198,9 +232,20 @@ public class DesktopInput extends InputHandler{
tryPlaceBlock(x, y); tryPlaceBlock(x, y);
} }
}else if(mode == breaking){
NormalizeResult result = PlaceUtils.normalizeArea(selectX, selectY, cursor.x, cursor.y, rotation, false, maxLength);
for(int x = 0; x <= Math.abs(result.x2 - result.x); x ++ ){
for(int y = 0; y <= Math.abs(result.y2 - result.y); y ++){
int wx = selectX + x * Mathf.sign(cursor.x - selectX);
int wy = selectY + y * Mathf.sign(cursor.y - selectY);
tryBreakBlock(wx, wy);
}
}
} }
selecting = false; mode = none;
return false; return false;
} }
@@ -265,4 +310,8 @@ public class DesktopInput extends InputHandler{
controly = control.gdxInput().getY(); controly = control.gdxInput().getY();
} }
} }
enum PlaceMode{
none, breaking, placing;
}
} }

View File

@@ -168,22 +168,17 @@ public abstract class InputHandler extends InputAdapter{
return true; return true;
} }
public boolean tryPlaceBlock(int x, int y){ public void tryPlaceBlock(int x, int y){
if(recipe != null && validPlace(x, y, recipe.result, rotation) && cursorNear()){ if(recipe != null && validPlace(x, y, recipe.result, rotation) && cursorNear()){
placeBlock(x, y, recipe, rotation); placeBlock(x, y, recipe, rotation);
return true;
} }
return false;
} }
public boolean tryDeleteBlock(int x, int y){ public void tryBreakBlock(int x, int y){
if(cursorNear() && validBreak(x, y)){ if(cursorNear() && validBreak(x, y)){
breakBlock(x, y); breakBlock(x, y);
return true;
} }
return false;
} }
public boolean validPlace(int x, int y, Block type, int rotation){ public boolean validPlace(int x, int y, Block type, int rotation){