[Androidアプリ開発]マップサイズに合わせて移動範囲の制限

Android

Androidアプリ開発の4回目です。
今回はマップの広さに合わせて移動可能範囲を制限していきます。
これは条件式を1つ追加すればいいだけです。

  • 使用言語 java
  • 開発ツール AndroidStudio

変更点

  • Mapクラスに移動制限用の条件分岐を追加

前回drawメソッドの中に追記した、横移動用の加算式に条件式を適用します。

package com.example.dbd.daybyday;

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

public class Map {

    private final int ROWCOUNT = 20; //縦表示ドット数
    private final int COLUMNCOUNT = 10; //横表示ドット数
    private float dotX;  //1ドットの横幅
    private float dotY;  //1ドットの縦幅
    private Stage stage; //描画用にstageクラスの参照保持
    private Paint paint; //色保持用

    int xx;             //横位置保持用
    int xxPlus;         //変数xx加算用

    //表示マップの2次元配列
    private int[][] map = {
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
    };

    //コンストラクタ
    //画面サイズを引数で受け取り、ドットサイズを設定
    public Map(float viewX, float viewY,Stage stage){
        this.stage = stage;
        dotX = viewX / COLUMNCOUNT;
        dotY = viewY / ROWCOUNT;
        paint = new Paint();
        paint.setColor(Color.GREEN);
    }

    public void draw(){
        while(true) {
            if(xx + xxPlus <= map[0].length - COLUMNCOUNT && xx + xxPlus >= 0) { //加算後の値がマップの範囲を超えなければ
                xx += xxPlus;  //横移動
            }
            Canvas canvas = stage.holder.lockCanvas(); //画面をロックして
            canvas.drawColor(Color.WHITE); //画面をクリア
            //描画ドット数繰り返す
            for (int y = 0; y < ROWCOUNT; y++) {
                for (int x = 0; x < COLUMNCOUNT; x++) {
                    switch (map[y][x + xx]) {  //xxが描画開始インデックスを調整
                        case 1:     //配列の値が1の場合は
                            float drawX = x * dotX;  //描画開始位置のX座標を設定
                            float drawY = y * dotY;  //描画開始位置の座標を設定
                            canvas.drawRect(drawX, drawY, drawX + dotX, drawY + dotY, paint);
                            break;
                    }
                }
            }
            stage.holder.unlockCanvasAndPost(canvas); //画面に変更反映
        }

    }

    //以下はゲッター

    public int getROWCOUNT() {
        return ROWCOUNT;
    }

    public int getCOLUMNCOUNT() {
        return COLUMNCOUNT;
    }

    public float getDotX() {
        return dotX;
    }

    public float getDotY() {
        return dotY;
    }

    public int[][] getMap() {
        return map;
    }
}

これでマップの端まで移動すると止まるようになりました。
次はいよいよキャラ移動を作成していきます。
そのための布石としてMainCharクラスを作成しておきます。

変更点

  • MainCharクラスを作成
  • キャラの初期位置と初期サイズを設定
  • drawメソッドにキャラ描画処理を追加
package com.example.dbd.daybyday;

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

public class Map {

    private final int ROWCOUNT = 20; //縦表示ドット数
    private final int COLUMNCOUNT = 10; //横表示ドット数
    private float dotX;  //1ドットの横幅
    private float dotY;  //1ドットの縦幅
    private Stage stage; //描画用にstageクラスの参照保持
    private Paint paint; //色保持用
    private MainChar mainChar;  //キャラ描画用データ参照用

    int xx;             //横位置保持用
    int xxPlus;         //変数xx加算用

    //表示マップの2次元配列
    private int[][] map = {
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
    };

    //コンストラクタ
    //画面サイズを引数で受け取り、ドットサイズを設定
    public Map(float viewX, float viewY,Stage stage){
        this.stage = stage;
        dotX = viewX / COLUMNCOUNT;
        dotY = viewY / ROWCOUNT;
        paint = new Paint();
        paint.setColor(Color.GREEN);
        //MainCharクラスのインスタンス生成(初期設定値)
        mainChar = new MainChar(viewX,viewY,dotX,dotY);
    }

    public void draw(){
        while(true) {
            if(xx + xxPlus <= map[0].length - COLUMNCOUNT && xx + xxPlus >= 0) { //加算後の値がマップの範囲を超えなければ
                xx += xxPlus;  //横移動
            }
            Canvas canvas = stage.holder.lockCanvas(); //画面をロックして
            canvas.drawColor(Color.WHITE); //画面をクリア
            //描画ドット数繰り返す
            for (int y = 0; y < ROWCOUNT; y++) {
                for (int x = 0; x < COLUMNCOUNT; x++) {
                    switch (map[y][x + xx]) {  //xxが描画開始インデックスを調整
                        case 1:     //配列の値が1の場合は
                            float drawX = x * dotX;  //描画開始位置のX座標を設定
                            float drawY = y * dotY;  //描画開始位置の座標を設定
                            canvas.drawRect(drawX, drawY, drawX + dotX, drawY + dotY, paint);
                            break;
                    }
                }
            }
            float[] charPoint = mainChar.getCharPoint();    //キャラの位置取得
            canvas.drawRect(charPoint[0],charPoint[1],charPoint[0] + mainChar.getSizeX(), charPoint[1] + mainChar.getSizeY(), mainChar.getPaint()); //キャラ描画
            stage.holder.unlockCanvasAndPost(canvas); //画面に変更反映
        }

    }

    //以下はゲッター

    public int getROWCOUNT() {
        return ROWCOUNT;
    }

    public int getCOLUMNCOUNT() {
        return COLUMNCOUNT;
    }

    public float getDotX() {
        return dotX;
    }

    public float getDotY() {
        return dotY;
    }

    public int[][] getMap() {
        return map;
    }
}
package com.example.dbd.daybyday;

import android.graphics.Color;
import android.graphics.Paint;

/**
 * Created by isechi on 2017/09/02.
 */

public class MainChar {

    private float x;            //キャラ横位置
    private float y;            //キャラ縦位置
    private float sizeX;        //キャラ横サイズ
    private float sizeY;        //キャラ縦サイズ
    private Paint paint;         //キャラの色


    //コンストラクタ
    public MainChar(float viewX, float viewY, float dotX, float dotY){
        x = viewX / 2;     //キャラ初期位置設定
        y = viewY / 2;     //キャラ初期位置設定
        sizeX = dotX / 2;      //キャラ初期サイズ設定
        sizeY = dotY * 2;      //キャラ初期サイズ設定
        paint = new Paint();
        paint.setColor(Color.RED);  //キャラの色設定
    }


    //以下getter setter
    float[] getCharPoint(){
        float[] point = {x,y};

        return point;
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getSizeX() {
        return sizeX;
    }

    public void setSizeX(float sizeX) {
        this.sizeX = sizeX;
    }

    public float getSizeY() {
        return sizeY;
    }

    public void setSizeY(float sizeY) {
        this.sizeY = sizeY;
    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }
}

これで画面の中央にキャラ描画ができます。
次回はキャラの移動や重力適用を実装していきます。

コメント

タイトルとURLをコピーしました