欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-30 10:29:33
...

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

效果和基本原理如下

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

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);
                }
            }
相关标签: 自动安放卡片