source: trunk/monsterz/mash.cpp @ 900

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

core: rename vec2i to ivec2 etc. to better match GLSL.

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