source: trunk/monsterz/fusion.cpp @ 339

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

Start working on a new gameplay, codenamed "Fusion".

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