Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.56 KB | None | 0 0
  1. package com.example.preko.thelastslayer.state;
  2.  
  3. import android.graphics.Canvas;
  4. import android.graphics.Paint;
  5. import android.view.MotionEvent;
  6.  
  7. import com.example.preko.thelastslayer.battle.controllelement.BattleController;
  8. import com.example.preko.thelastslayer.battle.graphics.BattleInterface;
  9. import com.example.preko.thelastslayer.battle.logic.Battle;
  10. import com.example.preko.thelastslayer.creature.Player;
  11. import com.example.preko.thelastslayer.core.Attributes;
  12. import com.example.preko.thelastslayer.core.StateStack;
  13.  
  14. import org.json.JSONException;
  15.  
  16. import java.util.ArrayList;
  17.  
  18. /**
  19.  * Created by Preko on 2017. 03. 15..
  20.  */
  21.  
  22. public class BattleState extends State {
  23.     public BattleState(StateStack stateStack, Attributes attributes)  {
  24.         super(stateStack, attributes);
  25.     }
  26.     @Override
  27.     public void render(Canvas canvas, Paint paint) {
  28.         battleInterface.render(canvas, paint);
  29.     }
  30.     @Override
  31.     public void handleEvent(MotionEvent event) {
  32.         battleInterface.handleEvent(event);
  33.     }
  34.     @Override
  35.     public void update(long deltaTime) {
  36.         try {
  37.             battleInterface.update(deltaTime);
  38.         } catch (JSONException e) {
  39.             e.printStackTrace();
  40.         }
  41.         if(battleController.isNextTurn())
  42.             battle.nextTurn();
  43.     }
  44.     @Override
  45.     public void onBackPressed() {
  46.         requestStatePush("ingamemenu");
  47.     }
  48.     @Override
  49.     void init()  {
  50.         ArrayList<Integer> skillList = new ArrayList<>();
  51.         skillList.add(0); skillList.add(1); skillList.add(2);
  52.         int[] stats = {15, 0, 0, 0 };
  53.         this.player01 = getAttributes().player;
  54.         this.player02 = new Player(0, 0, 0, stats, skillList);
  55.         player02.init(getAttributes());
  56.         battleController = new BattleController(player01, player02);
  57.         battle = new Battle(battleController ,player01, player02);
  58.         battleInterface = new BattleInterface(getAttributes(), battleController);
  59.     }
  60.     @Override
  61.     void resetStateElements() {
  62.  
  63.     }
  64.  
  65.     private Player player01;
  66.     private Player player02;
  67.     private Battle battle;
  68.     private BattleController battleController;
  69.     private BattleInterface battleInterface;
  70. }
  71.  
  72. package com.example.preko.thelastslayer.battle.logic;
  73.  
  74. import com.example.preko.thelastslayer.battle.controllelement.BattleController;
  75. import com.example.preko.thelastslayer.battle.controllelement.BattleEvent;
  76. import com.example.preko.thelastslayer.battle.controllelement.DamageType;
  77. import com.example.preko.thelastslayer.creature.Player;
  78. import com.example.preko.thelastslayer.skill.Skill;
  79. import com.example.preko.thelastslayer.skill.Target;
  80.  
  81. /**
  82.  * Created by Preko on 2017. 03. 15..
  83.  */
  84.  
  85. public class Battle {
  86.  
  87.     private BattleController controller;
  88.  
  89.     private Player player01;
  90.     private Player player02;
  91.  
  92.     private Player attacker;
  93.     private Player defender;
  94.  
  95.     public Battle(BattleController controller, Player player01, Player player02) {
  96.         this.controller = controller;
  97.         this.player01 = player01;
  98.         this.player02 = player02;
  99.         this.attacker = player01;
  100.         this.defender = player02;
  101.     }
  102.     //simulate full battle end give back the winner..
  103.     public Player simulateFullMatch() {
  104.         while(player01.isAlive() && player02.isAlive()) {
  105.             nextTurn();
  106.             player01.attack(player02, DamageType.PHYSICAL);
  107.             if(player02.isAlive())
  108.                 player02.attack(player01, DamageType.PHYSICAL);
  109.         }
  110.         return (player01.isAlive()) ? player01 : player02;
  111.     }
  112.  
  113.     public void nextTurn() {
  114.         if(controller.getOnGoingBattleEvent() == BattleEvent.ATTACK) {
  115.             controller.setDamage(attacker.attack(defender, DamageType.PHYSICAL));
  116.         }
  117.         else if(controller.getOnGoingBattleEvent() == BattleEvent.DEF) {
  118.  
  119.         };
  120.         if(controller.getOnGoingBattleEvent() == BattleEvent.USESKILL) {
  121.             Player target = (controller.getUsedSkill().getTarget() == Target.Self) ? attacker : defender;
  122.             attacker.useSkill(controller.getUsedSkill(), target);
  123.             System.out.println(controller.getUsedSkill().getName());
  124.             controller.setUsedSkill(null);
  125.         };
  126.         if(attacker == player01) {
  127.             attacker = player02;
  128.             defender = player01;
  129.         } else {
  130.             attacker = player01;
  131.             defender = player02;
  132.         }
  133.         controller.setNextTurn(false);
  134.         controller.setOnGoingBattleEvent(BattleEvent.NONE);
  135.         controller.setCurrentPlayer(attacker);
  136.     };
  137.     public Player getPlayer01() {
  138.         return player01;
  139.     }
  140.     public Player getPlayer02() {
  141.         return player02;
  142.     }
  143. }
  144.  
  145. package com.example.preko.thelastslayer.battle.controllelement;
  146.  
  147. import com.example.preko.thelastslayer.creature.Creature;
  148. import com.example.preko.thelastslayer.creature.Player;
  149. import com.example.preko.thelastslayer.skill.Skill;
  150.  
  151. /**
  152.  * Created by Preko on 2017. 03. 15..
  153.  */
  154.  
  155. public class BattleController {
  156.     private Player player01;
  157.     private Player player02;
  158.     private Player currentPlayer;
  159.     private BattleEvent onGoingBattleEvent;
  160.  
  161.     private Damage damage;
  162.     private Skill usedSkill;
  163.  
  164.     private boolean nextTurn = false;
  165.  
  166.     public BattleController(Player player01, Player player02) {
  167.         this.damage = new Damage();
  168.         this.player01 = player01;
  169.         this.player02 = player02;
  170.         this.currentPlayer = player01;
  171.         damage.setDamage(0);
  172.         damage.setType(DamageType.NONE);
  173.         damage.setOutcome(BattleTurnOutcome.None);
  174.         usedSkill = null;
  175.     }
  176.     ////////////////////////////////////////////////////////////////////////////////////////
  177.     public Player getCurrentPlayer() {
  178.         return currentPlayer;
  179.     }
  180.     public void setCurrentPlayer(Player player) {
  181.         currentPlayer = player;
  182.     }
  183.     public Damage getDamage() {
  184.         return this.damage;
  185.     }
  186.  
  187.     public void setDamage(Damage damage) {
  188.         this.damage = damage;
  189.     }
  190.  
  191.     public BattleEvent getOnGoingBattleEvent() {
  192.         return onGoingBattleEvent;
  193.     }
  194.  
  195.     public void setOnGoingBattleEvent(BattleEvent onGoingBattleEvent) {
  196.         this.onGoingBattleEvent = onGoingBattleEvent;
  197.     }
  198.  
  199.     public boolean isNextTurn() {
  200.         return nextTurn;
  201.     }
  202.     public void setNextTurn(boolean nextTurn) {
  203.         this.nextTurn = nextTurn;
  204.     }
  205.  
  206.     public Player getPlayer01() {
  207.         return player01;
  208.     }
  209.     public Player getPlayer02() {
  210.         return player02;
  211.     }
  212.  
  213.     public Skill getUsedSkill() {
  214.         return usedSkill;
  215.     }
  216.  
  217.     public void setUsedSkill(Skill usedSkill) {
  218.         this.usedSkill = usedSkill;
  219.     }
  220. }
  221.  
  222. package com.example.preko.thelastslayer.battle.graphics;
  223.  
  224. import android.graphics.Canvas;
  225. import android.graphics.Color;
  226. import android.graphics.Paint;
  227. import android.view.MotionEvent;
  228.  
  229. import com.example.preko.thelastslayer.battle.controllelement.BattleController;
  230. import com.example.preko.thelastslayer.battle.controllelement.BattleEvent;
  231. import com.example.preko.thelastslayer.battle.controllelement.DamageType;
  232. import com.example.preko.thelastslayer.core.Attributes;
  233. import com.example.preko.thelastslayer.creature.Player;
  234. import com.example.preko.thelastslayer.creature.draw.CreatureRender;
  235. import com.example.preko.thelastslayer.essential.Relative;
  236. import com.example.preko.thelastslayer.essential.Text;
  237. import com.example.preko.thelastslayer.inteface.EventHandled;
  238. import com.example.preko.thelastslayer.inteface.Renderable;
  239. import com.example.preko.thelastslayer.inteface.Updateable;
  240. import com.example.preko.thelastslayer.uniue.ActionCircle;
  241. import com.example.preko.thelastslayer.uniue.Skillbar;
  242.  
  243. import org.json.JSONException;
  244.  
  245. /**
  246.  * Created by Preko on 2017. 03. 15..
  247.  */
  248.  
  249. public class BattleInterface implements Updateable, Renderable, EventHandled {
  250.  
  251.     private BattleController controller;
  252.     private Attributes attributes;
  253.     public BattleInterface(Attributes attributes, BattleController controller) {
  254.         this.controller = controller;
  255.         this.attributes = attributes;
  256.         actionCircle = new ActionCircle(attributes);
  257.         damageText = null;
  258.         outputType = null;
  259.         outputType = new Text("0", 60, Color.BLACK,
  260.                 Relative.deviceWidth / 2 - Text.getBoundingRect("0", 60, attributes.paint).exactCenterX(),
  261.                 600);
  262.         damageText = new Text("0", 60, Color.BLACK,
  263.                 Relative.deviceWidth / 2 - Text.getBoundingRect("0", 60, attributes.paint).exactCenterX(),
  264.                 700);
  265.         player01 = new CreatureRender(attributes, controller.getPlayer01(), 25);
  266.         player02 = new CreatureRender(attributes, controller.getPlayer02(), Relative.deviceHeight - 450);
  267.         skillbar = new Skillbar();
  268.         skillbar.setDisplayedPlayer(controller.getCurrentPlayer());
  269.     }
  270.  
  271.     @Override
  272.     public void handleEvent(MotionEvent motionEvent) {
  273.         if(!skillbar.isVisible())
  274.             actionCircle.handleEvent(motionEvent);
  275.         skillbar.handleEvent(motionEvent);
  276.     }
  277.  
  278.     @Override
  279.     public void render(Canvas canvas, Paint paint) {
  280.         player01.render(canvas, paint);
  281.         player02.render(canvas, paint);
  282.         outputType.render(canvas, paint);
  283.         damageText.render(canvas, paint);
  284.         if(!skillbar.isVisible())
  285.             actionCircle.render(canvas, paint);
  286.         skillbar.render(canvas, paint);
  287.     }
  288.  
  289.     @Override
  290.     public void update(long deltaTime) throws JSONException {
  291.         player01.update(deltaTime);
  292.         player02.update(deltaTime);
  293.  
  294.         if(actionCircle.getOnGoingAction() == BattleEvent.ATTACK.ordinal()) {
  295.             controller.setOnGoingBattleEvent(BattleEvent.ATTACK);
  296.             controller.setNextTurn(true);
  297.             actionCircle.deactivate();
  298.             skillbar.setDisplayedPlayer(controller.getCurrentPlayer());
  299.         }
  300.  
  301.         if(actionCircle.getOnGoingAction() == BattleEvent.USESKILL.ordinal()) {
  302.             skillbar.setVisiable(true);
  303.             actionCircle.deactivate();
  304.         }
  305.         if(skillbar.haveSelected()) {
  306.             controller.setUsedSkill(skillbar.getSelectedSkill());
  307.             controller.setOnGoingBattleEvent(BattleEvent.USESKILL);
  308.             controller.setNextTurn(true);
  309.             skillbar.setDisplayedPlayer(controller.getCurrentPlayer());
  310.             skillbar.deactivate();
  311.         }
  312.         if(controller.getDamage().getType() != DamageType.NONE) {
  313.             damageText.setText(Integer.toString(controller.getDamage().getDamage()));
  314.             switch (controller.getDamage().getOutcome()) {
  315.                 case Critical:
  316.                     outputType.setText("CRITICAL");
  317.                     break;
  318.                 case Evaded:
  319.                     outputType.setText("EVADED");
  320.                     break;
  321.                 case Normal:
  322.                     outputType.setText("NORMAL");
  323.                     break;
  324.             }
  325.         }
  326.     }
  327.  
  328.     private Skillbar skillbar;
  329.     private Text damageText;
  330.     private Text outputType;
  331.     private ActionCircle actionCircle;
  332.     private CreatureRender player01;
  333.     private CreatureRender player02;
  334. }
  335.  
  336. package com.example.preko.thelastslayer.uniue;
  337.  
  338. import android.graphics.Canvas;
  339. import android.graphics.Paint;
  340. import android.graphics.PointF;
  341. import android.graphics.RectF;
  342. import android.view.MotionEvent;
  343.  
  344. import com.example.preko.thelastslayer.battle.controllelement.BattleController;
  345. import com.example.preko.thelastslayer.battle.controllelement.BattleEvent;
  346. import com.example.preko.thelastslayer.core.Attributes;
  347. import com.example.preko.thelastslayer.inteface.EventHandled;
  348. import com.example.preko.thelastslayer.inteface.Renderable;
  349. import com.example.preko.thelastslayer.inteface.Updateable;
  350. import com.example.preko.thelastslayer.rect.RectBitmap;
  351.  
  352. import org.json.JSONException;
  353.  
  354. /**
  355.  * Created by Preko on 2017. 03. 22..
  356.  */
  357.  
  358. public class ActionCircle implements Renderable, EventHandled {
  359.     //-1 nothing, 0 - attack, - 1 skillUse, - 2 defense;
  360.     private int onGoingAction;
  361.     private boolean visible;
  362.     private RectBitmap attack;
  363.     private RectBitmap useSkill;
  364.     private RectBitmap defense;
  365.     private RectF centerCancel;
  366.     public ActionCircle(Attributes attributes) {
  367.         centerCancel = new RectF();
  368.         onGoingAction = -1;
  369.         visible = false;
  370.         attack = new RectBitmap(attributes.bitmapHolder.getBitmap("attack"), 0, 0);
  371.         defense = new RectBitmap(attributes.bitmapHolder.getBitmap("defense"), 0, 0);
  372.         useSkill = new RectBitmap(attributes.bitmapHolder.getBitmap("useskill"), 0, 0);
  373.     }
  374.  
  375.     private void setCirclePosition(float x, float y) {
  376.         float width = attack.getBoundingRectangle().width();
  377.         float height = attack.getBoundingRectangle().height();
  378.         attack.setPosition(x - width - 100, y - height);
  379.         useSkill.setPosition(x + 100, y - height);
  380.         defense.setPosition(x - width / 2 + 10, y + 200);
  381.         centerCancel.set(x - width / 4, y - height/4, x + width / 4, y + height / 4);
  382.     }
  383.  
  384.     @Override
  385.     public void render(Canvas canvas, Paint paint) {
  386.            if(visible) {
  387.                attack.render(canvas, paint);
  388.                useSkill.render(canvas, paint);
  389.                defense.render(canvas, paint);
  390.            }
  391.     }
  392.  
  393.     @Override
  394.     public void handleEvent(MotionEvent event) {
  395.         PointF startPoint = new PointF(event.getX(), event.getY());
  396.         RectF startRect  = new RectF(startPoint.x-15, startPoint.y-15, startPoint.x + 15f, startPoint.y + 15);
  397.         if(event.getAction() == MotionEvent.ACTION_DOWN) {
  398.             visible = true;
  399.             setCirclePosition(startPoint.x, startPoint.y);
  400.         } else if (event.getAction() == MotionEvent.ACTION_UP){
  401.             PointF endPoint = new PointF(event.getX(), event.getY());
  402.             RectF endRect  = new RectF(endPoint.x-15, endPoint.y-15, endPoint.x + 15f, endPoint.y + 15);
  403.             visible = false;
  404.             if(!RectF.intersects(endRect, centerCancel)) {
  405.                 if (RectF.intersects(startRect, attack.getBoundingRectangle())) {
  406.                     onGoingAction = 0;
  407.                 } else if (RectF.intersects(startRect, defense.getBoundingRectangle()))
  408.                     onGoingAction = 1;
  409.                 else if (RectF.intersects(startRect, useSkill.getBoundingRectangle()))
  410.                     onGoingAction = 2;
  411.                 else
  412.                     onGoingAction = -1;
  413.             } else
  414.                 onGoingAction = -1;
  415.             System.out.println(onGoingAction);
  416.         }
  417.     }
  418.  
  419.     public void deactivate() {
  420.         onGoingAction = -1;
  421.         visible = false;
  422.     }
  423.  
  424.     public boolean isVisible() {
  425.         return visible;
  426.     }
  427.  
  428.     public void setVisible(boolean visible) {
  429.         this.visible = visible;
  430.     }
  431.  
  432.     public int getOnGoingAction() {
  433.         return onGoingAction;
  434.     }
  435. }
  436.  
  437. package com.example.preko.thelastslayer.uniue;
  438.  
  439. import android.graphics.Canvas;
  440. import android.graphics.Paint;
  441. import android.graphics.PointF;
  442. import android.graphics.Rect;
  443. import android.graphics.RectF;
  444. import android.view.MotionEvent;
  445.  
  446. import com.example.preko.thelastslayer.creature.Creature;
  447. import com.example.preko.thelastslayer.inteface.EventHandled;
  448. import com.example.preko.thelastslayer.inteface.Renderable;
  449. import com.example.preko.thelastslayer.inteface.Updateable;
  450. import com.example.preko.thelastslayer.rect.RectBitmap;
  451. import com.example.preko.thelastslayer.skill.Skill;
  452.  
  453. import org.json.JSONException;
  454.  
  455. import java.util.ArrayList;
  456.  
  457. /**
  458.  * Created by Preko on 2017. 03. 22..
  459.  */
  460.  
  461. public class Skillbar implements Renderable, EventHandled, Updateable {
  462.     private ArrayList<RectBitmap> skillBar;
  463.     private Skill selectedSkill = null;
  464.     private Creature creature;
  465.     private boolean visible = false;
  466.     public Skillbar() {
  467.         this.skillBar = new ArrayList<>();
  468.     }
  469.  
  470.     public boolean isVisible() {
  471.         return this.visible;
  472.     }
  473.  
  474.     public void deactivate() {
  475.         this.visible = false;
  476.         this.selectedSkill = null;
  477.     }
  478.  
  479.     public void setVisiable(boolean visible) {
  480.         this.visible = visible;
  481.     }
  482.  
  483.     public void setDisplayedPlayer(Creature creature) {
  484.         this.creature = creature;
  485.         skillBar.clear();
  486.         for(int i = 0; i < creature.getSkills().size(); i++)
  487.             skillBar.add(new RectBitmap(creature.getSkills().get(i).getBitmap(), 15 + 185 * i, 1100));
  488.         selectedSkill = null;
  489.     }
  490.  
  491.     public boolean haveSelected() {
  492.         return (selectedSkill == null) ? false : true;
  493.     }
  494.     public Skill getSelectedSkill() {
  495.         return selectedSkill;
  496.     }
  497.     @Override
  498.     public void handleEvent(MotionEvent event) {
  499.         if(visible) {
  500.             PointF startPoint = new PointF(event.getX(), event.getY());
  501.             RectF startRect = new RectF(startPoint.x - 15, startPoint.y - 15, startPoint.x + 15f, startPoint.y + 15);
  502.             if (event.getAction() == MotionEvent.ACTION_DOWN) {
  503.                 for (RectBitmap bitmap : skillBar) {
  504.                     if (RectF.intersects(startRect, bitmap.getBoundingRectangle()))
  505.                         selectedSkill = creature.getSkills().get(skillBar.indexOf(bitmap));
  506.                 }
  507.             }
  508.         }
  509.     }
  510.  
  511.     @Override
  512.     public void render(Canvas canvas, Paint paint) {
  513.         if(visible) {
  514.             for (RectBitmap bitmap : skillBar)
  515.                 bitmap.render(canvas, paint);
  516.         }
  517.     }
  518.  
  519.     @Override
  520.     public void update(long deltaTime) throws JSONException {
  521.  
  522.     }
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement