Unity + ex2D + iTween: 床がないと下に落ちるプレイヤー

床がないと下に落ちるプレイヤー。
http://dl.dropbox.com/u/5435697/Unity/2011-11-29-2/2011-11-29-2.html

セル座標と配列の数字が、わかりにくくなってしまった。配列に合わせて、左上を(0,0)にするのがよさそう。


@script RequireComponent(WallGenerator)

var player : GameObject;
var cell : int = 160;
var moveTime : float = 0.3;
var px : int = 1;
var py : int = 1;

private var state : String = "stop";
private var timer : float = 0;
private var vx : int = 0;
private var vy : int = 0;

private var wg : WallGenerator;

function Start () {
iTween.MoveTo(player, {"x":(px+0.5)*cell, "y":(py+0.5)*cell, "time":0});
wg = GetComponent(WallGenerator) as WallGenerator;
}

function Update () {

if (state == "stop") {

if (wg.wallArray[7-(py-1)][px] == 0) {
py -= 1;
state = "move";
timer = moveTime;
}

else {
vx = vy = 0;
if (Input.GetKey("left")) vx = -1;
else if (Input.GetKey("right")) vx = 1;
//else if (Input.GetKey("down")) vy = -1;
//else if (Input.GetKey("up")) vy = 1;

if (vx != 0 || vy != 0) {
if (wg.wallArray[7-(py+vy)][px+vx] == 0) {
px += vx;
py += vy;
state = "move";
timer = moveTime;
}
}
}
}

if (state == "move") {

timer -= Time.deltaTime;
iTween.MoveTo(player, {"x":(px+0.5)*cell, "y":(py+0.5)*cell, "time":moveTime});

if (timer vx = vy = 0;
state = "stop";
}
}

//Debug.Log(player.transform.position.x + ", " + player.transform.position.y);
//Debug.Log(px + ", " + py);
}

参考:パズルゲームアルゴリズムマニアックス

コメントを残す