source: trunk/monsterz/piece.cpp @ 230

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

Monsters can now blink.

File size: 1.9 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 <cmath>
17
18#include "core.h"
19#include "piece.h"
20#include "monsterz.h"
21
22/*
23 * Piece implementation class
24 */
25
26class PieceData
27{
28    friend class Piece;
29
30private:
31    Game *game;
32    int tiler;
33    int i, j, id;
34    float timer;
35
36    enum
37    {
38        IDLE,
39        BLINK,
40    }
41    state;
42};
43
44/*
45 * Public Piece class
46 */
47
48Piece::Piece(Game *game, int i, int j, int id)
49{
50    data = new PieceData();
51    data->game = game;
52    Ticker::Ref(game);
53    data->tiler = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
54    data->i = i;
55    data->j = j;
56    data->id = id;
57    data->state = PieceData::IDLE;
58}
59
60void Piece::Blink()
61{
62    if (data->state == PieceData::IDLE)
63    {
64        data->state = PieceData::BLINK;
65        data->timer = 400.0f;
66    }
67}
68
69void Piece::TickGame(float deltams)
70{
71    switch (data->state)
72    {
73    case PieceData::IDLE:
74        break;
75    case PieceData::BLINK:
76        data->timer -= deltams;
77        if (data->timer < 0.0f)
78            data->state = PieceData::IDLE;
79        break;
80    }
81    Entity::TickGame(deltams);
82}
83
84void Piece::TickDraw(float deltams)
85{
86    Entity::TickDraw(deltams);
87
88    int id = data->id;
89    int i = data->i;
90    int j = data->j;
91    if (data->state == PieceData::BLINK)
92        id++;
93    data->game->GetScene()->AddTile((data->tiler << 16) | id,
94                                    i * 48 + 24, j * 48 + 177, 2, 0);
95}
96
97Piece::~Piece()
98{
99    Ticker::Unref(data->game);
100    Tiler::Deregister(data->tiler);
101    delete data;
102}
103
Note: See TracBrowser for help on using the repository browser.