source: trunk/monsterz/piece.cpp @ 258

Last change on this file since 258 was 258, checked in by sam, 10 years ago

Implement valid move checks and early piece disappearance.

File size: 4.3 KB
Line 
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;
33    Int2 cell, pos, src, dst;
34    int id;
35    float timer;
36
37    enum
38    {
39        IDLE,
40        BLINK,
41        GRAB,
42        UNGRAB,
43        PUSH,
44    }
45    state;
46};
47
48/*
49 * Public Piece class
50 */
51
52Piece::Piece(Game *game, Int2 cell, int id)
53{
54    data = new PieceData();
55    data->game = game;
56    Ticker::Ref(game);
57    data->tiler = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
58    data->cell = cell;
59    data->pos = cell * 48;
60    data->id = id;
61    data->state = PieceData::IDLE;
62}
63
64void Piece::SetCell(Int2 cell)
65{
66    data->cell = cell;
67}
68
69Int2 Piece::GetPos() const
70{
71    return data->pos;
72}
73
74int Piece::GetId() const
75{
76    return data->id;
77}
78
79int Piece::Blink()
80{
81    switch (data->state)
82    {
83    case PieceData::IDLE:
84        data->state = PieceData::BLINK;
85        data->timer = 400.0f; /* Duration of a blink */
86        return 1;
87    default:
88        return 0;
89    }
90}
91
92int Piece::Pop()
93{
94    return 0;
95}
96
97int Piece::Grab(Int2 dir)
98{
99    switch (data->state)
100    {
101    case PieceData::UNGRAB:
102    case PieceData::PUSH:
103        if ((data->cell * 48 - data->pos).sqlen() > 24 * 24)
104            return 0;
105        /* Fall through */
106    case PieceData::IDLE:
107    case PieceData::BLINK:
108    case PieceData::GRAB:
109        data->state = PieceData::GRAB;
110        data->pos = data->pos + dir;
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;
123        data->timer = 0.0f;
124        data->src = data->pos;
125        data->dst = data->cell * 48;
126        return 1;
127    default:
128        return 0;
129    }
130}
131
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
151void Piece::TickGame(float deltams)
152{
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;
162    case PieceData::GRAB:
163        break;
164    case PieceData::UNGRAB:
165    case PieceData::PUSH:
166        data->timer += deltams;
167        Int2 trip = data->dst - data->src;
168        float moving_time = trip.len() / 0.3f; /* Moving speed */
169        float t = moving_time ? data->timer / moving_time : 1.0f;
170        if (t >= 1.0f)
171            t = 1.0f;
172        if (data->timer > moving_time + 200.0f)
173            data->state = PieceData::IDLE;
174        t = t * t * 3 - t * t * t * 2;
175        t = t * t * 3 - t * t * t * 2;
176        data->pos = data->src + (t * trip + 0.5f);
177        break;
178    }
179    Entity::TickGame(deltams);
180}
181
182void Piece::TickDraw(float deltams)
183{
184    Entity::TickDraw(deltams);
185
186    int id = data->id;
187    int x = data->pos.x + 24;
188    int y = data->pos.y + 177;
189    int z = 2;
190
191    /* Optional modifiers */
192    switch (data->state)
193    {
194    case PieceData::IDLE:
195        break;
196    case PieceData::BLINK:
197        id = data->id + 1;
198        break;
199    case PieceData::GRAB:
200        id = data->id + 1;
201        z = 4;
202        break;
203    case PieceData::UNGRAB:
204        if ((data->cell * 48 - data->src).sqlen() > 24 * 24)
205            id = data->id + 2;
206        z = 4;
207        break;
208    case PieceData::PUSH:
209        z = 3;
210        break;
211    }
212
213    data->game->GetScene()->AddTile((data->tiler << 16) | id, x, y, z, 0);
214
215    if (data->state == PieceData::GRAB)
216        data->game->GetScene()->AddTile((data->tiler << 16) | 0, x, y, 5, 0);
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.