source: trunk/monsterz/piece.cpp @ 241

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

Delta-time the ungrab animation.

File size: 3.2 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    Int2 cell, pos, tmp;
34    int id;
35    float timer;
36
37    enum
38    {
39        IDLE,
40        BLINK,
41        GRAB,
42        UNGRAB,
43    }
44    state;
45};
46
47/*
48 * Public Piece class
49 */
50
51Piece::Piece(Game *game, int i, int j, int id)
52{
53    data = new PieceData();
54    data->game = game;
55    Ticker::Ref(game);
56    data->tiler = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
57    data->cell = Int2(i, j);
58    data->pos = Int2(i * 48, j * 48);
59    data->id = id;
60    data->state = PieceData::IDLE;
61}
62
63int Piece::Blink()
64{
65    switch (data->state)
66    {
67    case PieceData::IDLE:
68        data->state = PieceData::BLINK;
69        data->timer = 400.0f;
70        return 1;
71    default:
72        return 0;
73    }
74}
75
76int Piece::Grab(Int2 dir)
77{
78    switch (data->state)
79    {
80    case PieceData::IDLE:
81    case PieceData::BLINK:
82    case PieceData::GRAB:
83        data->state = PieceData::GRAB;
84        data->pos = data->pos + dir;
85        return 1;
86    default:
87        return 0;
88    }
89}
90
91int Piece::Ungrab()
92{
93    switch (data->state)
94    {
95    case PieceData::GRAB:
96        data->state = PieceData::UNGRAB;
97        data->timer = 0.0f;
98        data->tmp = data->pos;
99        return 1;
100    default:
101        return 0;
102    }
103}
104
105void Piece::TickGame(float deltams)
106{
107    switch (data->state)
108    {
109    case PieceData::IDLE:
110        break;
111    case PieceData::BLINK:
112        data->timer -= deltams;
113        if (data->timer < 0.0f)
114            data->state = PieceData::IDLE;
115        break;
116    case PieceData::GRAB:
117        break;
118    case PieceData::UNGRAB:
119        data->timer += deltams;
120        Int2 trip = data->cell * 48 - data->tmp;
121        float total_time = trip.len() / 0.8f; /* Moving speed */
122        float t = total_time ? data->timer / total_time : 1.0f;
123        if (t >= 1.0f)
124        {
125            t = 1.0f;
126            data->state = PieceData::IDLE;
127        }
128        data->pos = data->tmp + (t * trip + Float2(0.5f));
129        break;
130    }
131    Entity::TickGame(deltams);
132}
133
134void Piece::TickDraw(float deltams)
135{
136    Entity::TickDraw(deltams);
137
138    int id = data->id;
139    int x = data->pos.x + 24;
140    int y = data->pos.y + 177;
141    int z = 2;
142
143    /* Optional modifiers */
144    switch (data->state)
145    {
146    case PieceData::IDLE:
147        break;
148    case PieceData::BLINK:
149        id = data->id + 1;
150        break;
151    case PieceData::GRAB:
152        id = data->id + 1;
153        z = 4;
154        break;
155    case PieceData::UNGRAB:
156        id = data->id + 2;
157        z = 4;
158        break;
159    }
160    data->game->GetScene()->AddTile((data->tiler << 16) | id, x, y, z, 0);
161}
162
163Piece::~Piece()
164{
165    Ticker::Unref(data->game);
166    Tiler::Deregister(data->tiler);
167    delete data;
168}
169
Note: See TracBrowser for help on using the repository browser.