source: trunk/monsterz/mash.cpp @ 264

Last change on this file since 264 was 264, checked in by sam, 11 years ago

Start implementing mash lifetime.

File size: 1.7 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, finished;
36};
37
38/*
39 * Public Mash class
40 */
41
42Mash::Mash(Game *game)
43  : data(new MashData()),
44    nextmash(NULL)
45{
46    data->game = game;
47    Ticker::Ref(game);
48    data->npieces = 0;
49    data->finished = 0;
50}
51
52void Mash::AddPiece(Piece *piece)
53{
54    data->pieces[data->npieces] = piece;
55    data->cells[data->npieces] = piece->GetCell();
56    data->npieces++;
57}
58
59int Mash::IsFinished() const
60{
61    return data->finished;
62}
63
64void Mash::TickGame(float deltams)
65{
66    Entity::TickGame(deltams);
67
68    if (data->finished)
69        return;
70
71    int inplace = 1;
72    for (int n = 0; n < data->npieces && inplace; n++)
73    {
74        Int2 dest = data->pieces[n]->GetCell() * 48;
75        Int2 cur = data->pieces[n]->GetPos();
76
77        if (abs(cur.x - dest.x) > 24)
78            inplace = 0;
79    }
80
81    if (inplace)
82    {
83        for (int n = 0; n < data->npieces; n++)
84            Ticker::Unref(data->pieces[n]);
85        data->finished = 1;
86    }
87}
88
89void Mash::TickDraw(float deltams)
90{
91    Entity::TickDraw(deltams);
92}
93
94Mash::~Mash()
95{
96    Ticker::Unref(data->game);
97    delete data;
98}
99
Note: See TracBrowser for help on using the repository browser.