是否进入防御塔范围,这里我们使用map来存int raduis = (int) Math.hypot(Math.abs(x), Math.abs(y));常用于勾股定理,如果在防御塔范围内,野怪就记录一下id,如果在两个防御塔内,就把两个防御塔的id记录一下,map的特性,不会有key重复 。也就是不会有防御塔重复攻击 。
public class BlameBean {/***使用map的好处是唯一* 被哪些防御塔攻击* */public Map<Integer,Integer> mapAttacksTower=new HashMap<>();
private void isAttacks(int position) {for (int j = 0; j < towerList.size(); j++) {int x = blameList.get(position).getX() - towerList.get(j).getX();int y = blameList.get(position).getY() - towerList.get(j).getY();int raduis = (int) Math.hypot(Math.abs(x), Math.abs(y));Map<Integer, Integer> mapAttacksTower = blameList.get(position).getMapAttacksTower();if (raduis < towerList.get(j).getRaduis() && blameList.get(position).getHP() > 0) {mapAttacksTower.put(towerList.get(j).getTowerId(), towerList.get(j).getTowerId());blameList.get(position).setMapAttacksTower(mapAttacksTower);} else {//移除防御塔//攻击到该野怪塔的集合for (Integer key : mapAttacksTower.keySet()) {if (mapAttacksTower.get(key) == towerList.get(j).getTowerId()) {mapAttacksTower.remove(key);blameList.get(position).setMapAttacksTower(mapAttacksTower);break;}}}}}
开炮动画,遍历野怪可被攻击的集合即可
//开炮动画if (blameList.get(i) != null && blameList.get(i).getMapAttacksTower().size() > 0) {Map<Integer, Integer> listAttacksTower = blameList.get(i).getMapAttacksTower();for (Integer j : listAttacksTower.keySet()) {shotMove(towerList.get(listAttacksTower.get(j)).getX(), towerList.get(listAttacksTower.get(j)).getY(), blameBean.getX(), blameBean.getY(), i, listAttacksTower.get(j));}}
1.5、炮弹动画判断如果可以攻击了,就开启一个从xy(防御塔),移动到x2y2 (野怪)的动画 ,动画结束后掉血 。动画开始时不可能再次开启,要符合防御塔一次只能攻击一个野怪的效果,这里开炮动画有点问题 , 就是视觉上老是打偏,有的时候炮弹慢的话,就会打在野怪身后 , 也没有好的解决方式 。博友有想法请留言 。
//炮弹动画private void shotMove(float x, float y, float x2, float y2, int blamePosition, int towerPosition) {if (!towerList.get(towerPosition).isAttacking()) {towerList.get(towerPosition).setAttacking(true);shotView = new ImageView(this.getContext());shotView.setImageDrawable(getContext().getDrawable(R.drawable.shot));shotView.layout(0, 0, 20, 20);addView(shotView);//开炮音效回调iShotService.shot();translateAnimation = new TranslateAnimation(x, x2, y, y2);translateAnimation.setDuration(towerList.get(0).getAttacksSpeed());translateAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {blameList.get(blamePosition).setHP(blameList.get(blamePosition).getHP() - towerList.get(towerPosition).getHarm());towerList.get(towerPosition).setAttacking(false);int childCount = getChildCount();if (childCount > 1) {removeView(getChildAt(childCount - 1));}}@Overridepublic void onAnimationRepeat(Animation animation) {}});shotView.startAnimation(translateAnimation);}}
我们需要控制开始与暂停和onResume下动画的释放等
public void start() {if (valueAnimator != null) {//开启动画addBlame();//addTower(450,450);valueAnimator.start();countDownTimer.start();}}public void pause() {if (valueAnimator != null) {//开启动画valueAnimator.pause();countDownTimer.cancel();}}/*** hasWindowFocus:true 获得焦点,开启动画;false 失去焦点,停止动画*/@Overridepublic void onWindowFocusChanged(boolean hasWindowFocus) {super.onWindowFocusChanged(hasWindowFocus);if (!hasWindowFocus) {if (valueAnimator != null) {//开启动画valueAnimator.pause();countDownTimer.cancel();countDownTimer = null;}if (countDownTimer != null) {countDownTimer.cancel();countDownTimer = null;}}}
总结:这里加入了新的背景图、多个防御塔随意摆放、一旦摆放就无法移动(后续加入拆除、升级)等功能 。难点还是在野怪移动上,还有多个防御塔攻击互相不影响 。
问题:现在的思路是刷新一下,野怪走一步,后续如果加入减速防御塔的话,应该怎么走呢,多个野怪如何做到行走速度互不影响呢 。
个人思路:可以正常野怪一次走5步 , 如果被减速防御塔打中后,就把5步见为2步,position+5调整为position+2 。这里只是记录学习View的过程,不要较真哦 。
推荐阅读
- 自定义映射resultMap
- OpenDataV低代码平台增加自定义属性编辑
- 星座牌怎么玩(星座塔罗牌怎么玩视频)
- 魔幻塔罗牌怎么玩(塔罗牌经典版怎么玩)
- 命运塔罗牌怎么玩啊,看不懂。切牌又是怎样啊
- jojo塔罗牌怎么玩(jojo塔罗牌代表什么)
- 塔罗牌该怎样入门(塔罗入门禁忌)
- 星宿魔法塔罗牌怎么玩(星宿魔法塔罗卡牌怎么玩)
- 神秘命运塔罗牌怎么玩(神秘命运塔罗牌78章怎么玩)
- vue3 自定义指令控制按钮权限