[Androidアプリ開発]キャラの落下

Android

Androidアプリ開発の5回目です。
今回はキャラの落下を実装していきます。

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

変更点

  • Mapクラスに画面サイズ保持用のメンバ変数を追加(MainChar参照用)
  • MainCharクラスのgetCharPointメソッドの処理を追加
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 viewX;        //画面の横サイズ
    private float viewY;        //画面の縦サイズ
    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;
        this.viewX = viewX;
        this.viewY = viewY;
        dotX = viewX / COLUMNCOUNT;
        dotY = viewY / ROWCOUNT;
        paint = new Paint();
        paint.setColor(Color.GREEN);
        //MainCharクラスのインスタンス生成(初期設定値)
        mainChar = new MainChar(this);
    }

    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(xx);    //キャラの位置取得
            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 getViewX() {
        return viewX;
    }

    public void setViewX(float viewX) {
        this.viewX = viewX;
    }

    public float getViewY() {
        return viewY;
    }

    public void setViewY(float viewY) {
        this.viewY = viewY;
    }

    public float getDotX() {
        return dotX;
    }

    public void setDotX(float dotX) {
        this.dotX = dotX;
    }

    public float getDotY() {
        return dotY;
    }

    public void setDotY(float dotY) {
        this.dotY = dotY;
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public Paint getPaint() {
        return paint;
    }

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

    public MainChar getMainChar() {
        return mainChar;
    }

    public void setMainChar(MainChar mainChar) {
        this.mainChar = mainChar;
    }

    public int getXx() {
        return xx;
    }

    public void setXx(int xx) {
        this.xx = xx;
    }

    public int getXxPlus() {
        return xxPlus;
    }

    public void setXxPlus(int xxPlus) {
        this.xxPlus = xxPlus;
    }

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

    public void setMap(int[][] map) {
        this.map = 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;        //キャラの色

    private int fallVel;      //落下速度
    Map map;

    //コンストラクタ
    public MainChar(Map map){
        x = map.getViewX() / 2;     //キャラ初期位置設定
        y = map.getViewY() / 2;     //キャラ初期位置設定
        sizeX = map.getDotX() / 2;      //キャラ初期サイズ設定
        sizeY = map.getDotY() * 2;      //キャラ初期サイズ設定
        paint = new Paint();
        paint.setColor(Color.RED);  //キャラの色設定
        this.map = map;
    }


    //キャラの位置を更新して衝突判定後に現在位置を戻す
    float[] getCharPoint(int xx){
        //現在位置からmap配列の要素番号の割り出し
        int indexX = (int)(x / map.getDotX());
        int indexY = (int)((y + sizeY) / map.getDotY());
        if(map.getMap()[indexY][indexX + xx] == 0){
            fallVel++;
            y = y + fallVel;
        }
        float[] point = {x,y};

        return point;
    }

    //以下getter setter
    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をコピーしました