source: trunk/monsterz/board.cpp @ 263

Last change on this file since 263 was 263, checked in by sam, 11 years ago

Spawn new pieces at the correct coordinates for a free fall.

  • Property svn:keywords set to Id
File size: 8.1 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#include <cstdlib>
18#include <ctime>
19
20#include "core.h"
21#include "board.h"
22#include "piece.h"
23#include "mash.h"
24#include "monsterz.h"
25
26/*
27 * Board implementation class
28 */
29
30class BoardData
31{
32    friend class Board;
33
34private:
35    Game *game;
36    int screen, board, tiles;
37    int click;
38    Piece *pieces[8][8];
39    Piece *grabbed;
40    Mash *mashes;
41    Int2 src_cell, dst_cell;
42
43    Int2 mouse;
44    Int3 buttons;
45    float nextblink;
46
47    enum
48    {
49        IDLE,
50        BADCLICK,
51        GRAB,
52    }
53    state;
54};
55
56/*
57 * Public Board class
58 */
59
60Board::Board(Game *game)
61  : data(new BoardData())
62{
63    data->game = game;
64    Ticker::Ref(game);
65    data->screen = Tiler::Register(PNG_BACKGROUND, 640, 480, 1.0f);
66    data->board = Tiler::Register(PNG_BOARD, 384, 384, 1.0f);
67    data->tiles = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
68    data->click = Sampler::Register(WAV_CLICK);
69
70    srand(rand() ^ time(NULL));
71
72restart:
73    for (int j = 0; j < 8; j++)
74        for (int i = 0; i < 8; i++)
75        {
76            int id = rand() % 7;
77            data->pieces[i][j] = new Piece(game, Int2(i, j), 25 + 5 * id);
78            Ticker::Ref(data->pieces[i][j]);
79        }
80
81    int list[8][8];
82    if (ListMashes(list))
83    {
84        for (int j = 0; j < 8; j++)
85            for (int i = 0; i < 8; i++)
86                Ticker::Unref(data->pieces[i][j]);
87        goto restart;
88    }
89
90    data->mashes = NULL;
91    data->nextblink = 0.0f;
92    data->state = BoardData::IDLE;
93}
94
95void Board::TickGame(float deltams)
96{
97    Entity::TickGame(deltams);
98
99    Int2 mouse = Input::GetMousePos();
100    Int3 buttons = Input::GetMouseButtons();
101
102    /* If possible, make a random monster blink */
103    if ((data->nextblink -= deltams) < 0.0f)
104    {
105        data->pieces[rand() % 8][rand() % 8]->Blink();
106        data->nextblink = (float)(200 + rand() % 500);
107    }
108
109    /* Should we start dragging something? */
110    switch (data->state)
111    {
112    case BoardData::IDLE:
113        if (buttons[0] && !data->buttons[0])
114        {
115            int x = mouse.x - 24;
116            int y = mouse.y - 72;
117            if (x >= 0 && x < 8 * 48 && y >= 0 && y < 8 * 48)
118            {
119                if (data->pieces[x / 48][y / 48]->Grab(Int2(0, 0)))
120                {
121                    Sampler::PlaySample(data->click);
122                    data->grabbed = data->pieces[x / 48][y / 48];
123                    data->src_cell = Int2(x / 48, y / 48);
124                    data->dst_cell = Int2(-1);
125                    data->state = BoardData::GRAB;
126                }
127                else
128                    data->state = BoardData::BADCLICK;
129            }
130            else
131                data->state = BoardData::BADCLICK;
132        }
133        break;
134    case BoardData::GRAB:
135        if (mouse.x >= 0 && mouse.y >= 0)
136        {
137            data->grabbed->Grab(mouse - data->mouse);
138            Int2 cur_pos = data->grabbed->GetPos();
139            Int2 cur_cell = (cur_pos + 24) / 48;
140            if (cur_cell.i < 0 || cur_cell.i >= 8
141                 || cur_cell.j < 0 || cur_cell.j >= 8
142                 || (cur_pos - cur_cell * 48).sqlen() > 24 * 24
143                 || (cur_cell - data->src_cell).sqlen() != 1)
144                cur_cell = Int2(-1);
145            /* If potential target changed, update our cache. */
146            if (cur_cell != data->dst_cell)
147            {
148                if (data->dst_cell != Int2(-1))
149                    data->pieces[data->dst_cell.i]
150                                [data->dst_cell.j]->Goto(data->dst_cell * 48,
151                                                         0.3f);
152                if (cur_cell != Int2(-1))
153                    data->pieces[cur_cell.i]
154                                [cur_cell.j]->Goto(data->src_cell * 48, 0.3f);
155                data->dst_cell = cur_cell;
156            }
157        }
158        if (!buttons[0] || mouse.x < 0 || mouse.y < 0
159             || (data->src_cell * 48
160                  - data->grabbed->GetPos()).sqlen() > 100 * 100)
161        {
162            data->grabbed->Ungrab();
163            if (data->dst_cell != Int2(-1))
164                Switch(data->src_cell, data->dst_cell);
165            data->state = BoardData::IDLE;
166        }
167        break;
168    case BoardData::BADCLICK:
169        if (!buttons[0])
170            data->state = BoardData::IDLE;
171        break;
172    }
173
174    data->mouse = mouse;
175    data->buttons = buttons;
176}
177
178void Board::TickDraw(float deltams)
179{
180    Entity::TickDraw(deltams);
181
182    data->game->GetScene()->AddTile((data->screen << 16) | 0, 0, 1050, 0, 0);
183    data->game->GetScene()->AddTile((data->board << 16) | 0, 24, 912, 1, 0);
184
185    if (data->mouse.x >= 0 && data->mouse.y >= 0)
186    {
187        int x = data->mouse.x - 2;
188        int y = data->mouse.y + 59;
189        data->game->GetScene()->AddTile((data->tiles << 16) | 7, x, y, 10, 0);
190    }
191}
192
193void Board::Switch(Int2 cell_a, Int2 cell_b)
194{
195    Piece *a = data->pieces[cell_a.i][cell_a.j];
196    Piece *b = data->pieces[cell_b.i][cell_b.j];
197    data->pieces[cell_a.i][cell_a.j] = b;
198    data->pieces[cell_b.i][cell_b.j] = a;
199
200    /* Check whether this is a valid move by testing all patterns.
201     * If the move is invalid, cancel the swap and bail out */
202    int list[8][8];
203
204    if (!ListMashes(list))
205    {
206        data->pieces[cell_a.i][cell_a.j] = a;
207        data->pieces[cell_b.i][cell_b.j] = b;
208        a->Goto(cell_a * 48, 0.3f);
209        b->Goto(cell_b * 48, 0.3f);
210        return;
211    }
212
213    /* Perform the swap */
214    a->SetCell(cell_b);
215    a->Goto(cell_b * 48, 0.3f);
216    b->SetCell(cell_a);
217    b->Goto(cell_a * 48, 0.3f);
218
219    /* Remove matching pieces */
220    do
221    {
222//        Mash *mash = new Mash(data->game);
223//        Ticker::Ref(mash);
224
225        for (int j = 8; j--;) for (int i = 0; i < 8; i++)
226        {
227            if (!list[i][j])
228                continue;
229
230            /* The mash becomes the new owner of the disappearing piece */
231//            mash->AddPiece(data->pieces[i][j], Int2(i, j));
232            Ticker::Unref(data->pieces[i][j]);
233
234            Int2 newpos = data->pieces[i][7]->GetPos() + Int2(0, 48);
235
236            for (int j2 = j + 1; j2 < 8; j2++)
237            {
238                data->pieces[i][j2 - 1] = data->pieces[i][j2];
239                data->pieces[i][j2 - 1]->SetCell(Int2(i, j2 - 1));
240                data->pieces[i][j2 - 1]->Goto(Int2(i, j2 - 1) * 48, 0.1f);
241                list[i][j2 - 1] = list[i][j2];
242            }
243            data->pieces[i][7] = new Piece(data->game, Int2(i, 7), 25 + 5 * (rand() % 7));
244            data->pieces[i][7]->SetPos(newpos);
245            data->pieces[i][7]->Goto(Int2(i, 7) * 48, 0.1f);
246            Ticker::Ref(data->pieces[i][7]);
247            list[i][7] = 0;
248        }
249    }
250    while(ListMashes(list));
251}
252
253int Board::ListMashes(int list[8][8])
254{
255    int ret = 0;
256
257    for (int j = 0; j < 8; j++)
258        for (int i = 0; i < 8; i++)
259            list[i][j] = 0;
260
261    for (int j = 0; j < 8; j++)
262        for (int i = 0; i < 8; i++)
263        {
264            int id = data->pieces[i][j]->GetId();
265
266            if (i + 2 < 8 && data->pieces[i + 1][j]->GetId() == id
267                          && data->pieces[i + 2][j]->GetId() == id)
268            {
269                list[i][j] = list[i + 1][j] = list[i + 2][j] = 1;
270                ret = 1;
271            }
272
273            if (j + 2 < 8 && data->pieces[i][j + 1]->GetId() == id
274                          && data->pieces[i][j + 2]->GetId() == id)
275            {
276                list[i][j] = list[i][j + 1] = list[i][j + 2] = 1;
277                ret = 1;
278            }
279        }
280
281    return ret;
282}
283
284Board::~Board()
285{
286    Ticker::Unref(data->game);
287    for (int j = 0; j < 8; j++)
288        for (int i = 0; i < 8; i++)
289            Ticker::Unref(data->pieces[i][j]);
290    Tiler::Deregister(data->board);
291    Tiler::Deregister(data->screen);
292    Tiler::Deregister(data->tiles);
293    Sampler::Deregister(data->click);
294    delete data;
295}
296
Note: See TracBrowser for help on using the repository browser.