Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.preko.thelastslayer.state;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.view.MotionEvent;
- import com.example.preko.thelastslayer.battle.controllelement.BattleController;
- import com.example.preko.thelastslayer.battle.graphics.BattleInterface;
- import com.example.preko.thelastslayer.battle.logic.Battle;
- import com.example.preko.thelastslayer.creature.Player;
- import com.example.preko.thelastslayer.core.Attributes;
- import com.example.preko.thelastslayer.core.StateStack;
- import org.json.JSONException;
- import java.util.ArrayList;
- /**
- * Created by Preko on 2017. 03. 15..
- */
- public class BattleState extends State {
- public BattleState(StateStack stateStack, Attributes attributes) {
- super(stateStack, attributes);
- }
- @Override
- public void render(Canvas canvas, Paint paint) {
- battleInterface.render(canvas, paint);
- }
- @Override
- public void handleEvent(MotionEvent event) {
- battleInterface.handleEvent(event);
- }
- @Override
- public void update(long deltaTime) {
- try {
- battleInterface.update(deltaTime);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- if(battleController.isNextTurn())
- battle.nextTurn();
- }
- @Override
- public void onBackPressed() {
- requestStatePush("ingamemenu");
- }
- @Override
- void init() {
- ArrayList<Integer> skillList = new ArrayList<>();
- skillList.add(0); skillList.add(1); skillList.add(2);
- int[] stats = {15, 0, 0, 0 };
- this.player01 = getAttributes().player;
- this.player02 = new Player(0, 0, 0, stats, skillList);
- player02.init(getAttributes());
- battleController = new BattleController(player01, player02);
- battle = new Battle(battleController ,player01, player02);
- battleInterface = new BattleInterface(getAttributes(), battleController);
- }
- @Override
- void resetStateElements() {
- }
- private Player player01;
- private Player player02;
- private Battle battle;
- private BattleController battleController;
- private BattleInterface battleInterface;
- }
- package com.example.preko.thelastslayer.battle.logic;
- import com.example.preko.thelastslayer.battle.controllelement.BattleController;
- import com.example.preko.thelastslayer.battle.controllelement.BattleEvent;
- import com.example.preko.thelastslayer.battle.controllelement.DamageType;
- import com.example.preko.thelastslayer.creature.Player;
- import com.example.preko.thelastslayer.skill.Skill;
- import com.example.preko.thelastslayer.skill.Target;
- /**
- * Created by Preko on 2017. 03. 15..
- */
- public class Battle {
- private BattleController controller;
- private Player player01;
- private Player player02;
- private Player attacker;
- private Player defender;
- public Battle(BattleController controller, Player player01, Player player02) {
- this.controller = controller;
- this.player01 = player01;
- this.player02 = player02;
- this.attacker = player01;
- this.defender = player02;
- }
- //simulate full battle end give back the winner..
- public Player simulateFullMatch() {
- while(player01.isAlive() && player02.isAlive()) {
- nextTurn();
- player01.attack(player02, DamageType.PHYSICAL);
- if(player02.isAlive())
- player02.attack(player01, DamageType.PHYSICAL);
- }
- return (player01.isAlive()) ? player01 : player02;
- }
- public void nextTurn() {
- if(controller.getOnGoingBattleEvent() == BattleEvent.ATTACK) {
- controller.setDamage(attacker.attack(defender, DamageType.PHYSICAL));
- }
- else if(controller.getOnGoingBattleEvent() == BattleEvent.DEF) {
- };
- if(controller.getOnGoingBattleEvent() == BattleEvent.USESKILL) {
- Player target = (controller.getUsedSkill().getTarget() == Target.Self) ? attacker : defender;
- attacker.useSkill(controller.getUsedSkill(), target);
- System.out.println(controller.getUsedSkill().getName());
- controller.setUsedSkill(null);
- };
- if(attacker == player01) {
- attacker = player02;
- defender = player01;
- } else {
- attacker = player01;
- defender = player02;
- }
- controller.setNextTurn(false);
- controller.setOnGoingBattleEvent(BattleEvent.NONE);
- controller.setCurrentPlayer(attacker);
- };
- public Player getPlayer01() {
- return player01;
- }
- public Player getPlayer02() {
- return player02;
- }
- }
- package com.example.preko.thelastslayer.battle.controllelement;
- import com.example.preko.thelastslayer.creature.Creature;
- import com.example.preko.thelastslayer.creature.Player;
- import com.example.preko.thelastslayer.skill.Skill;
- /**
- * Created by Preko on 2017. 03. 15..
- */
- public class BattleController {
- private Player player01;
- private Player player02;
- private Player currentPlayer;
- private BattleEvent onGoingBattleEvent;
- private Damage damage;
- private Skill usedSkill;
- private boolean nextTurn = false;
- public BattleController(Player player01, Player player02) {
- this.damage = new Damage();
- this.player01 = player01;
- this.player02 = player02;
- this.currentPlayer = player01;
- damage.setDamage(0);
- damage.setType(DamageType.NONE);
- damage.setOutcome(BattleTurnOutcome.None);
- usedSkill = null;
- }
- ////////////////////////////////////////////////////////////////////////////////////////
- public Player getCurrentPlayer() {
- return currentPlayer;
- }
- public void setCurrentPlayer(Player player) {
- currentPlayer = player;
- }
- public Damage getDamage() {
- return this.damage;
- }
- public void setDamage(Damage damage) {
- this.damage = damage;
- }
- public BattleEvent getOnGoingBattleEvent() {
- return onGoingBattleEvent;
- }
- public void setOnGoingBattleEvent(BattleEvent onGoingBattleEvent) {
- this.onGoingBattleEvent = onGoingBattleEvent;
- }
- public boolean isNextTurn() {
- return nextTurn;
- }
- public void setNextTurn(boolean nextTurn) {
- this.nextTurn = nextTurn;
- }
- public Player getPlayer01() {
- return player01;
- }
- public Player getPlayer02() {
- return player02;
- }
- public Skill getUsedSkill() {
- return usedSkill;
- }
- public void setUsedSkill(Skill usedSkill) {
- this.usedSkill = usedSkill;
- }
- }
- package com.example.preko.thelastslayer.battle.graphics;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.MotionEvent;
- import com.example.preko.thelastslayer.battle.controllelement.BattleController;
- import com.example.preko.thelastslayer.battle.controllelement.BattleEvent;
- import com.example.preko.thelastslayer.battle.controllelement.DamageType;
- import com.example.preko.thelastslayer.core.Attributes;
- import com.example.preko.thelastslayer.creature.Player;
- import com.example.preko.thelastslayer.creature.draw.CreatureRender;
- import com.example.preko.thelastslayer.essential.Relative;
- import com.example.preko.thelastslayer.essential.Text;
- import com.example.preko.thelastslayer.inteface.EventHandled;
- import com.example.preko.thelastslayer.inteface.Renderable;
- import com.example.preko.thelastslayer.inteface.Updateable;
- import com.example.preko.thelastslayer.uniue.ActionCircle;
- import com.example.preko.thelastslayer.uniue.Skillbar;
- import org.json.JSONException;
- /**
- * Created by Preko on 2017. 03. 15..
- */
- public class BattleInterface implements Updateable, Renderable, EventHandled {
- private BattleController controller;
- private Attributes attributes;
- public BattleInterface(Attributes attributes, BattleController controller) {
- this.controller = controller;
- this.attributes = attributes;
- actionCircle = new ActionCircle(attributes);
- damageText = null;
- outputType = null;
- outputType = new Text("0", 60, Color.BLACK,
- Relative.deviceWidth / 2 - Text.getBoundingRect("0", 60, attributes.paint).exactCenterX(),
- 600);
- damageText = new Text("0", 60, Color.BLACK,
- Relative.deviceWidth / 2 - Text.getBoundingRect("0", 60, attributes.paint).exactCenterX(),
- 700);
- player01 = new CreatureRender(attributes, controller.getPlayer01(), 25);
- player02 = new CreatureRender(attributes, controller.getPlayer02(), Relative.deviceHeight - 450);
- skillbar = new Skillbar();
- skillbar.setDisplayedPlayer(controller.getCurrentPlayer());
- }
- @Override
- public void handleEvent(MotionEvent motionEvent) {
- if(!skillbar.isVisible())
- actionCircle.handleEvent(motionEvent);
- skillbar.handleEvent(motionEvent);
- }
- @Override
- public void render(Canvas canvas, Paint paint) {
- player01.render(canvas, paint);
- player02.render(canvas, paint);
- outputType.render(canvas, paint);
- damageText.render(canvas, paint);
- if(!skillbar.isVisible())
- actionCircle.render(canvas, paint);
- skillbar.render(canvas, paint);
- }
- @Override
- public void update(long deltaTime) throws JSONException {
- player01.update(deltaTime);
- player02.update(deltaTime);
- if(actionCircle.getOnGoingAction() == BattleEvent.ATTACK.ordinal()) {
- controller.setOnGoingBattleEvent(BattleEvent.ATTACK);
- controller.setNextTurn(true);
- actionCircle.deactivate();
- skillbar.setDisplayedPlayer(controller.getCurrentPlayer());
- }
- if(actionCircle.getOnGoingAction() == BattleEvent.USESKILL.ordinal()) {
- skillbar.setVisiable(true);
- actionCircle.deactivate();
- }
- if(skillbar.haveSelected()) {
- controller.setUsedSkill(skillbar.getSelectedSkill());
- controller.setOnGoingBattleEvent(BattleEvent.USESKILL);
- controller.setNextTurn(true);
- skillbar.setDisplayedPlayer(controller.getCurrentPlayer());
- skillbar.deactivate();
- }
- if(controller.getDamage().getType() != DamageType.NONE) {
- damageText.setText(Integer.toString(controller.getDamage().getDamage()));
- switch (controller.getDamage().getOutcome()) {
- case Critical:
- outputType.setText("CRITICAL");
- break;
- case Evaded:
- outputType.setText("EVADED");
- break;
- case Normal:
- outputType.setText("NORMAL");
- break;
- }
- }
- }
- private Skillbar skillbar;
- private Text damageText;
- private Text outputType;
- private ActionCircle actionCircle;
- private CreatureRender player01;
- private CreatureRender player02;
- }
- package com.example.preko.thelastslayer.uniue;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.PointF;
- import android.graphics.RectF;
- import android.view.MotionEvent;
- import com.example.preko.thelastslayer.battle.controllelement.BattleController;
- import com.example.preko.thelastslayer.battle.controllelement.BattleEvent;
- import com.example.preko.thelastslayer.core.Attributes;
- import com.example.preko.thelastslayer.inteface.EventHandled;
- import com.example.preko.thelastslayer.inteface.Renderable;
- import com.example.preko.thelastslayer.inteface.Updateable;
- import com.example.preko.thelastslayer.rect.RectBitmap;
- import org.json.JSONException;
- /**
- * Created by Preko on 2017. 03. 22..
- */
- public class ActionCircle implements Renderable, EventHandled {
- //-1 nothing, 0 - attack, - 1 skillUse, - 2 defense;
- private int onGoingAction;
- private boolean visible;
- private RectBitmap attack;
- private RectBitmap useSkill;
- private RectBitmap defense;
- private RectF centerCancel;
- public ActionCircle(Attributes attributes) {
- centerCancel = new RectF();
- onGoingAction = -1;
- visible = false;
- attack = new RectBitmap(attributes.bitmapHolder.getBitmap("attack"), 0, 0);
- defense = new RectBitmap(attributes.bitmapHolder.getBitmap("defense"), 0, 0);
- useSkill = new RectBitmap(attributes.bitmapHolder.getBitmap("useskill"), 0, 0);
- }
- private void setCirclePosition(float x, float y) {
- float width = attack.getBoundingRectangle().width();
- float height = attack.getBoundingRectangle().height();
- attack.setPosition(x - width - 100, y - height);
- useSkill.setPosition(x + 100, y - height);
- defense.setPosition(x - width / 2 + 10, y + 200);
- centerCancel.set(x - width / 4, y - height/4, x + width / 4, y + height / 4);
- }
- @Override
- public void render(Canvas canvas, Paint paint) {
- if(visible) {
- attack.render(canvas, paint);
- useSkill.render(canvas, paint);
- defense.render(canvas, paint);
- }
- }
- @Override
- public void handleEvent(MotionEvent event) {
- PointF startPoint = new PointF(event.getX(), event.getY());
- RectF startRect = new RectF(startPoint.x-15, startPoint.y-15, startPoint.x + 15f, startPoint.y + 15);
- if(event.getAction() == MotionEvent.ACTION_DOWN) {
- visible = true;
- setCirclePosition(startPoint.x, startPoint.y);
- } else if (event.getAction() == MotionEvent.ACTION_UP){
- PointF endPoint = new PointF(event.getX(), event.getY());
- RectF endRect = new RectF(endPoint.x-15, endPoint.y-15, endPoint.x + 15f, endPoint.y + 15);
- visible = false;
- if(!RectF.intersects(endRect, centerCancel)) {
- if (RectF.intersects(startRect, attack.getBoundingRectangle())) {
- onGoingAction = 0;
- } else if (RectF.intersects(startRect, defense.getBoundingRectangle()))
- onGoingAction = 1;
- else if (RectF.intersects(startRect, useSkill.getBoundingRectangle()))
- onGoingAction = 2;
- else
- onGoingAction = -1;
- } else
- onGoingAction = -1;
- System.out.println(onGoingAction);
- }
- }
- public void deactivate() {
- onGoingAction = -1;
- visible = false;
- }
- public boolean isVisible() {
- return visible;
- }
- public void setVisible(boolean visible) {
- this.visible = visible;
- }
- public int getOnGoingAction() {
- return onGoingAction;
- }
- }
- package com.example.preko.thelastslayer.uniue;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.PointF;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.view.MotionEvent;
- import com.example.preko.thelastslayer.creature.Creature;
- import com.example.preko.thelastslayer.inteface.EventHandled;
- import com.example.preko.thelastslayer.inteface.Renderable;
- import com.example.preko.thelastslayer.inteface.Updateable;
- import com.example.preko.thelastslayer.rect.RectBitmap;
- import com.example.preko.thelastslayer.skill.Skill;
- import org.json.JSONException;
- import java.util.ArrayList;
- /**
- * Created by Preko on 2017. 03. 22..
- */
- public class Skillbar implements Renderable, EventHandled, Updateable {
- private ArrayList<RectBitmap> skillBar;
- private Skill selectedSkill = null;
- private Creature creature;
- private boolean visible = false;
- public Skillbar() {
- this.skillBar = new ArrayList<>();
- }
- public boolean isVisible() {
- return this.visible;
- }
- public void deactivate() {
- this.visible = false;
- this.selectedSkill = null;
- }
- public void setVisiable(boolean visible) {
- this.visible = visible;
- }
- public void setDisplayedPlayer(Creature creature) {
- this.creature = creature;
- skillBar.clear();
- for(int i = 0; i < creature.getSkills().size(); i++)
- skillBar.add(new RectBitmap(creature.getSkills().get(i).getBitmap(), 15 + 185 * i, 1100));
- selectedSkill = null;
- }
- public boolean haveSelected() {
- return (selectedSkill == null) ? false : true;
- }
- public Skill getSelectedSkill() {
- return selectedSkill;
- }
- @Override
- public void handleEvent(MotionEvent event) {
- if(visible) {
- PointF startPoint = new PointF(event.getX(), event.getY());
- RectF startRect = new RectF(startPoint.x - 15, startPoint.y - 15, startPoint.x + 15f, startPoint.y + 15);
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- for (RectBitmap bitmap : skillBar) {
- if (RectF.intersects(startRect, bitmap.getBoundingRectangle()))
- selectedSkill = creature.getSkills().get(skillBar.indexOf(bitmap));
- }
- }
- }
- }
- @Override
- public void render(Canvas canvas, Paint paint) {
- if(visible) {
- for (RectBitmap bitmap : skillBar)
- bitmap.render(canvas, paint);
- }
- }
- @Override
- public void update(long deltaTime) throws JSONException {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement