Feb 26, 2013

Movement of a player

Just my way to adjust player view depends on keys pressed.
That are indexes of our angles:



// let's define keys, which will move player (keyCode, state)

private static var keys:Array<Array<Int>> = [[37,0],[39,0],[38,0],[40,0],[69,0],[65,0],[68,0],[87,0],[83,0]];
// define array, where angles will be stored
private var angles:Array<Int>;
// adjust angles to special indexes

private function Init(e:Event):Void // Event.ADDED_TO_STAGE
{
    angles = new Array<Int>();
    angles[1] = 0;
    angles[3] = 45;
    angles[2] = 90;
    angles[6] = 135;
    angles[4] = 180;
    angles[11] = 225;
    angles[7] = 270;
    angles[8] = 315;
}


public function on_key_down(e:KeyboardEvent):Void // KeyboardEvent.KEY_DOWN
{
    for(key in keys)
        if(e.keyCode == key[0] && key[1] == 0)
            key[1] = 1;
}

public function on_key_up(e:KeyboardEvent):Void // KeyboardEvent.KEY_UP
{
    for(key in keys)
        if(e.keyCode == key[0])
            key[1] = 0;
}



public function on_enter_frame(e:Event):Void // Event.ENTER_FRAME
{
    var r_index:Int = 0;
    // up - down
    if(keys[2][1] == 1 || keys[7][1] == 1) {
        r_index += 1;
    }else if(keys[3][1] == 1 || keys[8][1] == 1){
        r_index += 4;
    }
    // left - right
    if(keys[0][1] == 1 || keys[5][1] == 1)
    {
        r_index += 7;
    }else if(keys[1][1] == 1 || keys[6][1] == 1){
        r_index +=2;
    }
    if(r_index != 0)
        this.rotation = angles[r_index];
}

What is pros of that way for me:
- you can define not only array of angles, but array of Bitmaps or Sprites
- you can redefine array of angles (Sprites) on the way without any of code overhead
- cleaner code as for me (just two if - else)

Aug 30, 2012

SimpleCache in HaXe

Made SimpleCache class based on Ant.Karlov tutorial using HaXe.
Here is class code:
package mmg;

class SimpleCache<t>
{
    public var instance(get, never):T;

    private var _targetClass:Class<t>;
    private var _currentIndex:Int;
    private var _instances:Array<t>;

    public function new(targetClass:Class<t>, initialCapacity:Int)
    {
        _targetClass = targetClass; // Base class of all of objects
        _currentIndex = initialCapacity - 1; // Index of current free object
        _instances = []; // Array of all instances

        // Fill it up
        for (i in 0...initialCapacity)
        {
            _instances[i] = getNewInstance();
        }
    }

    private function getNewInstance():T
    {
        return Type.createEmptyInstance(_targetClass);
    }

    private function get():T
    {
        if (_currentIndex >= 0)
        {
            // Returning free object from cache
            _currentIndex--;
            return _instances[_currentIndex + 1];
        }
        else
        {
            // If cache is empty we should create one more instance
            return getNewInstance();
        }
    }

    private function put(instance:T):Void
    {
        _currentIndex++;
        // If cache is overfilled
        if (_currentIndex == _instances.length)
        {
            // We should put instance to the back of the instances array
            _instances[_instances.length] = instance;
        }
        else
        {
            // Putting instance to the free cell of array
            _instances[_currentIndex] = instance;
        }
    }
}
Usage example:
// init cache
var cache:SimpleCache<ourobjectclass> = new SimpleCache(OurObjectClass, 50); 
// take on of instances
var tmp:OurObjectClass = cache.instance; 
tmp.doSomething();
// put object back to cache
cache.put(tmp);