Changeset 241


Ignore:
Timestamp:
Jan 19, 2011, 9:34:02 PM (8 years ago)
Author:
sam
Message:

Delta-time the ungrab animation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/monsterz/piece.cpp

    r238 r241  
    3131    Game *game;
    3232    int tiler;
    33     int i, j, id, x, y;
     33    Int2 cell, pos, tmp;
     34    int id;
    3435    float timer;
    3536
     
    5455    Ticker::Ref(game);
    5556    data->tiler = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
    56     data->i = i;
    57     data->j = j;
    58     data->x = i * 48;
    59     data->y = j * 48;
     57    data->cell = Int2(i, j);
     58    data->pos = Int2(i * 48, j * 48);
    6059    data->id = id;
    6160    data->state = PieceData::IDLE;
     
    8382    case PieceData::GRAB:
    8483        data->state = PieceData::GRAB;
    85         data->x += dir.x;
    86         data->y += dir.y;
     84        data->pos = data->pos + dir;
    8785        return 1;
    8886    default:
     
    9795    case PieceData::GRAB:
    9896        data->state = PieceData::UNGRAB;
     97        data->timer = 0.0f;
     98        data->tmp = data->pos;
    9999        return 1;
    100100    default:
     
    117117        break;
    118118    case PieceData::UNGRAB:
    119         int dx = data->i * 48 - data->x;
    120         int dy = data->j * 48 - data->y;
    121         dx = dx + 2 / 3;
    122         dy = dy + 2 / 3;
    123         data->x += dx;
    124         data->y += dy;
    125         if (dx == 0 && dy == 0)
     119        data->timer += deltams;
     120        Int2 trip = data->cell * 48 - data->tmp;
     121        float total_time = trip.len() / 0.8f; /* Moving speed */
     122        float t = total_time ? data->timer / total_time : 1.0f;
     123        if (t >= 1.0f)
     124        {
     125            t = 1.0f;
    126126            data->state = PieceData::IDLE;
     127        }
     128        data->pos = data->tmp + (t * trip + Float2(0.5f));
    127129        break;
    128130    }
     
    135137
    136138    int id = data->id;
    137     int x = data->x + 24;
    138     int y = data->y + 177;
     139    int x = data->pos.x + 24;
     140    int y = data->pos.y + 177;
    139141    int z = 2;
    140     if (data->state == PieceData::BLINK)
     142
     143    /* Optional modifiers */
     144    switch (data->state)
     145    {
     146    case PieceData::IDLE:
     147        break;
     148    case PieceData::BLINK:
    141149        id = data->id + 1;
    142     if (data->state == PieceData::GRAB)
    143     {
     150        break;
     151    case PieceData::GRAB:
    144152        id = data->id + 1;
    145153        z = 4;
     154        break;
     155    case PieceData::UNGRAB:
     156        id = data->id + 2;
     157        z = 4;
     158        break;
    146159    }
    147160    data->game->GetScene()->AddTile((data->tiler << 16) | id, x, y, z, 0);
Note: See TracChangeset for help on using the changeset viewer.