source: trunk/monsterz/board.cpp @ 243

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

Allow to immediately grab an ungrabbed piece if it is close enough.

  • Property svn:keywords set to Id
File size: 3.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 <cmath>
17#include <cstdlib>
18
19#include "core.h"
20#include "board.h"
21#include "piece.h"
22#include "monsterz.h"
23
24/*
25 * Board implementation class
26 */
27
28class BoardData
29{
30    friend class Board;
31
32private:
33    Game *game;
34    int screen, board, tiles;
35    Piece *pieces[8][8];
36    Piece *grabbed;
37
38    Int2 mouse;
39    Int3 buttons;
40    float nextblink;
41
42    enum
43    {
44        IDLE,
45        BADCLICK,
46        GRAB,
47    }
48    state;
49};
50
51/*
52 * Public Board class
53 */
54
55Board::Board(Game *game)
56{
57    data = new BoardData();
58    data->game = game;
59    Ticker::Ref(game);
60    data->screen = Tiler::Register(PNG_BACKGROUND, 640, 480, 1.0f);
61    data->board = Tiler::Register(PNG_BOARD, 384, 384, 1.0f);
62    data->tiles = Tiler::Register(PNG_TILES, 48, 48, 1.0f);
63
64    for (int j = 0; j < 8; j++)
65        for (int i = 0; i < 8; i++)
66        {
67            int id = (35 + i + (i ^ (j + 13)) * (2 * j + 711)) % 9;
68            data->pieces[i][j] = new Piece(game, Int2(i, j), 25 + 5 * id);
69            Ticker::Ref(data->pieces[i][j]);
70        }
71
72    data->nextblink = 0.0f;
73    data->state = BoardData::IDLE;
74}
75
76void Board::TickGame(float deltams)
77{
78    Entity::TickGame(deltams);
79
80    Int2 mouse = Input::GetMousePos();
81    Int3 buttons = Input::GetMouseButtons();
82
83    /* If possible, make a random monster blink */
84    if ((data->nextblink -= deltams) < 0.0f)
85    {
86        data->pieces[rand() % 8][rand() % 8]->Blink();
87        data->nextblink = (float)(200 + rand() % 500);
88    }
89
90    /* Should we start dragging something? */
91    switch (data->state)
92    {
93    case BoardData::IDLE:
94        if (buttons[0] && !data->buttons[0])
95        {
96            int x = mouse.x - 24;
97            int y = mouse.y - 72;
98            if (x >= 0 && x < 8 * 48 && y >= 0 && y < 8 * 48)
99            {
100                if (data->pieces[x / 48][y / 48]->Grab(Int2(0, 0)))
101                {
102                    data->grabbed = data->pieces[x / 48][y / 48];
103                    data->state = BoardData::GRAB;
104                }
105                else
106                    data->state = BoardData::BADCLICK;
107            }
108            else
109                data->state = BoardData::BADCLICK;
110        }
111        break;
112    case BoardData::GRAB:
113        if (mouse.x >= 0 && mouse.y >= 0)
114            data->grabbed->Grab(mouse - data->mouse);
115        if (!buttons[0] || mouse.x < 0 || mouse.y < 0)
116        {
117            data->grabbed->Ungrab();
118            data->state = BoardData::IDLE;
119        }
120        break;
121    case BoardData::BADCLICK:
122        if (!buttons[0])
123            data->state = BoardData::IDLE;
124        break;
125    }
126
127    data->mouse = mouse;
128    data->buttons = buttons;
129}
130
131void Board::TickDraw(float deltams)
132{
133    Entity::TickDraw(deltams);
134
135    data->game->GetScene()->AddTile((data->screen << 16) | 0, 0, 1050, 0, 0);
136    data->game->GetScene()->AddTile((data->board << 16) | 0, 24, 912, 1, 0);
137
138    if (data->mouse.x >= 0 && data->mouse.y >= 0)
139    {
140        int x = data->mouse.x - 2;
141        int y = data->mouse.y + 59;
142        data->game->GetScene()->AddTile((data->tiles << 16) | 9, x, y, 10, 0);
143    }
144}
145
146Board::~Board()
147{
148    Ticker::Unref(data->game);
149    for (int j = 0; j < 8; j++)
150        for (int i = 0; i < 8; i++)
151            Ticker::Unref(data->pieces[i][j]);
152    Tiler::Deregister(data->board);
153    Tiler::Deregister(data->screen);
154    Tiler::Deregister(data->tiles);
155    delete data;
156}
157
Note: See TracBrowser for help on using the repository browser.