Implemented Android support
This commit is contained in:
121
core/src/io/anuke/mindustry/input/AndroidInput.java
Normal file
121
core/src/io/anuke/mindustry/input/AndroidInput.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package io.anuke.mindustry.input;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.InputAdapter;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import io.anuke.mindustry.Inventory;
|
||||
import io.anuke.mindustry.World;
|
||||
import io.anuke.mindustry.resource.ItemStack;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.Blocks;
|
||||
import io.anuke.mindustry.world.blocks.ProductionBlocks;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Graphics;
|
||||
import io.anuke.ucore.core.Sounds;
|
||||
import io.anuke.ucore.scene.utils.Cursors;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public class AndroidInput extends InputAdapter{
|
||||
public static float mousex, mousey;
|
||||
private static float lmousex, lmousey;
|
||||
private static float warmup;
|
||||
private static float warmupDelay = 20;
|
||||
|
||||
@Override
|
||||
public boolean keyDown (int keycode) {
|
||||
if(keycode == Keys.E){
|
||||
place();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown (int screenX, int screenY, int pointer, int button) {
|
||||
if(pointer == 0){
|
||||
lmousex = screenX;
|
||||
lmousey = screenY;
|
||||
}
|
||||
warmup = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Tile selected(){
|
||||
Vector2 vec = Graphics.world(mousex, mousey);
|
||||
return World.tile(Mathf.scl2(vec.x, tilesize), Mathf.scl2(vec.y, tilesize));
|
||||
}
|
||||
|
||||
public static void breakBlock(){
|
||||
Tile tile = selected();
|
||||
breaktime += Mathf.delta();
|
||||
if(breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}
|
||||
}
|
||||
|
||||
public static void place(){
|
||||
Vector2 vec = Graphics.world(mousex, mousey);
|
||||
|
||||
int tilex = Mathf.scl2(vec.x, tilesize);
|
||||
int tiley = Mathf.scl2(vec.y, tilesize);
|
||||
|
||||
if(recipe != null &&
|
||||
World.validPlace(tilex, tiley, recipe.result)){
|
||||
|
||||
Tile tile = World.tile(tilex, tiley);
|
||||
|
||||
if(tile == null)
|
||||
return; //just in case
|
||||
|
||||
tile.setBlock(recipe.result);
|
||||
tile.rotation = rotation;
|
||||
|
||||
Effects.effect("place", tilex*tilesize, tiley*tilesize);
|
||||
Effects.shake(2f, 2f);
|
||||
Sounds.play("place");
|
||||
|
||||
for(ItemStack stack : recipe.requirements){
|
||||
Inventory.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Inventory.hasItems(recipe.requirements)){
|
||||
recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void doInput(){
|
||||
if(Gdx.input.isTouched(0)
|
||||
&& Mathf.near2d(lmousex, lmousey, Gdx.input.getX(0), Gdx.input.getY(0), 50)
|
||||
&& !ui.hasMouse() && recipe == null){
|
||||
warmup += Mathf.delta();
|
||||
|
||||
mousex = Gdx.input.getX(0);
|
||||
mousey = Gdx.input.getY(0);
|
||||
|
||||
Tile sel = selected();
|
||||
|
||||
if(warmup > warmupDelay && sel.block() != ProductionBlocks.core && sel.breakable()){
|
||||
breaktime += Mathf.delta();
|
||||
|
||||
if(breaktime > selected().block().breaktime){
|
||||
breakBlock();
|
||||
breaktime = 0;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
warmup = 0;
|
||||
lmousex = Gdx.input.getX(0);
|
||||
lmousey = Gdx.input.getY(0);
|
||||
breaktime = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
99
core/src/io/anuke/mindustry/input/GestureHandler.java
Normal file
99
core/src/io/anuke/mindustry/input/GestureHandler.java
Normal file
@@ -0,0 +1,99 @@
|
||||
package io.anuke.mindustry.input;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.input.GestureDetector.GestureAdapter;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
import io.anuke.mindustry.World;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.Blocks;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Sounds;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public class GestureHandler extends GestureAdapter{
|
||||
Vector2 pinch1 = new Vector2(-1, -1), pinch2 = pinch1.cpy();
|
||||
Vector2 vector = new Vector2();
|
||||
float initzoom = -1;
|
||||
|
||||
@Override
|
||||
public boolean longPress(float x, float y){
|
||||
Tile tile = World.cursorTile();
|
||||
breaktime += Mathf.delta();
|
||||
if(breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean pan(float x, float y, float deltaX, float deltaY){
|
||||
if(recipe == null){
|
||||
player.x -= deltaX*control.camera.zoom/control.cameraScale;
|
||||
player.y += deltaY*control.camera.zoom/control.cameraScale;
|
||||
}else{
|
||||
AndroidInput.mousex += deltaX;
|
||||
AndroidInput.mousey += deltaY;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean pinch (Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
|
||||
if(recipe == null)
|
||||
return false;
|
||||
|
||||
if(pinch1.x < 0){
|
||||
pinch1.set(initialPointer1);
|
||||
pinch2.set(initialPointer2);
|
||||
}
|
||||
|
||||
Vector2 vec = (vector.set(pointer1).add(pointer2).scl(0.5f)).sub(pinch1.add(pinch2).scl(0.5f));
|
||||
|
||||
player.x -= vec.x*control.camera.zoom/control.cameraScale;
|
||||
player.y += vec.y*control.camera.zoom/control.cameraScale;
|
||||
|
||||
pinch1.set(pointer1);
|
||||
pinch2.set(pointer2);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean zoom(float initialDistance, float distance){
|
||||
|
||||
if(initzoom <= 0)
|
||||
initzoom = initialDistance;
|
||||
|
||||
control.targetzoom /= (distance/initzoom);
|
||||
control.clampZoom();
|
||||
control.camera.update();
|
||||
|
||||
initzoom = distance;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pinchStop () {
|
||||
initzoom = -1;
|
||||
pinch2.set(pinch1.set(-1, -1));
|
||||
}
|
||||
|
||||
int touches(){
|
||||
int sum = 0;
|
||||
for(int i = 0; i < 10; i ++){
|
||||
if(Gdx.input.isTouched(i)) sum++;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
136
core/src/io/anuke/mindustry/input/Input.java
Normal file
136
core/src/io/anuke/mindustry/input/Input.java
Normal file
@@ -0,0 +1,136 @@
|
||||
package io.anuke.mindustry.input;
|
||||
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
import com.badlogic.gdx.Input.Buttons;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
|
||||
import io.anuke.mindustry.Inventory;
|
||||
import io.anuke.mindustry.World;
|
||||
import io.anuke.mindustry.entities.Weapon;
|
||||
import io.anuke.mindustry.resource.ItemStack;
|
||||
import io.anuke.mindustry.world.Tile;
|
||||
import io.anuke.mindustry.world.blocks.Blocks;
|
||||
import io.anuke.mindustry.world.blocks.ProductionBlocks;
|
||||
import io.anuke.ucore.core.Effects;
|
||||
import io.anuke.ucore.core.Inputs;
|
||||
import io.anuke.ucore.core.Sounds;
|
||||
import io.anuke.ucore.scene.utils.Cursors;
|
||||
import io.anuke.ucore.util.Mathf;
|
||||
|
||||
public class Input{
|
||||
|
||||
public static void doInput(){
|
||||
//player is dead
|
||||
if(player.health <= 0) return;
|
||||
|
||||
if(Inputs.scrolled()){
|
||||
Weapon[] val = Weapon.values();
|
||||
int index = 0;
|
||||
for(int i = 0; i < val.length; i ++)
|
||||
if(val[i] == currentWeapon){
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
|
||||
for(int i = 0; i < val.length; i ++){
|
||||
index += Inputs.scroll();
|
||||
if(index >= 0 && index < val.length){
|
||||
if(weapons.get(val[index])){
|
||||
currentWeapon = (val[index]);
|
||||
break;
|
||||
}
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ui.updateWeapons();
|
||||
}
|
||||
|
||||
if(Inputs.keyUp("rotate"))
|
||||
rotation++;
|
||||
|
||||
rotation %= 4;
|
||||
|
||||
if(recipe != null && !Inventory.hasItems(recipe.requirements)){
|
||||
recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
|
||||
for(int i = 0; i < 9; i ++)
|
||||
if(Inputs.keyUp(Keys.valueOf(""+(i+1))) && getWeapon(i) != null){
|
||||
currentWeapon = getWeapon(i);
|
||||
ui.updateWeapons();
|
||||
}
|
||||
|
||||
if(Inputs.buttonUp(Buttons.LEFT) && recipe != null &&
|
||||
World.validPlace(World.tilex(), World.tiley(), recipe.result) && !ui.hasMouse()){
|
||||
Tile tile = World.tile(World.tilex(), World.tiley());
|
||||
|
||||
if(tile == null)
|
||||
return; //just in case
|
||||
|
||||
tile.setBlock(recipe.result);
|
||||
tile.rotation = rotation;
|
||||
|
||||
Effects.effect("place", World.roundx(), World.roundy());
|
||||
Effects.shake(2f, 2f);
|
||||
Sounds.play("place");
|
||||
|
||||
for(ItemStack stack : recipe.requirements){
|
||||
Inventory.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Inventory.hasItems(recipe.requirements)){
|
||||
recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
}
|
||||
|
||||
if(recipe != null && Inputs.buttonUp(Buttons.RIGHT)){
|
||||
recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
|
||||
Tile cursor = World.cursorTile();
|
||||
|
||||
//block breaking
|
||||
if(Inputs.buttonDown(Buttons.RIGHT) && World.cursorNear() && cursor.breakable()
|
||||
&& cursor.block() != ProductionBlocks.core){
|
||||
Tile tile = cursor;
|
||||
breaktime += Mathf.delta();
|
||||
if(breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}
|
||||
}else{
|
||||
breaktime = 0f;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int currentWeapons(){
|
||||
int i = 0;
|
||||
|
||||
for(Weapon w : Weapon.values())
|
||||
if(weapons.get(w))
|
||||
i ++;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
public static Weapon getWeapon(int id){
|
||||
int i = 0;
|
||||
|
||||
for(Weapon w : Weapon.values())
|
||||
if(weapons.get(w))
|
||||
if(i ++ == id)
|
||||
return w;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user