source: trunk/monsterz/mash.cpp @ 746

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

monsterz: perform move as soon as the grabbed piece is close to its
destination cell.

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