Android Open Source - ubisoldiers Unit Fragment






From Project

Back to project page ubisoldiers.

License

The source code is released under:

MIT License

If you think the Android project ubisoldiers 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.github.gobbisanches.ubisoldiers.app;
//from  w  w w .ja  v  a2s  .co  m
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.github.gobbisanches.ubisoldiers.mechanics.BattleUnit;
import com.github.gobbisanches.ubisoldiers.mechanics.Unit;

/**
 * Created by Sanches on 08/07/2014.
 */
public class UnitFragment extends Fragment {
    public enum State { SHOOTING, TAKING_DAMAGE, NORMAL }
    private static final String BATTLE_UNIT = "com.github.gobbisanches.ubisoldier.app.UnitFragment.BattleUnit";

    private BattleUnit battleUnit;
    private TextView nameView;
    private TextView attackView;
    private TextView defenseView;
    private TextView hpView;
    private ImageView soldierImageView;
    private ImageView weaponImageView;
    private ImageView armorImageView;
    private State state;
    private LinearLayout linearLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        battleUnit = (BattleUnit) getArguments().getSerializable(BATTLE_UNIT);
        if (battleUnit == null) {
            throw new RuntimeException("Invalid BattleUnit object in UnitFragment object");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View v = inflater.inflate(R.layout.unit_layout, container, false);
        nameView = (TextView) v.findViewById(R.id.nameView);
        attackView = (TextView) v.findViewById(R.id.attackView);
        defenseView = (TextView) v.findViewById(R.id.defenseView);
        hpView = (TextView) v.findViewById(R.id.hpView);
        soldierImageView  = (ImageView) v.findViewById(R.id.soldierImageView);
        weaponImageView = (ImageView) v.findViewById(R.id.weaponImageView);
        armorImageView = (ImageView) v.findViewById(R.id.armorImageView);
        linearLayout = (LinearLayout) v.findViewById(R.id.unit_linear_layout);
        state = State.NORMAL;

        updateView();

        return v;
    }

    public static UnitFragment newInstance(BattleUnit battleUnit) {
        Bundle args = new Bundle();
        args.putSerializable(BATTLE_UNIT, battleUnit);

        UnitFragment fragment = new UnitFragment();
        fragment.setArguments(args);

        return fragment;
    }

    public static UnitFragment newInstance(Unit unit) {
        Bundle args = new Bundle();
        args.putSerializable(BATTLE_UNIT, new BattleUnit(unit));

        UnitFragment fragment = new UnitFragment();
        fragment.setArguments(args);

        return fragment;
    }

    public void takeDamage(int amount) {
        battleUnit.takeDamage(amount);
        updateView();
    }

    public void setState(State state) {
        this.state = state;
        updateView();
    }

    public void updateBattleUnit(BattleUnit battleUnit) {
        this.battleUnit = battleUnit;
        updateView();
    }

    public void updateUnitAndSetHpToFull(Unit unit) {
        updateBattleUnit(new BattleUnit(unit));
    }

    public String getSoldierName() {
        return battleUnit.getUnit().getSoldier().getName();
    }

    private void updateView() {
        soldierImageView.setImageResource(ResourceManager.getResourceForItem(battleUnit.getUnit().getSoldier()));
        weaponImageView.setImageResource(ResourceManager.getResourceForItem(battleUnit.getUnit().getWeapon()));
        armorImageView.setImageResource(ResourceManager.getResourceForItem(battleUnit.getUnit().getArmor()));
        nameView.setText(battleUnit.getUnit().getSoldier().getName());
        attackView.setText(valueOf(battleUnit.getUnit().getAttack()));
        defenseView.setText(valueOf(battleUnit.getUnit().getDefense()));
        hpView.setText(valueOf(battleUnit.getHp()));
        switch (state) {
            case NORMAL: linearLayout.setBackgroundResource(R.drawable.unit_background1);
                break;
            case SHOOTING: linearLayout.setBackgroundResource(R.drawable.unit_background_attack1);
                break;
            case TAKING_DAMAGE: linearLayout.setBackgroundResource(R.drawable.unit_background_damage1);
                break;
            default: throw new RuntimeException("Invalid state for UnitFragment object");
        }
        if(battleUnit.isDead()) {
            nameView.setTextColor(Color.RED);
        } else {
            nameView.setTextColor(Color.BLACK);
        }
    }

    private String valueOf(double value) {
        return String.valueOf((int) Math.floor(value));
    }
}




Java Source Code List

com.github.gobbisanches.ubisoldiers.app.ArmyFragment.java
com.github.gobbisanches.ubisoldiers.app.BattleActivity.java
com.github.gobbisanches.ubisoldiers.app.BattleFragment.java
com.github.gobbisanches.ubisoldiers.app.ItemListFragment.java
com.github.gobbisanches.ubisoldiers.app.MainActivity.java
com.github.gobbisanches.ubisoldiers.app.ResourceManager.java
com.github.gobbisanches.ubisoldiers.app.SingleFragmentActivity.java
com.github.gobbisanches.ubisoldiers.app.UbisoldierUosApplication.java
com.github.gobbisanches.ubisoldiers.app.UbisoldiersDriver.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationActivity.java
com.github.gobbisanches.ubisoldiers.app.UnitCustomizationFragment.java
com.github.gobbisanches.ubisoldiers.app.UnitFragment.java
com.github.gobbisanches.ubisoldiers.app.UosManager.java
com.github.gobbisanches.ubisoldiers.mechanics.Armor.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogEntry.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLogParser.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleLog.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleSquad.java
com.github.gobbisanches.ubisoldiers.mechanics.BattleUnit.java
com.github.gobbisanches.ubisoldiers.mechanics.Battle.java
com.github.gobbisanches.ubisoldiers.mechanics.DefaultGameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.GameRules.java
com.github.gobbisanches.ubisoldiers.mechanics.General.java
com.github.gobbisanches.ubisoldiers.mechanics.ItemCollection.java
com.github.gobbisanches.ubisoldiers.mechanics.Item.java
com.github.gobbisanches.ubisoldiers.mechanics.MechanicsEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.PolicyManager.java
com.github.gobbisanches.ubisoldiers.mechanics.SearchEngine.java
com.github.gobbisanches.ubisoldiers.mechanics.Soldier.java
com.github.gobbisanches.ubisoldiers.mechanics.Squad.java
com.github.gobbisanches.ubisoldiers.mechanics.Unit.java
com.github.gobbisanches.ubisoldiers.mechanics.Weapon.java