source: trunk/monsterz/board.cpp @ 230

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

Monsters can now blink.

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