06 Android 植物人大战僵尸-安放卡片时自动定位邻近区域

1.安放卡片时自动定位邻近区域

效果和基本原理如下

网格图片

2.基本思路

1. 背景图切割,如上图,例如多少行多少列

这里记录可安放的有效区域

package com.su.botanywarzombies.view;

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {


    private void creatElement() {
        ......

        // 初始化可安放卡片的所有有效区域
        final int LINE = 5;
        final int ROW = 9;
        final int TOTAL_ROW = 11;
        final int TOTAL_LINE = 6;
        for (int i = 0; i < LINE; i++) {
            for (int j = 0; j < ROW; j++) {
                int x = (j + 2) * Config.screenWidth / TOTAL_ROW - Config.screenWidth / TOTAL_ROW / 2;
                int y = (i + 1) * Config.screenHeight / TOTAL_LINE;
                Point mPoint = new Point(x, y);

                Config.plantPoint.put(i * (ROW + 1) + j, mPoint);

                if (j == 0) {
                    // 记录每一个可安放区域的跑道Y坐标
                    Config.racWayYpoint[i] = (i + 1) * Config.screenHeight / TOTAL_LINE;
                }
            }

        }
2. 就近原则安放卡片
  1. 事件触发判断,在手指离开屏幕的时候判断
package com.su.botanywarzombies.entity;

    @Override
    public boolean onTouch(MotionEvent event) {
        ....
        case MotionEvent.ACTION_UP:
            // 对象标志位失效,即死亡对象
            isLive = false;

            GameView.getInstance().applay4Plant(locationX, locationY, this);
            break;
  1. 就近原则安放卡片

      1. 两个卡片中心点距离是一个单元格宽度,这里我们取两个卡片直接画中心线
      1. 可放置卡片偏向那,自动放置哪里
package com.su.botanywarzombies.view;

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {


    public void applay4Plant(int locationX, int locationY, EmplacePea emplacePea) {
        synchronized (mSurfaceHolder) {
            // 当前卡片中心坐标与可安放集合最近的坐标点
            for (Integer key : Config.plantPoint.keySet()) {
                // 1. 两个卡片中心点距离是一个单元格宽度,这里我们取两个卡片直接画中心线
                // 2. 可放置卡片偏向那,自动放置哪里
                final int TOTAL_ROW = 11;
                final int TOTAL_LINE = 6;
                Point point = Config.plantPoint.get(key);
                if ((Math.abs(locationX - point.x) < Config.screenWidth / TOTAL_ROW / 2) && (Math.abs(locationY - point.y) < Config.screenHeight / TOTAL_LINE / 2)) {
                    int raceIndex = TOTAL_LINE;

                    for (int i = 0; i < Config.racWayYpoint.length; i++) {
                        if (point.y == Config.racWayYpoint[i]) {
                            raceIndex = i;
                        }
                    }

                    if (isExist(key, raceIndex)) {
                        return;
                    }

                    switch (raceIndex) {
                    case 0:
                        gameLayout4plant0.add(new Pea(point.x, point.y, key));
                        break;
                    case 1:
                        gameLayout4plant1.add(new Pea(point.x, point.y, key));
                        break;
                    case 2:
                        gameLayout4plant2.add(new Pea(point.x, point.y, key));
                        break;
                    case 3:
                        gameLayout4plant3.add(new Pea(point.x, point.y, key));
                        break;
                    case 4:
                        gameLayout4plant4.add(new Pea(point.x, point.y, key));
                        break;

                    default:
                        break;
                    }
                }
            }
        }

    }
  1. 卡片图层绘制

这里使用了 5 行,5 个跑道

package com.su.botanywarzombies.view;

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    // 安放植物 跑道1
    private ArrayList<BaseModel> gameLayout4plant0;
    // 安放植物 跑道2
    private ArrayList<BaseModel> gameLayout4plant1;
    // 安放植物 跑道3
    private ArrayList<BaseModel> gameLayout4plant2;
    // 安放植物 跑道4
    private ArrayList<BaseModel> gameLayout4plant3;
    // 安放植物 跑道5
    private ArrayList<BaseModel> gameLayout4plant4;

    private void creatElement() {
        gameLayout4plant0 = new ArrayList<BaseModel>();
        gameLayout4plant1 = new ArrayList<BaseModel>();
        gameLayout4plant2 = new ArrayList<BaseModel>();
        gameLayout4plant3 = new ArrayList<BaseModel>();
        gameLayout4plant4 = new ArrayList<BaseModel>();


    @Override
    public void run() {
        while (gameRunFlag) {

        ....

            for (BaseModel model : gameLayout4plant0) {
                model.drawSelf(mCanvas, mPaint);
            }

            for (BaseModel model : gameLayout4plant1) {
                model.drawSelf(mCanvas, mPaint);
            }

            for (BaseModel model : gameLayout4plant2) {
                model.drawSelf(mCanvas, mPaint);
            }

            for (BaseModel model : gameLayout4plant3) {
                model.drawSelf(mCanvas, mPaint);
            }

            for (BaseModel model : gameLayout4plant4) {
                model.drawSelf(mCanvas, mPaint);
            }

            for (BaseModel model : gameLayout2) {
                model.drawSelf(mCanvas, mPaint);
            }

            // 后画的会覆盖先画的,故位置在gameLayout2下面
            if (gameLayout1 != null && !gameLayout1.isEmpty()) {
                for (BaseModel model : gameLayout1) {
                    model.drawSelf(mCanvas, mPaint);
                }
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值