08 Android 植物人大战僵尸-添加向日葵卡片

1. 效果图

添加向日葵

2.思路

仿照这豌豆射手

2.1 点击状态栏的向日葵
package com.su.botanywarzombies.entity;

public class SeedFlower extends BaseModel implements TouchAble {

    @Override
    public boolean onTouch(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        // x,y坐标是否在触摸区域
        if (touchArea.contains(x, y)) {
            Log.d("sufadi", "touch seed flower");
            apply4EmplaceFlower();
            return true;
        }

        return false;
    }

    private void apply4EmplaceFlower() {
        GameView.getInstance().applyEmplacePlant(locationX, locationY, this);

    }
2.2 可拖动的向日葵图片
package com.su.botanywarzombies.view;

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


    public void applyEmplacePlant(int locationX, int locationY, BaseModel mBaseModel) {
        synchronized (mSurfaceHolder) {
            // 顶层加入被安放状态的植物
            // gameLayout1 表示正在安放植物的集合
            // 安放状态下一次只能放一个植物
            if (gameLayout1.size() < 1) {
                if (mBaseModel instanceof SeedPea) {
                    gameLayout1.add(new EmplacePea(locationX, locationY));
                } else if (mBaseModel instanceof SeedFlower) {
                    gameLayout1.add(new EmplaceFlower(locationX, locationY));
                }
            }
        }
    }
2.3 松开可拖动的向日葵卡片

MotionEvent.ACTION_UP 事件

package com.su.botanywarzombies.entity;

public class EmplaceFlower extends BaseModel implements TouchAble {

@Override
    public boolean onTouch(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        if (touchArea.contains(x, y)) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_MOVE:
                // 卡片位置更新,这里是卡片原点的变化,故是中心点(locationX, locationY)的变化
                locationX = x - Config.peaFlames[0].getWidth() / 2;
                locationY = y - Config.peaFlames[0].getHeight() / 2;

                // 触摸点跟随,重新移到新的位置
                touchArea.offsetTo(locationX, locationY);
                break;
            case MotionEvent.ACTION_UP:
                // 对象标志位失效,即死亡对象
                isLive = false;

                GameView.getInstance().applay4Plant(locationX, locationY, Config.TYPE_FLOWER);
                break;
            default:
                break;
            }
        }

        return false;
    }
2.4 可安放区域自定定位邻近位置
package com.su.botanywarzombies.view;

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

    public void applay4Plant(int locationX, int locationY, int type) {
        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(getPlant(point.x, point.y, key, type));
                        break;
                    case 1:
                        gameLayout4plant1.add(getPlant(point.x, point.y, key, type));
                        break;
                    case 2:
                        gameLayout4plant2.add(getPlant(point.x, point.y, key, type));
                        break;
                    case 3:
                        gameLayout4plant3.add(getPlant(point.x, point.y, key, type));
                        break;
                    case 4:
                        gameLayout4plant4.add(getPlant(point.x, point.y, key, type));
                        break;

                    default:
                        break;
                    }
                }
            }
        }

    }

    private BaseModel getPlant(int locationX, int locationY, int key, int type) {
        switch (type) {
        case Config.TYPE_PEA:
            return new Pea(locationX, locationY, key);
        case Config.TYPE_FLOWER:
            return new Flower(locationX, locationY, key);
        default:
            break;
        }

        return null;
    }

    private boolean isExist(int key, int raceIndex) {
        switch (raceIndex) {
        case 0:
            return hasMapIndex(key, gameLayout4plant0);
        case 1:
            return hasMapIndex(key, gameLayout4plant1);
        case 2:
            return hasMapIndex(key, gameLayout4plant2);
        case 3:
            return hasMapIndex(key, gameLayout4plant3);
        case 4:
            return hasMapIndex(key, gameLayout4plant4);
        default:
            break;
        }
        return false;
    }
2.5 一个不断点头的向日葵
package com.su.botanywarzombies.entity;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.su.botanywarzombies.constant.Config;
import com.su.botanywarzombies.model.BaseModel;

public class Flower extends BaseModel {

    private int farmeIndex = 0;
    // 禁止一个位置放2个动画卡片
    private int mapIndex;

    public Flower(int locationX, int locationY, int mapIndex) {
        this.locationX = locationX;
        this.locationY = locationY;
        isLive = true;
        this.mapIndex = mapIndex;
    }

    @Override
    public void drawSelf(Canvas canvas, Paint paint) {
        super.drawSelf(canvas, paint);
        if (isLive) {
            canvas.drawBitmap(Config.flowerFlames[farmeIndex], locationX, locationY, paint);

            /*
             * farmeIndex ++; if (farmeIndex == Config.peaFlames.length -1) {
             * farmeIndex = 0; }
             */
            farmeIndex = (++farmeIndex) % 8;
        }
    }

    @Override
    public int getMapIndex() {
        return mapIndex;
    }
}

3. Demo 下载

https://github.com/sufadi/BotanyWarZombies

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

法迪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值