source: trunk/monsterz/mash.cpp @ 266

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

Add the pop animation frame to disappearing monsterz.

File size: 2.5 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                Ticker::Unref(data->pieces[n]);
109            data->npieces = 0;
110            data->state = MashData::DEAD;
111        }
112        break;
113    }
114    case MashData::DEAD:
115        break;
116    }
117}
118
119void Mash::TickDraw(float deltams)
120{
121    Entity::TickDraw(deltams);
122}
123
124Mash::~Mash()
125{
126    for (int n = 0; n < data->npieces; n++)
127        Ticker::Unref(data->pieces[n]);
128    Ticker::Unref(data->game);
129    delete data;
130}
131
Note: See TracBrowser for help on using the repository browser.