source: trunk/monsterz/mash.cpp @ 664

Last change on this file since 664 was 664, checked in by sam, 9 years ago

Get rid of float3, float4, int3 etc. in favour of GLSL types.

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