Changeset 245


Ignore:
Timestamp:
Jan 20, 2011, 1:02:56 AM (12 years ago)
Author:
sam
Message:

Swapping animation works.

Location:
trunk/monsterz
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/monsterz/board.cpp

    r243 r245  
    3535    Piece *pieces[8][8];
    3636    Piece *grabbed;
     37    Int2 src_cell, dst_cell;
    3738
    3839    Int2 mouse;
     
    101102                {
    102103                    data->grabbed = data->pieces[x / 48][y / 48];
     104                    data->src_cell = Int2(x / 48, y / 48);
     105                    data->dst_cell = Int2(-1);
    103106                    data->state = BoardData::GRAB;
    104107                }
     
    112115    case BoardData::GRAB:
    113116        if (mouse.x >= 0 && mouse.y >= 0)
     117        {
    114118            data->grabbed->Grab(mouse - data->mouse);
     119            Int2 cur_pos = data->grabbed->GetPos();
     120            Int2 cur_cell = (cur_pos + 24) / 48;
     121            if (cur_cell.i < 0 || cur_cell.i >= 8
     122                 || cur_cell.j < 0 || cur_cell.j >= 8
     123                 || (cur_pos - cur_cell * 48).sqlen() > 24 * 24)
     124                cur_cell = Int2(-1);
     125            /* If target cell changed, change destinations. */
     126            if (cur_cell != data->dst_cell)
     127            {
     128                if (data->dst_cell != Int2(-1))
     129                    data->pieces[data->dst_cell.i]
     130                                [data->dst_cell.j]->Goto(data->dst_cell * 48);
     131                if (cur_cell != Int2(-1)
     132                     && (cur_cell - data->src_cell).sqlen() == 1)
     133                    data->pieces[cur_cell.i]
     134                                [cur_cell.j]->Goto(data->src_cell * 48);
     135                data->dst_cell = cur_cell;
     136            }
     137        }
    115138        if (!buttons[0] || mouse.x < 0 || mouse.y < 0)
    116139        {
    117140            data->grabbed->Ungrab();
     141            if (data->dst_cell != Int2(-1))
     142                data->pieces[data->dst_cell.i]
     143                            [data->dst_cell.j]->Goto(data->dst_cell * 48);
    118144            data->state = BoardData::IDLE;
    119145        }
  • trunk/monsterz/piece.cpp

    r243 r245  
    3131    Game *game;
    3232    int tiler;
    33     Int2 cell, pos, tmp;
     33    Int2 cell, pos, src, dst;
    3434    int id;
    3535    float timer;
     
    4141        GRAB,
    4242        UNGRAB,
     43        PUSH,
    4344    }
    4445    state;
     
    6162}
    6263
     64Int2 Piece::GetPos() const
     65{
     66    return data->pos;
     67}
     68
    6369int Piece::Blink()
    6470{
     
    7985    {
    8086    case PieceData::UNGRAB:
     87    case PieceData::PUSH:
    8188        if ((data->cell * 48 - data->pos).sqlen() > 24 * 24)
    8289            return 0;
     
    100107        data->state = PieceData::UNGRAB;
    101108        data->timer = 0.0f;
    102         data->tmp = data->pos;
    103         return 1;
     109        data->src = data->pos;
     110        data->dst = data->cell * 48;
     111        return 1;
     112    default:
     113        return 0;
     114    }
     115}
     116
     117int Piece::Goto(Int2 pos)
     118{
     119    switch (data->state)
     120    {
     121    case PieceData::IDLE:
     122    case PieceData::BLINK:
     123    case PieceData::UNGRAB:
     124    case PieceData::PUSH:
     125        data->state = PieceData::PUSH;
     126        data->timer = 0.0f;
     127        data->src = data->pos;
     128        data->dst = pos;
     129        return 1;
     130    case PieceData::GRAB:
    104131    default:
    105132        return 0;
     
    121148        break;
    122149    case PieceData::UNGRAB:
     150    case PieceData::PUSH:
    123151        data->timer += deltams;
    124         Int2 trip = data->cell * 48 - data->tmp;
    125         float moving_time = trip.len() / 0.8f; /* Moving speed */
     152        Int2 trip = data->dst - data->src;
     153        float moving_time = trip.len() / 0.3f; /* Moving speed */
    126154        float t = moving_time ? data->timer / moving_time : 1.0f;
    127155        if (t >= 1.0f)
     
    129157        if (data->timer > moving_time + 200.0f)
    130158            data->state = PieceData::IDLE;
    131         data->pos = data->tmp + (t * trip + 0.5f);
     159        data->pos = data->src + (t * trip + 0.5f);
    132160        break;
    133161    }
     
    157185        break;
    158186    case PieceData::UNGRAB:
    159         if ((data->cell * 48 - data->tmp).sqlen() > 24 * 24)
     187        if ((data->cell * 48 - data->src).sqlen() > 24 * 24)
    160188            id = data->id + 2;
    161189        z = 4;
     190        break;
     191    case PieceData::PUSH:
     192        z = 3;
    162193        break;
    163194    }
  • trunk/monsterz/piece.h

    r243 r245  
    2323    virtual ~Piece();
    2424
     25    Int2 GetPos() const;
     26
    2527    int Blink();
    2628    int Grab(Int2 dir);
    2729    int Ungrab();
     30    int Goto(Int2 dir);
    2831
    2932protected:
Note: See TracChangeset for help on using the changeset viewer.