source: trunk/monsterz/mash.cpp @ 274

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

Implement above/below pieces.

File size: 3.0 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 <cstdlib>
17#include <cmath>
18
19#include "core.h"
20#include "mash.h"
21#include "monsterz.h"
22
23/*
24 * Mash implementation class
25 */
26
27class MashData
28{
29    friend class Mash;
30
31private:
32    Game *game;
33    Piece *pieces[8 * 8];
34    Int2 cells[8 * 8];
35    int npieces;
36
37    enum
38    {
39        WAIT,
40        POP,
41        DEAD,
42    }
43    state;
44};
45
46/*
47 * Public Mash class
48 */
49
50Mash::Mash(Game *game)
51  : data(new MashData()),
52    nextmash(NULL)
53{
54    data->game = game;
55    Ticker::Ref(game);
56    data->npieces = 0;
57    data->state = MashData::WAIT;
58}
59
60void Mash::AddPiece(Piece *piece)
61{
62    data->pieces[data->npieces] = piece;
63    data->cells[data->npieces] = piece->GetCell();
64    data->npieces++;
65}
66
67int Mash::IsDead() const
68{
69    return data->state == MashData::DEAD;
70}
71
72void Mash::TickGame(float deltams)
73{
74    Entity::TickGame(deltams);
75
76    switch (data->state)
77    {
78    case MashData::WAIT:
79    {
80        int inplace = 1;
81        for (int n = 0; n < data->npieces && inplace; n++)
82        {
83            Int2 dest = data->pieces[n]->GetCell() * 48;
84            Int2 cur = data->pieces[n]->GetPos();
85
86            /* If piece is still too high, don't start the animation */
87            if (cur.y - dest.y > 2)
88                inplace = 0;
89        }
90
91        if (inplace)
92        {
93            for (int n = 0; n < data->npieces; n++)
94                data->pieces[n]->Pop();
95            data->state = MashData::POP;
96        }
97        break;
98    }
99    case MashData::POP:
100    {
101        int alldead = 1;
102        for (int n = 0; n < data->npieces && alldead; n++)
103            if (!data->pieces[n]->IsDead())
104                alldead = 0;
105        if (alldead)
106        {
107            for (int n = 0; n < data->npieces; n++)
108            {
109                /* Remove links for this piece */
110                Piece *above = data->pieces[n]->GetAbove();
111                Piece *below = data->pieces[n]->GetBelow();
112                data->pieces[n]->SetAbove(NULL);
113                data->pieces[n]->SetBelow(NULL);
114                /* If there is a new link to create, create it */
115                if (above && below)
116                    above->SetBelow(below);
117                /* Give away references to this piece */
118                Ticker::Unref(data->pieces[n]);
119            }
120            data->npieces = 0;
121            data->state = MashData::DEAD;
122        }
123        break;
124    }
125    case MashData::DEAD:
126        break;
127    }
128}
129
130void Mash::TickDraw(float deltams)
131{
132    Entity::TickDraw(deltams);
133}
134
135Mash::~Mash()
136{
137    for (int n = 0; n < data->npieces; n++)
138        Ticker::Unref(data->pieces[n]);
139    Ticker::Unref(data->game);
140    delete data;
141}
142
Note: See TracBrowser for help on using the repository browser.