source: trunk/monsterz/fusion.cpp @ 343

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

Fix a display bug in the Fusion mouse tracking.

  • Property svn:keywords set to Id
File size: 5.8 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 "fusion.h"
22#include "piece.h"
23#include "thumbs.h"
24#include "mash.h"
25#include "monsterz.h"
26
27/*
28 * Fusion implementation class
29 */
30
31class FusionData
32{
33    friend class Fusion;
34
35private:
36    int2 dim;
37    int npieces;
38    int board, tiles;
39
40    struct Pair
41    {
42        int id;
43        Piece *piece;
44    }
45    pairs[MAX_WIDTH][MAX_HEIGHT], current[2];
46    int next[2], rotation;
47
48    Text *scoretext;
49    int score;
50
51    Emitter *emitter;
52    Thumbs *thumbs;
53
54    enum
55    {
56        IDLE,
57    }
58    state;
59};
60
61/*
62 * Public Fusion class
63 */
64
65Fusion::Fusion(int2 dim, int npieces)
66  : data(new FusionData())
67{
68    data->dim = dim;
69    data->npieces = npieces;
70    data->board = Tiler::Register(PNG_BOARD, 384, 384, 1.0f);
71    data->tiles = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
72
73    data->emitter = new Emitter(data->tiles, float3(0, -0.0006f, 0));
74    Ticker::Ref(data->emitter);
75
76    data->thumbs = new Thumbs(npieces);
77    Ticker::Ref(data->thumbs);
78
79    for (int j = 0; j < data->dim.j; j++)
80        for (int i = 0; i < data->dim.i; i++)
81            data->pairs[i][j].id = 0;
82
83    data->current[0].id = 1 + rand() % data->npieces;
84    data->current[1].id = 1 + rand() % data->npieces;
85    data->current[0].piece = new Piece(data->emitter, int2(3, 8),
86                                       80 + 20 * data->current[0].id);
87    data->current[1].piece = new Piece(data->emitter, int2(4, 8),
88                                       80 + 20 * data->current[1].id);
89    Ticker::Ref(data->current[0].piece);
90    Ticker::Ref(data->current[1].piece);
91    data->current[0].piece->SetPos(int2(3, 7) * 48);
92    data->current[1].piece->SetPos(int2(4, 7) * 48);
93
94    data->next[0] = 1 + rand() % data->npieces;
95    data->next[1] = 1 + rand() % data->npieces;
96    data->rotation = 0;
97
98    data->state = FusionData::IDLE;
99
100    position = int3(24, 72, 1);
101    bbox[0] = position;
102    bbox[1] = bbox[0] + int3(384, 384, 0);
103
104    Input::TrackMouse(this);
105}
106
107void Fusion::TickGame(float deltams)
108{
109    Entity::TickGame(deltams);
110
111    int3 buttons = Input::GetMouseButtons();
112
113    int column = -1;
114
115    if (clicked[2])
116    {
117        column = data->current[0].piece->GetCell().x;
118        data->rotation = (data->rotation + 1) % 2;
119        if (column - data->rotation > data->dim.i - 1)
120            column = data->dim.i - 1 - data->rotation;
121        if (!data->rotation)
122        {
123            FusionData::Pair tmp = data->current[0];
124            data->current[0] = data->current[1];
125            data->current[1] = tmp;
126            if (column == data->dim.i - 1)
127                column = 6;
128        }
129    }
130
131    if (mousepos.x != -1
132        && mousepos.x / 48 != data->current[0].piece->GetCell().x)
133    {
134        column = mousepos.x / 48;
135        column = column < 0 ? 0 : column > data->dim.i - 2 + data->rotation ? data->dim.i - 2 + data->rotation : column;
136    }
137
138    if (column != -1)
139    {
140        if (data->rotation)
141        {
142            data->current[0].piece->SetCell(int2(column, 6));
143            data->current[1].piece->SetCell(int2(column, 7));
144        }
145        else
146        {
147            data->current[0].piece->SetCell(int2(column, 7));
148            data->current[1].piece->SetCell(int2(column + 1, 7));
149        }
150
151        data->current[0].piece->Move(data->current[0].piece->GetCell() * 48);
152        data->current[1].piece->Move(data->current[1].piece->GetCell() * 48);
153    }
154
155    if (clicked[0])
156    {
157        for (int t = 0; t < 2; t++)
158        {
159            int i = data->current[t].piece->GetCell().i;
160            for (int j = 0; j < 7; j++)
161                if (data->pairs[i][j].id == 0)
162                {
163                    data->current[t].piece->SetCell(int2(i, j));
164                    data->current[t].piece->Move(int2(i, j) * 48);
165                    data->pairs[i][j] = data->current[t];
166                    break;
167                }
168        }
169
170        data->current[0].id = data->next[0];
171        data->current[1].id = data->next[1];
172        data->current[0].piece = new Piece(data->emitter, int2(3, 7),
173                                           80 + 20 * data->current[0].id);
174        data->current[1].piece = new Piece(data->emitter, int2(4, 7),
175                                           80 + 20 * data->current[1].id);
176        Ticker::Ref(data->current[0].piece);
177        Ticker::Ref(data->current[1].piece);
178        data->current[0].piece->SetPos(int2(3, 8) * 48);
179        data->current[1].piece->SetPos(int2(4, 8) * 48);
180        data->current[0].piece->Move(data->current[0].piece->GetCell() * 48);
181        data->current[1].piece->Move(data->current[1].piece->GetCell() * 48);
182        data->next[0] = 1 + rand() % data->npieces;
183        data->next[1] = 1 + rand() % data->npieces;
184        data->rotation = 0;
185    }
186
187    switch (data->state)
188    {
189    case FusionData::IDLE:
190        break;
191    }
192}
193
194void Fusion::TickDraw(float deltams)
195{
196    Entity::TickDraw(deltams);
197
198    Scene::GetDefault()->AddTile((data->board << 16) | 0,
199                                 position.x, position.y, 1, 0);
200}
201
202Fusion::~Fusion()
203{
204    Input::UntrackMouse(this);
205
206    Ticker::Unref(data->thumbs);
207    for (int j = 0; j < data->dim.j; j++)
208        for (int i = 0; i < data->dim.i; i++)
209            if (data->pairs[i][j].id)
210                Ticker::Unref(data->pairs[i][j].piece);
211    Ticker::Unref(data->current[0].piece);
212    Ticker::Unref(data->current[1].piece);
213    Ticker::Unref(data->emitter);
214    Tiler::Deregister(data->board);
215    Tiler::Deregister(data->tiles);
216    delete data;
217}
218
Note: See TracBrowser for help on using the repository browser.