source: trunk/monsterz/piece.cpp @ 259

Last change on this file since 259 was 259, checked in by sam, 12 years ago

Slightly change the ...Data initialisation in classes.

File size: 4.3 KB
RevLine 
[226]1//
2// Monsterz
3//
4// Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16#include <cmath>
17
18#include "core.h"
19#include "piece.h"
20#include "monsterz.h"
21
22/*
23 * Piece implementation class
24 */
25
26class PieceData
27{
28    friend class Piece;
29
30private:
31    Game *game;
32    int tiler;
[245]33    Int2 cell, pos, src, dst;
[241]34    int id;
[230]35    float timer;
36
37    enum
38    {
39        IDLE,
40        BLINK,
[238]41        GRAB,
42        UNGRAB,
[245]43        PUSH,
[230]44    }
45    state;
[226]46};
47
48/*
49 * Public Piece class
50 */
51
[243]52Piece::Piece(Game *game, Int2 cell, int id)
[259]53  : data(new PieceData())
[226]54{
55    data->game = game;
56    Ticker::Ref(game);
57    data->tiler = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
[243]58    data->cell = cell;
59    data->pos = cell * 48;
[227]60    data->id = id;
[230]61    data->state = PieceData::IDLE;
[226]62}
63
[248]64void Piece::SetCell(Int2 cell)
65{
66    data->cell = cell;
67}
68
[245]69Int2 Piece::GetPos() const
70{
71    return data->pos;
72}
73
[258]74int Piece::GetId() const
75{
76    return data->id;
77}
78
[238]79int Piece::Blink()
[230]80{
[238]81    switch (data->state)
[230]82    {
[238]83    case PieceData::IDLE:
[230]84        data->state = PieceData::BLINK;
[243]85        data->timer = 400.0f; /* Duration of a blink */
[238]86        return 1;
87    default:
88        return 0;
[230]89    }
90}
91
[258]92int Piece::Pop()
93{
94    return 0;
95}
96
[238]97int Piece::Grab(Int2 dir)
98{
99    switch (data->state)
100    {
[243]101    case PieceData::UNGRAB:
[245]102    case PieceData::PUSH:
[243]103        if ((data->cell * 48 - data->pos).sqlen() > 24 * 24)
104            return 0;
105        /* Fall through */
[238]106    case PieceData::IDLE:
107    case PieceData::BLINK:
108    case PieceData::GRAB:
109        data->state = PieceData::GRAB;
[241]110        data->pos = data->pos + dir;
[238]111        return 1;
112    default:
113        return 0;
114    }
115}
116
117int Piece::Ungrab()
118{
119    switch (data->state)
120    {
121    case PieceData::GRAB:
122        data->state = PieceData::UNGRAB;
[241]123        data->timer = 0.0f;
[245]124        data->src = data->pos;
125        data->dst = data->cell * 48;
[238]126        return 1;
127    default:
128        return 0;
129    }
130}
131
[245]132int Piece::Goto(Int2 pos)
133{
134    switch (data->state)
135    {
136    case PieceData::IDLE:
137    case PieceData::BLINK:
138    case PieceData::UNGRAB:
139    case PieceData::PUSH:
140        data->state = PieceData::PUSH;
141        data->timer = 0.0f;
142        data->src = data->pos;
143        data->dst = pos;
144        return 1;
145    case PieceData::GRAB:
146    default:
147        return 0;
148    }
149}
150
[226]151void Piece::TickGame(float deltams)
152{
[230]153    switch (data->state)
154    {
155    case PieceData::IDLE:
156        break;
157    case PieceData::BLINK:
158        data->timer -= deltams;
159        if (data->timer < 0.0f)
160            data->state = PieceData::IDLE;
161        break;
[238]162    case PieceData::GRAB:
163        break;
164    case PieceData::UNGRAB:
[245]165    case PieceData::PUSH:
[241]166        data->timer += deltams;
[245]167        Int2 trip = data->dst - data->src;
168        float moving_time = trip.len() / 0.3f; /* Moving speed */
[243]169        float t = moving_time ? data->timer / moving_time : 1.0f;
[241]170        if (t >= 1.0f)
171            t = 1.0f;
[243]172        if (data->timer > moving_time + 200.0f)
[238]173            data->state = PieceData::IDLE;
[250]174        t = t * t * 3 - t * t * t * 2;
175        t = t * t * 3 - t * t * t * 2;
[245]176        data->pos = data->src + (t * trip + 0.5f);
[238]177        break;
[230]178    }
[226]179    Entity::TickGame(deltams);
180}
181
182void Piece::TickDraw(float deltams)
183{
184    Entity::TickDraw(deltams);
185
[227]186    int id = data->id;
[241]187    int x = data->pos.x + 24;
188    int y = data->pos.y + 177;
[238]189    int z = 2;
[241]190
191    /* Optional modifiers */
192    switch (data->state)
[238]193    {
[241]194    case PieceData::IDLE:
195        break;
196    case PieceData::BLINK:
[238]197        id = data->id + 1;
[241]198        break;
199    case PieceData::GRAB:
200        id = data->id + 1;
[238]201        z = 4;
[241]202        break;
203    case PieceData::UNGRAB:
[245]204        if ((data->cell * 48 - data->src).sqlen() > 24 * 24)
[243]205            id = data->id + 2;
[241]206        z = 4;
207        break;
[245]208    case PieceData::PUSH:
209        z = 3;
210        break;
[238]211    }
[243]212
[238]213    data->game->GetScene()->AddTile((data->tiler << 16) | id, x, y, z, 0);
[243]214
215    if (data->state == PieceData::GRAB)
[250]216        data->game->GetScene()->AddTile((data->tiler << 16) | 0, x, y, 5, 0);
[226]217}
218
219Piece::~Piece()
220{
221    Ticker::Unref(data->game);
222    Tiler::Deregister(data->tiler);
223    delete data;
224}
225
Note: See TracBrowser for help on using the repository browser.