source: trunk/monsterz/board.cpp @ 231

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

Properly delta-time blinks.

  • Property svn:keywords set to Id
File size: 2.0 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;
35    Piece *pieces[8][8];
36    float nextblink;
37};
38
39/*
40 * Public Board class
41 */
42
43Board::Board(Game *game)
44{
45    data = new BoardData();
46    data->game = game;
47    Ticker::Ref(game);
48    data->screen = Tiler::Register(PNG_BACKGROUND, 640, 480, 1.0f);
49    data->board = Tiler::Register(PNG_BOARD, 384, 384, 1.0f);
50
51    for (int j = 0; j < 8; j++)
52        for (int i = 0; i < 8; i++)
53        {
54            int id = (35 + i + (i ^ (j + 13)) * (2 * j + 711)) % 9;
55            data->pieces[j][i] = new Piece(game, i, j, 25 + 5 * id);
56            Ticker::Ref(data->pieces[j][i]);
57        }
58
59    data->nextblink = 0.0f;
60}
61
62void Board::TickGame(float deltams)
63{
64    if ((data->nextblink -= deltams) < 0.0f)
65    {
66        data->pieces[rand() % 8][rand() % 8]->Blink();
67        data->nextblink = (float)(200 + rand() % 500);
68    }
69    Entity::TickGame(deltams);
70}
71
72void Board::TickDraw(float deltams)
73{
74    Entity::TickDraw(deltams);
75
76    data->game->GetScene()->AddTile((data->screen << 16) | 0, 0, 1050, 0, 0);
77    data->game->GetScene()->AddTile((data->board << 16) | 0, 24, 912, 1, 0);
78}
79
80Board::~Board()
81{
82    Ticker::Unref(data->game);
83    for (int j = 0; j < 8; j++)
84        for (int i = 0; i < 8; i++)
85            Ticker::Unref(data->pieces[j][i]);
86    Tiler::Deregister(data->board);
87    Tiler::Deregister(data->screen);
88    delete data;
89}
90
Note: See TracBrowser for help on using the repository browser.