Major restructuring of Vars class- made global state less messy
This commit is contained in:
@@ -51,12 +51,12 @@ public class AndroidInput extends InputAdapter{
|
||||
|
||||
public static void breakBlock(){
|
||||
Tile tile = selected();
|
||||
breaktime += Mathf.delta();
|
||||
if(breaktime >= tile.block().breaktime){
|
||||
player.breaktime += Mathf.delta();
|
||||
if(player.breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
breaktime = 0f;
|
||||
player.breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}
|
||||
}
|
||||
@@ -67,27 +67,27 @@ public class AndroidInput extends InputAdapter{
|
||||
int tilex = Mathf.scl2(vec.x, tilesize);
|
||||
int tiley = Mathf.scl2(vec.y, tilesize);
|
||||
|
||||
if(recipe != null &&
|
||||
World.validPlace(tilex, tiley, recipe.result)){
|
||||
if(player.recipe != null &&
|
||||
World.validPlace(tilex, tiley, player.recipe.result)){
|
||||
|
||||
Tile tile = World.tile(tilex, tiley);
|
||||
|
||||
if(tile == null)
|
||||
return; //just in case
|
||||
|
||||
tile.setBlock(recipe.result);
|
||||
tile.rotation = rotation;
|
||||
tile.setBlock(player.recipe.result);
|
||||
tile.rotation = player.rotation;
|
||||
|
||||
Effects.effect("place", tilex*tilesize, tiley*tilesize);
|
||||
Effects.shake(2f, 2f);
|
||||
Sounds.play("place");
|
||||
|
||||
for(ItemStack stack : recipe.requirements){
|
||||
for(ItemStack stack : player.recipe.requirements){
|
||||
Inventory.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Inventory.hasItems(recipe.requirements)){
|
||||
recipe = null;
|
||||
if(!Inventory.hasItems(player.recipe.requirements)){
|
||||
player.recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
}
|
||||
@@ -96,7 +96,7 @@ public class AndroidInput extends InputAdapter{
|
||||
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){
|
||||
&& !ui.hasMouse() && player.recipe == null){
|
||||
warmup += Mathf.delta();
|
||||
|
||||
mousex = Gdx.input.getX(0);
|
||||
@@ -107,18 +107,18 @@ public class AndroidInput extends InputAdapter{
|
||||
if(sel == null) return;
|
||||
|
||||
if(warmup > warmupDelay && sel.block() != ProductionBlocks.core && sel.breakable()){
|
||||
breaktime += Mathf.delta();
|
||||
player.breaktime += Mathf.delta();
|
||||
|
||||
if(breaktime > selected().block().breaktime){
|
||||
if(player.breaktime > selected().block().breaktime){
|
||||
breakBlock();
|
||||
breaktime = 0;
|
||||
player.breaktime = 0;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
warmup = 0;
|
||||
lmousex = Gdx.input.getX(0);
|
||||
lmousey = Gdx.input.getY(0);
|
||||
breaktime = 0;
|
||||
player.breaktime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,12 +21,12 @@ public class GestureHandler extends GestureAdapter{
|
||||
@Override
|
||||
public boolean longPress(float x, float y){
|
||||
Tile tile = World.cursorTile();
|
||||
breaktime += Mathf.delta();
|
||||
if(breaktime >= tile.block().breaktime){
|
||||
player.breaktime += Mathf.delta();
|
||||
if(player.breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
breaktime = 0f;
|
||||
player.breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}
|
||||
return false;
|
||||
@@ -34,7 +34,7 @@ public class GestureHandler extends GestureAdapter{
|
||||
|
||||
@Override
|
||||
public boolean pan(float x, float y, float deltaX, float deltaY){
|
||||
if(recipe == null){
|
||||
if(player.recipe == null){
|
||||
player.x -= deltaX*control.camera.zoom/control.cameraScale;
|
||||
player.y += deltaY*control.camera.zoom/control.cameraScale;
|
||||
}else{
|
||||
@@ -47,7 +47,7 @@ public class GestureHandler extends GestureAdapter{
|
||||
|
||||
@Override
|
||||
public boolean pinch (Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
|
||||
if(recipe == null)
|
||||
if(player.recipe == null)
|
||||
return false;
|
||||
|
||||
if(pinch1.x < 0){
|
||||
|
||||
@@ -25,71 +25,56 @@ public class Input{
|
||||
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;
|
||||
}
|
||||
int index = currentWeapon();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
index -= Inputs.scroll();
|
||||
player.weapon = control.getWeapons().get(Mathf.clamp(index, 0, control.getWeapons().size-1));
|
||||
|
||||
ui.updateWeapons();
|
||||
}
|
||||
|
||||
if(Inputs.keyUp("rotate"))
|
||||
rotation++;
|
||||
player.rotation++;
|
||||
|
||||
rotation %= 4;
|
||||
player.rotation %= 4;
|
||||
|
||||
if(recipe != null && !Inventory.hasItems(recipe.requirements)){
|
||||
recipe = null;
|
||||
if(player.recipe != null && !Inventory.hasItems(player.recipe.requirements)){
|
||||
player.recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
|
||||
for(int i = 0; i < 9; i ++)
|
||||
if(Inputs.keyUp(Keys.valueOf(""+(i+1))) && getWeapon(i) != null){
|
||||
currentWeapon = getWeapon(i);
|
||||
if(Inputs.keyUp(Keys.valueOf(""+(i+1))) && i < control.getWeapons().size){
|
||||
player.weapon = control.getWeapons().get(i);
|
||||
ui.updateWeapons();
|
||||
}
|
||||
|
||||
if(Inputs.buttonUp(Buttons.LEFT) && recipe != null &&
|
||||
World.validPlace(World.tilex(), World.tiley(), recipe.result) && !ui.hasMouse()){
|
||||
if(Inputs.buttonUp(Buttons.LEFT) && player.recipe != null &&
|
||||
World.validPlace(World.tilex(), World.tiley(), player.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;
|
||||
tile.setBlock(player.recipe.result);
|
||||
tile.rotation = player.rotation;
|
||||
|
||||
Effects.effect("place", World.roundx(), World.roundy());
|
||||
Effects.shake(2f, 2f);
|
||||
Sounds.play("place");
|
||||
|
||||
for(ItemStack stack : recipe.requirements){
|
||||
for(ItemStack stack : player.recipe.requirements){
|
||||
Inventory.removeItem(stack);
|
||||
}
|
||||
|
||||
if(!Inventory.hasItems(recipe.requirements)){
|
||||
recipe = null;
|
||||
if(!Inventory.hasItems(player.recipe.requirements)){
|
||||
player.recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
}
|
||||
|
||||
if(recipe != null && Inputs.buttonUp(Buttons.RIGHT)){
|
||||
recipe = null;
|
||||
if(player.recipe != null && Inputs.buttonUp(Buttons.RIGHT)){
|
||||
player.recipe = null;
|
||||
Cursors.restoreCursor();
|
||||
}
|
||||
|
||||
@@ -99,38 +84,27 @@ public class Input{
|
||||
if(Inputs.buttonDown(Buttons.RIGHT) && World.cursorNear() && cursor.breakable()
|
||||
&& cursor.block() != ProductionBlocks.core){
|
||||
Tile tile = cursor;
|
||||
breaktime += Mathf.delta();
|
||||
if(breaktime >= tile.block().breaktime){
|
||||
player.breaktime += Mathf.delta();
|
||||
if(player.breaktime >= tile.block().breaktime){
|
||||
Effects.effect("break", tile.worldx(), tile.worldy());
|
||||
Effects.shake(3f, 1f);
|
||||
tile.setBlock(Blocks.air);
|
||||
breaktime = 0f;
|
||||
player.breaktime = 0f;
|
||||
Sounds.play("break");
|
||||
}
|
||||
}else{
|
||||
breaktime = 0f;
|
||||
player.breaktime = 0f;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int currentWeapons(){
|
||||
public static int currentWeapon(){
|
||||
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;
|
||||
for(Weapon weapon : control.getWeapons()){
|
||||
if(player.weapon == weapon)
|
||||
return i;
|
||||
i ++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user