Android Open Source - diploma-assignment Battle Screen






From Project

Back to project page diploma-assignment.

License

The source code is released under:

MIT License

If you think the Android project diploma-assignment listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.me.battlescreen;
/*  ww w .  j a v  a2s. co m*/


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
import com.me.main.MyGame;
import com.me.main.Player;

public class BattleScreen implements Screen {
  
  private SpriteBatch batch;
  private CleanBackground Bg;
  private int height;
  private int width;
  private MyGame game;
  private Castle castlePlayer,castleOpponent;
  private Ballista ballista;
  private HumanPlayer player;
  private ComputerPlayer playerComp;
  private Stage stage;
  private CopyOnWriteArrayList<IHittable> hittables;
  private CopyOnWriteArrayList<IAttacking> attacking;
  private CopyOnWriteArrayList<IUpdateable> updateables;
  private CopyOnWriteArrayList<BallistaArrow> arrows;
  private long startTime,endTime,startGame,stopTime;
  private boolean first,firstRender=true;
  private boolean winner;
  private boolean running;
  
  public BattleScreen(MyGame g,Player p){
    stage=new Stage();
    game=g;
    batch = new SpriteBatch();
    Texture texture = new Texture(Gdx.files.internal("data/necropolis_top_252_1600.png"));
    Bg=new CleanBackground(texture,this);
    Bg.addListener(new InputListener(){
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        Bg.startDrag(x);
                return true;
      }
        
      public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        Bg.endDrag();
          }
      
      public void touchDragged(InputEvent event, float x, float y, int pointer){
        Bg.drag(x);
      }
    });
    hittables=new CopyOnWriteArrayList<IHittable>();
    arrows=new CopyOnWriteArrayList<BallistaArrow>();
    attacking=new CopyOnWriteArrayList<IAttacking>();
    updateables=new CopyOnWriteArrayList<IUpdateable>();
    first=true;
    running=true;
    player=new HumanPlayer(p);
    playerComp=new ComputerPlayer(this,p.getLevel());
    winner=false;
  }

  @Override
  public void dispose() {
    batch.dispose();
  }

  private void update() {
    for(BallistaArrow arrow:arrows)
      arrow.arrowHit();
    float leastPlayerX=2*Constants.BACKWIDTH;
    float maxComputerX=-Constants.BACKWIDTH;
    for(IHittable hit:hittables){
      if(hit.getPlayer()==playerComp){
        if(leastPlayerX>hit.giveX())
          leastPlayerX=hit.giveX();
      } else if(maxComputerX<hit.giveX()) {
          maxComputerX=hit.giveX();
      }
    }
    for(IAttacking atc: attacking){
      if(atc.isAttacking() && atc.timeToAttack()){
        for(IHittable hit:hittables){
          if(hit.enemy(atc.getPlayer()))
            hit.hit(atc.getStart(), atc.getEnd(), atc.getAttack());
        }
      }
      if(atc.getPlayer()==player)
        atc.checkForAttack(leastPlayerX);
      else
        atc.checkForAttack(maxComputerX);
    }
    player.increaseCurrMana(player.getManaReg());
    playerComp.increaseCurrMana(playerComp.getManaReg());
    for(IUpdateable updateable:updateables)
      updateable.update(player);
    playerComp.checkForUnit();
  }

  @Override
  public void resize(int w, int h) {
    if(first){
      first=false;
      width=w;
      height=h;
      stage.setViewport(w, h, false);
      stage.clear();
      hittables.clear();
      attacking.clear();
      updateables.clear();
      Bg.setY((int) (h*(1-Constants.BACKGROUND_HEIGHT_PERCENTAGE)));
      Bg.setHeight(h*Constants.BACKGROUND_HEIGHT_PERCENTAGE);
      Bg.restart();
      stage.addActor(Bg);
      
      Texture texture = new Texture(Gdx.files.internal("data/rampart_castle_tower.png"));
      castlePlayer=new Castle(this,player,texture,(int)(height*Constants.CASTLE_HEIGHT),
          (int)(height*Constants.CASTLE_HEIGHT_RAMPART_OFFSET),false,
          player.getPlayer().getLevel()*Constants.CASTLE_LEVEL_HEALTH);
      
      texture = new Texture(Gdx.files.internal("data/necro_castle_tower.png"));
      castleOpponent=new Castle(this,playerComp,texture,(int)(height*Constants.CASTLE_HEIGHT),
          (int)(height*Constants.CASTLE_HEIGHT_NECRO_OFFSET),true,
          player.getPlayer().getLevel()*Constants.CASTLE_LEVEL_HEALTH);
      castleOpponent.setX(Constants.BACKWIDTH-castleOpponent.getWidth());
      
      stage.addActor(castlePlayer);
      stage.addActor(castleOpponent);
      
      hittables.add(castlePlayer);
      hittables.add(castleOpponent);
      
      Constants.MONSTER_X_OFFSET=castlePlayer.getWidth()/2;
      Constants.MONSTER_X_COMPUTER_OFFSET=Constants.BACKWIDTH - castleOpponent.getWidth()/2;
      
      playerComp.setMonsters();
      
      texture= new Texture(Gdx.files.internal("data/ballista.png"));
      Texture texture2= new Texture(Gdx.files.internal("data/arrow.png"));
      ballista=new Ballista(this,texture,texture2,(int)(height*Constants.BALLISTA_HEIGHT),
          (int)(height*Constants.BALLISTA_HEIGHT_OFFSET));
      stage.addActor(ballista);
      
      addButtons();
      
      BitmapFont font=new BitmapFont(Gdx.files.internal("data/myFont.fnt"),
          Gdx.files.internal("data/myFont.png"),false);
      font.setUseIntegerPositions(false);
      font.setScale(h*0.10f/font.getLineHeight());
      
      MyLabel label=new MyLabel(this,"",font,new int[]{(int) (width*0.2f),height});
      label.setTouchable(Touchable.disabled);
      stage.addActor(label);
      player.setManaLabel(label);
      
      startGame=TimeUtils.millis();
      TimeLabel timeLabel=new TimeLabel(this,"",font,new int[]{(int)(width*0.7f),height});
      stage.addActor(timeLabel);
      updateables.add(timeLabel);
    }
  }
  
  public void addMonster(Monster m){
    stage.addActor(m);
    hittables.add(m);
    attacking.add(m);
  }
  
  public void removeActor(Actor a){
    stage.getActors().removeIndex(stage.getActors().indexOf(a, true));
  }
  
  public void removeHittable(IHittable obstacle) {
    // TODO Auto-generated method stub
    hittables.remove(obstacle);
  }
  
  public void removeAttacking(IAttacking atk) {
    // TODO Auto-generated method stub
    attacking.remove(atk);
  }
  
  public void addArrow(BallistaArrow ballistaArrow) {
    stage.addActor(ballistaArrow);
    arrows.add(ballistaArrow);
  }

  public void removeArrow(BallistaArrow ballistaArrow) {
    stage.getActors().removeIndex(stage.getActors().indexOf(ballistaArrow, true));
    arrows.remove(ballistaArrow);
  }
  
  public void arrowHit(float x1, float y1, float x2, float y2,int attack,boolean ballista) {
    // TODO Auto-generated method stub
    for(IHittable hittable: hittables){
      hittable.arrowHit(x1,y1,x2,y2,attack,ballista);
    }
  }

  @Override
  public void pause() {
    stopTime=TimeUtils.millis();
  }

  @Override
  public void resume() {
    startGame+=TimeUtils.millis()-stopTime;
  }

  @Override
  public void render(float delta) {
    // TODO Auto-generated method stub
    update();
    if(firstRender){
      startTime=TimeUtils.millis();
      firstRender=false;
      endTime=startTime+42;
    } else{
      endTime=TimeUtils.millis();
    }    
    if(endTime-startTime<33 && endTime-startTime>0)
      try {
        Thread.sleep(33-(endTime-startTime));
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    startTime=TimeUtils.millis();
    System.out.println(Gdx.graphics.getFramesPerSecond());
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    batch.begin();
    stage.draw();
        batch.end();
        
  }

  @Override
  public void show() {
    // TODO Auto-generated method stub
    Gdx.input.setInputProcessor(stage);
  }

  @Override
  public void hide() {
    // TODO Auto-generated method stub
    Gdx.input.setInputProcessor(null);
  }
  
  private void addButtons(){
    loadBottomButtons();
    
    loadBallistaButtons();
  }

  private void loadBottomButtons() {
    
    final int costs[]=new int[]{100,300,600,1000,1500,2100,3000,-1};
    
    CopyOnWriteArrayList<UnitButton> myButtons=new CopyOnWriteArrayList<UnitButton>();
    
    createUnitButtons(costs, myButtons);
    
    prepareUnitButtons(costs, myButtons);
    
    final UnitButton bal=myButtons.get(7);
    bal.addListener(new ClickListener(){
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        bal.setPressed(true);
                return true;
      }
        
      public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        bal.setPressed(false);
        ballista.changeAttack();
          }
    });
  }
  
  private void createUnitButtons(final int[] costs,
      CopyOnWriteArrayList<UnitButton> myButtons) {
    ArrayList<Texture> id_thumbnail_list=new ArrayList<Texture>();
    getTextures.getThumbnailsRampart(id_thumbnail_list,player.getUpList());
    
    
    int h = (int) (height*(1-Constants.BACKGROUND_HEIGHT_PERCENTAGE));
    
    int button_place = 0;
    int button_height =(int) (0.9*h);
    int button_width = width/8;
    
    int height_offset=(int)(0.05*h);
    int width_offset=(int)((button_width-button_height)/2);
    
    for (int i = 0; i < 8; i++) {
      final int curr=i;
      final UnitButton but=new UnitButton(id_thumbnail_list.get(curr),costs[curr]);
      but.setPosition(button_place+width_offset, height_offset);
      but.setSize(button_height, button_height);
      myButtons.add(but);
      stage.addActor(but);
      button_place = button_place + button_width;
    }
  }

  private void prepareUnitButtons(final int[] costs,
      CopyOnWriteArrayList<UnitButton> myButtons) {
    
    final ArrayList<Texture> walk=new ArrayList<Texture>();
    getTextures.getWalkingRampart(walk,player.getUpList());
    final ArrayList<Texture> die=new ArrayList<Texture>();
    getTextures.getDyingRampart(die,player.getUpList());
    final ArrayList<Texture> fight=new ArrayList<Texture>();
    getTextures.getAttackingRampart(fight,player.getUpList());
    final ArrayList<Texture[]> textures=new ArrayList<Texture[]>();
    for(int i=0;i<7;i++){
      textures.add(new Texture[]{walk.get(i),die.get(i),fight.get(i)});
    }
    final List<int[]> id_list = new ArrayList<int[]>();
    getTextures.getIdItemsRampart(id_list,height,player.getUpList());
    final float times[]=new float[]{0.5f,1f,2f,3.5f,6f,10f,15f};
    for(int i=0;i<7;i++){
      final int element=i;
      final UnitButton b=myButtons.get(i);
      b.setTouchable(Touchable.disabled);
      updateables.add(b);
      if(player.getUnlockedList()[element]==1){
        b.addListener(new ClickListener(){
          public boolean touchDown (InputEvent event, float x,
              float y, int pointer, int button) {
            b.setTouchable(Touchable.disabled);
            b.setCanChange(false);
            PlayerMonster m=new PlayerMonster(BattleScreen.this,
                textures.get(element),id_list.get(element),player);
            addMonster(m);
            player.decreaseCurrMana(costs[element]);
            Timer.schedule(new Task(){
              @Override
              public void run(){
                b.setTouchable(Touchable.enabled);
                b.setCanChange(true);
              }
            }, times[element]);
                    return true;
          }
        });
      } else {
        b.setCanChange(false);
      }
    }
  }

  private void loadBallistaButtons() {
    // TODO Auto-generated method stub
    final MyButton b1=new MyButton(new Texture(Gdx.files.internal("data/up.png")));
    final MyButton b2=new MyButton(new Texture(Gdx.files.internal("data/down.png")));
    b1.setPosition((int)(width*0.9f),(int) (height*0.8f));
    b1.setSize((int)(height*0.2f),(int) (height*0.2f));
    b2.setPosition((int)(width*0.9f),(int) (height*0.5f));
    b2.setSize((int)(height*0.2f),(int) (height*0.2f));
    stage.addActor(b1);
    b1.addListener(new ClickListener(){
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        b1.setPressed(true);
        Timer.schedule(new Task(){
          @Override
          public void run(){
            if(b1.isPressed()){
              ballista.changeAngle(2);
              Timer.schedule(this,0.1f);
            }
          }
        },0.2f);
                return true;
      }
        
      public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        b1.setPressed(false);
        ballista.changeAngle(2);
          }
    });
    stage.addActor(b2);
    b2.addListener(new ClickListener(){
      public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        b2.setPressed(true);
        Timer.schedule(new Task(){
          @Override
          public void run(){
            if(b2.isPressed()){
              ballista.changeAngle(-2);
              Timer.schedule(this,0.1f);
            }
          }
        },0.2f);
                return true;
      }
        
      public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        b2.setPressed(false);
          }
    });
  }

  public CleanBackground getBg() {
    // TODO Auto-generated method stub
    return Bg;
  }

  public int getHeight() {
    // TODO Auto-generated method stub
    return height;
  }

  public int getWidth() {
    // TODO Auto-generated method stub
    return width;
  }

  public long getTime() {
    // TODO Auto-generated method stub
    return startGame;
  }

  public void endGame() {
    // TODO Auto-generated method stub
    if(running){
      Player p=player.getPlayer();
      if(winner){
        p.increaseGold(1000);
        running=false;
        p.increaseLevel();
      }
      else
        p.increaseGold(100);
      game.setScreen(game.getOpeningScreen());
      game.getOpeningScreen().intoPlayerScreen();
    }
  }

  public void setWinner(boolean b) {
    // TODO Auto-generated method stub
    winner=b;
  }

  public void giveGoldandMana(int amount) {
    // TODO Auto-generated method stub
    player.getPlayer().increaseGold(amount);
    player.increaseCurrMana(amount);
  }

  public void killedOpponentMonster(int index) {
    // TODO Auto-generated method stub
    player.getPlayer().kill(index);
  }
}




Java Source Code List

com.me.battlescreen.BallistaArrow.java
com.me.battlescreen.Ballista.java
com.me.battlescreen.BattlePlayer.java
com.me.battlescreen.BattleScreen.java
com.me.battlescreen.Bot.java
com.me.battlescreen.Castle.java
com.me.battlescreen.CleanBackground.java
com.me.battlescreen.ComputerPlayer.java
com.me.battlescreen.Constants.java
com.me.battlescreen.HumanPlayer.java
com.me.battlescreen.Monster.java
com.me.battlescreen.MyButton.java
com.me.battlescreen.MyLabel.java
com.me.battlescreen.OpponentMonster.java
com.me.battlescreen.PlayerMonster.java
com.me.battlescreen.TimeLabel.java
com.me.battlescreen.UnitButton.java
com.me.battlescreen.getTextures.java
com.me.battlescreen.iAttacking.java
com.me.battlescreen.iHittable.java
com.me.battlescreen.iUpdateable.java
com.me.main.MyGame.java
com.me.main.Player.java
com.me.mygdxgame.MainActivity.java
com.me.openingscreen.AbstractMenu.java
com.me.openingscreen.Constants.java
com.me.openingscreen.FirstMenu.java
com.me.openingscreen.FloatingBackground.java
com.me.openingscreen.OpeningScreen.java
com.me.openingscreen.PlayerMenu.java
com.me.openingscreen.ResetMenu.java
com.me.openingscreen.StatisticsMenu.java
com.me.openingscreen.UpgradeButton.java
com.me.openingscreen.UpgradeMenu.java