source: trunk/monsterz/piece.cpp @ 248

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

Make swaps effective now, just for testing purposes.

File size: 4.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, src, dst;
34    int id;
35    float timer;
36
37    enum
38    {
39        IDLE,
40        BLINK,
41        GRAB,
42        UNGRAB,
43        PUSH,
44    }
45    state;
46};
47
48/*
49 * Public Piece class
50 */
51
52Piece::Piece(Game *game, Int2 cell, int id)
53{
54    data = new PieceData();
55    data->game = game;
56    Ticker::Ref(game);
57    data->tiler = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
58    data->cell = cell;
59    data->pos = cell * 48;
60    data->id = id;
61    data->state = PieceData::IDLE;
62}
63
64void Piece::SetCell(Int2 cell)
65{
66    data->cell = cell;
67}
68
69Int2 Piece::GetPos() const
70{
71    return data->pos;
72}
73
74int Piece::Blink()
75{
76    switch (data->state)
77    {
78    case PieceData::IDLE:
79        data->state = PieceData::BLINK;
80        data->timer = 400.0f; /* Duration of a blink */
81        return 1;
82    default:
83        return 0;
84    }
85}
86
87int Piece::Grab(Int2 dir)
88{
89    switch (data->state)
90    {
91    case PieceData::UNGRAB:
92    case PieceData::PUSH:
93        if ((data->cell * 48 - data->pos).sqlen() > 24 * 24)
94            return 0;
95        /* Fall through */
96    case PieceData::IDLE:
97    case PieceData::BLINK:
98    case PieceData::GRAB:
99        data->state = PieceData::GRAB;
100        data->pos = data->pos + dir;
101        return 1;
102    default:
103        return 0;
104    }
105}
106
107int Piece::Ungrab()
108{
109    switch (data->state)
110    {
111    case PieceData::GRAB:
112        data->state = PieceData::UNGRAB;
113        data->timer = 0.0f;
114        data->src = data->pos;
115        data->dst = data->cell * 48;
116        return 1;
117    default:
118        return 0;
119    }
120}
121
122int Piece::Goto(Int2 pos)
123{
124    switch (data->state)
125    {
126    case PieceData::IDLE:
127    case PieceData::BLINK:
128    case PieceData::UNGRAB:
129    case PieceData::PUSH:
130        data->state = PieceData::PUSH;
131        data->timer = 0.0f;
132        data->src = data->pos;
133        data->dst = pos;
134        return 1;
135    case PieceData::GRAB:
136    default:
137        return 0;
138    }
139}
140
141void Piece::TickGame(float deltams)
142{
143    switch (data->state)
144    {
145    case PieceData::IDLE:
146        break;
147    case PieceData::BLINK:
148        data->timer -= deltams;
149        if (data->timer < 0.0f)
150            data->state = PieceData::IDLE;
151        break;
152    case PieceData::GRAB:
153        break;
154    case PieceData::UNGRAB:
155    case PieceData::PUSH:
156        data->timer += deltams;
157        Int2 trip = data->dst - data->src;
158        float moving_time = trip.len() / 0.3f; /* Moving speed */
159        float t = moving_time ? data->timer / moving_time : 1.0f;
160        if (t >= 1.0f)
161            t = 1.0f;
162        if (data->timer > moving_time + 200.0f)
163            data->state = PieceData::IDLE;
164        data->pos = data->src + (t * trip + 0.5f);
165        break;
166    }
167    Entity::TickGame(deltams);
168}
169
170void Piece::TickDraw(float deltams)
171{
172    Entity::TickDraw(deltams);
173
174    int id = data->id;
175    int x = data->pos.x + 24;
176    int y = data->pos.y + 177;
177    int z = 2;
178
179    /* Optional modifiers */
180    switch (data->state)
181    {
182    case PieceData::IDLE:
183        break;
184    case PieceData::BLINK:
185        id = data->id + 1;
186        break;
187    case PieceData::GRAB:
188        id = data->id + 1;
189        z = 4;
190        break;
191    case PieceData::UNGRAB:
192        if ((data->cell * 48 - data->src).sqlen() > 24 * 24)
193            id = data->id + 2;
194        z = 4;
195        break;
196    case PieceData::PUSH:
197        z = 3;
198        break;
199    }
200
201    data->game->GetScene()->AddTile((data->tiler << 16) | id, x, y, z, 0);
202
203    if (data->state == PieceData::GRAB)
204        data->game->GetScene()->AddTile((data->tiler << 16) | 0, x, y, z, 0);
205}
206
207Piece::~Piece()
208{
209    Ticker::Unref(data->game);
210    Tiler::Deregister(data->tiler);
211    delete data;
212}
213
Note: See TracBrowser for help on using the repository browser.