Changeset 737 for trunk/monsterz


Ignore:
Timestamp:
Mar 13, 2011, 1:22:55 AM (9 years ago)
Author:
sam
Message:

monsterz: some refactoring in the Piece and Board classes.

Location:
trunk/monsterz
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/monsterz/board.cpp

    r735 r737  
    217217            /* Mouse is still in the window, keep grabbing */
    218218            data->grabbed.piece->Grab(mousepos - data->oldmouse);
    219             vec2i cur_pos = data->grabbed.piece->GetPos();
     219            vec2i cur_pos = data->grabbed.piece->GetPos()
     220                          + data->grabbed.piece->GetOffset();
    220221            vec2i cur_cell = (cur_pos + data->size / 2) / data->size;
    221222            if (cur_cell.i < 0 || cur_cell.i >= data->dim.i
     
    242243        }
    243244        if (!buttons[0] || mousepos.x == -1
    244              || (data->src_cell * data->size
    245                   - data->grabbed.piece->GetPos()).sqlen() > 100 * 100)
     245             || (data->src_cell * data->size - data->grabbed.piece->GetPos()
     246                  - data->grabbed.piece->GetOffset()).sqlen() > 100 * 100)
    246247        {
    247248            /* Mouse released, or exited window, or dragged too far. */
  • trunk/monsterz/piece.cpp

    r686 r737  
    3737    Emitter *emitter;
    3838    Piece *above, *below;
    39     vec2i size, cell, pos, src, dst;
     39    vec2i size, cell, pos, pos_src, pos_dst, off, off_src;
    4040    int id;
    41     float speed, timer, blink, idle;
     41    float speed, timer;
     42
     43    struct
     44    {
     45        /* The idle loop */
     46        float idle, idle_max;
     47        /* Piece is blinking */
     48        float blink;
     49        /* Piece is popping */
     50        float pop;
     51    }
     52    timers;
    4253
    4354    enum
    4455    {
     56        /* Piece is idle, at its normal position */
    4557        IDLE,
    46         BLINK,
     58        /* Piece is being grabbed */
    4759        GRAB,
     60        /* Piece was released after a grab */
    4861        UNGRAB,
    4962        MOVE,
     
    7184    data->cell = cell;
    7285    data->state = PieceData::IDLE;
    73     data->timer = RandF(DELAY_IDLE);
    74     data->blink = RandF(DELAY_BLINK);
    75     data->idle = RandF(DELAY_IDLE, 2 * DELAY_IDLE);
     86    data->timers.idle = 0.0f;
     87    data->timers.idle_max = RandF(DELAY_IDLE);
     88    data->timers.blink = RandF(2 * DELAY_BLINK);
     89    data->timers.pop = 0.0f;
    7690
    7791    switch (data->piece)
     
    90104
    91105    data->pos = data->cell * data->size;
     106    data->off = 0;
    92107}
    93108
     
    99114}
    100115
    101 void Piece::SetCell(vec2i cell)
    102 {
    103     data->cell = cell;
    104 }
    105 
    106 vec2i Piece::GetCell() const
    107 {
    108     return data->cell;
    109 }
    110 
    111 void Piece::SetPos(vec2i pos)
    112 {
    113     data->pos = pos;
    114 }
    115 
    116 vec2i Piece::GetPos() const
    117 {
    118     return data->pos;
    119 }
    120 
    121 vec2i Piece::GetSize() const
    122 {
    123     return data->size;
    124 }
     116void Piece::SetCell(vec2i cell) { data->cell = cell; }
     117vec2i Piece::GetCell() const { return data->cell; }
     118
     119void Piece::SetPos(vec2i pos) { data->pos = pos; }
     120vec2i Piece::GetPos() const { return data->pos; }
     121
     122vec2i Piece::GetOffset() const { return data->off; }
     123vec2i Piece::GetSize() const { return data->size; }
    125124
    126125vec2i Piece::GetShift() const
    127126{
    128     return data->pos - data->cell * data->size;
     127    return data->pos + data->off - data->cell * data->size;
    129128}
    130129
     
    187186    {
    188187    case PieceData::IDLE:
    189     case PieceData::BLINK:
    190188    case PieceData::GRAB:
    191189    case PieceData::UNGRAB:
     
    193191    case PieceData::POP:
    194192        data->state = PieceData::POP;
    195         data->timer = DELAY_DUH + DELAY_POP;
     193        data->timers.pop = DELAY_DUH + DELAY_POP;
    196194        return 1;
    197195    default:
     
    207205    case PieceData::MOVE:
    208206        /* Only allow swaps when piece is close enough to its target cell. */
    209         if ((data->cell * data->size - data->pos).sqlen()
    210               > data->size.sqlen() / 8)
     207        if (GetShift().sqlen() > data->size.sqlen() / 8)
    211208            return 0;
    212209        /* Fall through */
    213210    case PieceData::IDLE:
    214     case PieceData::BLINK:
    215211    case PieceData::GRAB:
    216212        data->state = PieceData::GRAB;
    217         data->pos = data->pos + dir;
     213        data->off += dir;
    218214        return 1;
    219215    default:
     
    227223    {
    228224    case PieceData::IDLE:
    229     case PieceData::BLINK:
    230225    case PieceData::GRAB:
    231226    case PieceData::UNGRAB:
     
    234229        data->speed = 0.4f;
    235230        data->timer = 0.0f;
    236         data->src = data->pos;
    237         data->dst = pos;
     231        data->off = data->off_src = data->off + data->pos - pos;
     232        data->pos = data->pos_src = data->pos_dst = pos;
    238233        return 1;
    239234    default:
     
    247242    {
    248243    case PieceData::IDLE:
    249     case PieceData::BLINK:
    250244    case PieceData::UNGRAB:
    251245    case PieceData::MOVE:
     
    253247        data->speed = 0.3f;
    254248        data->timer = 0.0f;
    255         data->src = data->pos;
    256         data->dst = pos;
     249        data->pos_src = data->pos;
     250        data->pos_dst = pos;
     251        data->off_src = data->off;
    257252        return 1;
    258253    case PieceData::GRAB:
     
    264259void Piece::TickGame(float deltams)
    265260{
    266     switch (data->state)
    267     {
    268     case PieceData::IDLE:
    269         data->timer -= deltams;
    270         if (data->timer < 0.0f)
    271             data->timer = data->idle = RandF(DELAY_IDLE, 2 * DELAY_IDLE);
    272         data->blink -= deltams;
    273         if (data->blink < 0.0f)
    274         {
    275             data->state = PieceData::BLINK;
    276             data->timer = DURATION_BLINK;
    277         }
    278         break;
    279     case PieceData::BLINK:
    280         data->timer -= deltams;
    281         if (data->timer < 0.0f)
    282         {
    283             data->state = PieceData::IDLE;
    284             data->timer = data->idle = RandF(DELAY_IDLE, 2 * DELAY_IDLE);
    285             data->blink = RandF(DELAY_BLINK, 2 * DELAY_BLINK);
    286         }
    287         break;
     261    data->timers.blink -= deltams;
     262    if (data->timers.blink < 0.0f)
     263        data->timers.blink = RandF(DELAY_BLINK, 2 * DELAY_BLINK);
     264
     265    data->timers.idle -= deltams;
     266    if (data->timers.idle < 0.0f)
     267        data->timers.idle = data->timers.idle_max
     268                          = RandF(DELAY_IDLE, 2 * DELAY_IDLE);
     269
     270    data->timers.pop -= deltams;
     271
     272    switch (data->state)
     273    {
     274    case PieceData::IDLE:
    288275    case PieceData::GRAB:
    289276        break;
     
    292279    {
    293280        data->timer += deltams;
    294         vec2i trip = data->dst - data->src;
    295         float moving_time = trip.len() / data->speed;
    296         float t = moving_time ? data->timer / moving_time : 1.0f;
     281
     282        vec2i trip = data->pos_dst - data->pos_src;
     283        float pos_time = trip.len() / data->speed;
     284        float t = pos_time ? data->timer / pos_time : 1.0f;
    297285        if (t >= 1.0f)
    298286            t = 1.0f;
    299         data->pos = data->src + (t * trip + 0.5f);
     287        data->pos = data->pos_src + (t * trip + 0.5f);
     288
     289        float off_time = data->off_src.len() / data->speed;
     290        t = off_time ? data->timer / off_time : 1.0f;
     291        if (t >= 1.0f)
     292            t = 1.0f;
     293        data->off = (1.0f - t) * data->off_src;
     294
    300295        /* If the piece below is blocking us, clamp our position and
    301296         * start falling again. */
     
    304299        {
    305300            data->pos.y = data->below->data->pos.y + data->size.y;
    306             data->src = data->pos;
     301            data->pos_src = data->pos;
     302            data->off_src = data->off;
    307303            data->timer = 0.0f;
    308304        }
    309         if (data->timer > moving_time + 200.0f)
    310         {
     305        if (data->timer > pos_time + 200.0f && data->timer > off_time + 200.0f)
    311306            data->state = PieceData::IDLE;
    312             data->timer = data->idle = RandF(DELAY_IDLE, 2 * DELAY_IDLE);
    313         }
    314307        break;
    315308    }
    316309    case PieceData::POP:
    317         data->timer -= deltams;
    318         if (data->timer < 0.0f)
     310        /* If piece has finished its pop animation, spawn particles and
     311         * kill the piece. */
     312        if (data->timers.pop < 0.0f)
    319313        {
    320314            vec3 pos(data->pos.x + 24, data->pos.y + 72, 5);
     
    333327        break;
    334328    case PieceData::DEAD:
     329        /* If piece is dead, there's nothing we can do. */
    335330        break;
    336331    }
     
    343338
    344339    int id = data->id, off = 0;
    345     int x = data->pos.x + 24;
    346     int y = data->pos.y + 72;
     340    int x = data->pos.x + data->off.x + 24;
     341    int y = data->pos.y + data->off.y + 72;
    347342    int z = 2;
    348343
     
    352347    case PieceData::IDLE:
    353348    {
    354         off = data->timer * 3 / data->idle;
     349        off = data->timers.idle * 3 / data->timers.idle_max;
    355350        if (off < 0) off = 0;
    356351        if (off > 2) off = 2;
    357         break;
    358     }
    359     case PieceData::BLINK:
     352        if (data->timers.blink < DURATION_BLINK) off = 3;
     353        break;
     354    }
     355    case PieceData::GRAB:
    360356        off = 3;
    361         break;
    362     case PieceData::GRAB:
    363         off = 3;
    364         z = 4;
    365         break;
    366     case PieceData::UNGRAB:
    367         if ((data->cell * data->size - data->src).sqlen()
     357        z = 10;
     358        break;
     359    case PieceData::UNGRAB:
     360        if ((data->cell * data->size - data->pos_src).sqlen()
    368361              > data->size.sqlen() / 8)
    369362            off = 7;
    370         z = 4;
     363        z = 8;
    371364        break;
    372365    case PieceData::MOVE:
     
    375368    case PieceData::POP:
    376369    {
    377         off = 4 - (int)(data->timer * 5 / DELAY_POP);
     370        off = 4 - (int)(data->timers.pop * 5 / DELAY_POP);
    378371        if (off < 0) off = 0;
    379372        if (off > 4) off = 4;
     
    399392
    400393    if (data->state == PieceData::GRAB)
    401         Scene::GetDefault()->AddTile((data->tiler << 16) | 0, x, y, 5, 0);
     394        Scene::GetDefault()->AddTile((data->tiler << 16) | 0, x, y, 9, 0);
    402395}
    403396
  • trunk/monsterz/piece.h

    r664 r737  
    4141    vec2i GetPos() const;
    4242
     43    vec2i GetOffset() const;
    4344    vec2i GetSize() const;
    4445    vec2i GetShift() const;
Note: See TracChangeset for help on using the changeset viewer.