source: trunk/monsterz/mash.cpp @ 265

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

Fix piece position detection for mash updates.

File size: 1.8 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 piece is still too high, don't start the animation */
78        if (cur.y - dest.y > 12)
79            inplace = 0;
80    }
81
82    if (inplace)
83    {
84        for (int n = 0; n < data->npieces; n++)
85            Ticker::Unref(data->pieces[n]);
86        data->finished = 1;
87    }
88}
89
90void Mash::TickDraw(float deltams)
91{
92    Entity::TickDraw(deltams);
93}
94
95Mash::~Mash()
96{
97    Ticker::Unref(data->game);
98    delete data;
99}
100
Note: See TracBrowser for help on using the repository browser.