強火で進め

このブログではプログラム関連の記事を中心に書いてます。

Walker.csのJavaScript版

RagePixelに付属しているデモで使用している Walker.cs のJavaScript版。これを参考にすれば他のRagePixelのAPIについてもJavaScriptで使えるかと思います。

全ファイルJavaScriptに移植するのは面倒臭いのでベースとなる部分についてはC#のものをそのまま使います。

この様にJavaScriptC#を連携する場合はそのままだと上手くいかないので専用のフォルダに移動させます。今回は Standard Assets フォルダを使います。

Unity Script Reference – Overview: Script compilation (Advanced)
http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html

まずは Code のフォルダをルートの位置に移動させる為、何も無い所にドラッグします。

するとこの様な並びになります。

次に Code フォルダを Standard Assets にリネームします。

これでRagePixelのAPIJavaScriptから呼べる様になりました。

Walker.cs の代わりのJavaScriptはこの様な記述となります。これでC#版と同様な処理が可能です。

#pragma strict
#pragma downcast

private var ragePixel: IRagePixel;

enum WalkingState {Standing=0, WalkRight, WalkLeft};
var state: WalkingState = WalkingState.Standing;
public var arrowLeft: RagePixelSprite;
public var arrowRight: RagePixelSprite;

public var walkingSpeed: float = 10f;

function Start () {
	ragePixel = GetComponent("RagePixelSprite");
}

function Update () {
    //Check the keyboard state and set the character state accordingly
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        state = WalkingState.WalkLeft;
    }
    else if (Input.GetKey(KeyCode.RightArrow))
    {
        state = WalkingState.WalkRight;
    }
    else
    {
        state = WalkingState.Standing;
    }

    var moveDirection: Vector3 = new Vector3();
    
    switch (state)
    {
        case(WalkingState.Standing):
            //Reset the horizontal flip for clarity
            ragePixel.SetHorizontalFlip(false);
            ragePixel.PlayNamedAnimation("STAY", false);
            if (arrowLeft != null) arrowLeft.SetTintColor(Color.gray);
            if (arrowRight != null) arrowRight.SetTintColor(Color.gray);
            break;

        case (WalkingState.WalkLeft):
            //Flip horizontally. Our animation is drawn to walk right.
            ragePixel.SetHorizontalFlip(true);
            //PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
            ragePixel.PlayNamedAnimation("WALK", false);
            //Move direction. X grows right so left is -1.
            moveDirection = new Vector3(-1f, 0f, 0f);
            if (arrowLeft != null) arrowLeft.SetTintColor(Color.white);
            if (arrowRight != null) arrowRight.SetTintColor(Color.gray);
            break;

        case (WalkingState.WalkRight):
            //Not flipping horizontally. Our animation is drawn to walk right.
            ragePixel.SetHorizontalFlip(false);
            //PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
            ragePixel.PlayNamedAnimation("WALK", false);
            //Move direction. X grows right so left is +1.
            moveDirection = new Vector3(1f, 0f, 0f);
            if (arrowLeft != null) arrowLeft.SetTintColor(Color.gray);
            if (arrowRight != null) arrowRight.SetTintColor(Color.white);
            break;
    }

    //Move the sprite into moveDirection at walkingSpeed pixels/sec
    transform.Translate(moveDirection * Time.deltaTime * walkingSpeed);
}