source: trunk/monsterz/score.cpp @ 838

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

monsterz: create a Score class to display the score text and increment it
progressively.

  • Property svn:keywords set to Id
File size: 1.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 <cmath>
16#include <cstdlib>
17#include <ctime>
18
19#include "core.h"
20
21using namespace lol;
22
23#include "score.h"
24#include "monsterz.h"
25
26/*
27 * Score implementation class
28 */
29
30class ScoreData
31{
32    friend class Score;
33
34private:
35    int score;
36    int display;
37
38    float countdown;
39};
40
41/*
42 * Public Score class
43 */
44
45Score::Score(int score)
46  : Text(NULL, "monsterz/gfx/font2.png"),
47    data(new ScoreData())
48{
49    data->score = score;
50    data->display = score;
51    data->countdown = 0.0f;
52
53    SetAlign(Text::ALIGN_RIGHT);
54}
55
56void Score::TickGame(float deltams)
57{
58    Text::TickGame(deltams);
59
60    if (data->display != data->score)
61    {
62        data->countdown += deltams;
63
64        if (data->countdown > 500.0f)
65        {
66            int diff = data->score - data->display;
67            int mul = 1;
68
69            while (diff / 10)
70            {
71                diff /= 10;
72                mul *= 10;
73            }
74
75            if (data->score > data->display)
76                data->display += mul;
77            else
78                data->display += mul;
79
80            data->countdown = 0.0f;
81        }
82    }
83
84    SetInt(data->display);
85}
86
87void Score::TickDraw(float deltams)
88{
89    Text::TickDraw(deltams);
90}
91
92void Score::Add(int points)
93{
94    data->score += points;
95}
96
97Score::~Score()
98{
99    delete data;
100}
101
Note: See TracBrowser for help on using the repository browser.