~ubuntu-branches/ubuntu/lucid/balder2d/lucid

« back to all changes in this revision

Viewing changes to src/player.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bjørn Hansen
  • Date: 2006-10-14 15:33:42 UTC
  • Revision ID: james.westby@ubuntu.com-20061014153342-un0jglolcs01gcow
Tags: upstream-1.0~rc1
Import upstream version 1.0~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2004 by Bjorn Hansen                                    *
 
3
 *   holomorph@users.sourceforge.net                                       *
 
4
 *                                                                         *
 
5
 *   This program is free software; you can redistribute it and/or modify  *
 
6
 *   it under the terms of the GNU General Public License as published by  *
 
7
 *   the Free Software Foundation; either version 2 of the License, or     *
 
8
 *   (at your option) any later version.                                   *
 
9
 *                                                                         *
 
10
 *   This program is distributed in the hope that it will be useful,       *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
13
 *   GNU General Public License for more details.                          *
 
14
 *                                                                         *
 
15
 *   You should have received a copy of the GNU General Public License     *
 
16
 *   along with this program; if not, write to the                         *
 
17
 *   Free Software Foundation, Inc.,                                       *
 
18
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 
19
 ***************************************************************************/
 
20
 
 
21
#include <SDL/SDL.h>
 
22
#include <SDL/SDL_image.h>
 
23
#include <SDL/SDL_gfxPrimitives.h>
 
24
#include "../include/player.h"
 
25
#include "../include/probe.h"
 
26
#include "../include/input.h"
 
27
#include "../include/gamemanager.h"
 
28
#include "SFont/Font.h"
 
29
#include <sstream>
 
30
 
 
31
using namespace Balder;
 
32
 
 
33
Font* Player::scoreFont = 0;
 
34
Font* Player::lifeFont = 0;
 
35
SDL_Surface* Player::scoreFontImage = 0;
 
36
SDL_Surface* Player::lifeFontImage = 0;
 
37
unsigned int Player::numPlayers = 0;
 
38
std::stringstream Player::stringConverter;
 
39
 
 
40
Player::Player(player_id id, GameManager* gm): game_manager(gm), id(id), probe(0), score(0)
 
41
{
 
42
    if (!scoreFont){
 
43
        scoreFontImage = IMG_Load("SFont/scorefont.png");
 
44
        scoreFont = new Font(scoreFontImage);
 
45
    }
 
46
    if (!lifeFont){
 
47
        lifeFontImage = IMG_Load("SFont/lifefont.png");
 
48
        lifeFont = new Font(lifeFontImage);
 
49
    }
 
50
    ++numPlayers;
 
51
}
 
52
 
 
53
Player::~Player()
 
54
{
 
55
    --numPlayers;
 
56
    if(numPlayers == 0){
 
57
        delete scoreFont;
 
58
        delete lifeFont;
 
59
        scoreFont = lifeFont = 0;
 
60
        SDL_FreeSurface(scoreFontImage);
 
61
        SDL_FreeSurface(lifeFontImage);
 
62
    }
 
63
}
 
64
void Player::DoControl ( input_states states )
 
65
{
 
66
    if (!probe) throw "player has no probe!";
 
67
    probe->RotateLeft(states & LEFT);
 
68
    probe->RotateRight(states & RIGHT);
 
69
    probe->Stick(states & STICK);
 
70
if (states & PUSH) {probe->PushOff();}
 
71
    if (states & FIRE) {probe->Fire();}
 
72
}
 
73
 
 
74
void Player::SetProbe ( Probe* p )
 
75
{
 
76
    probe = p;
 
77
}
 
78
 
 
79
const void Player::DrawStats(SDL_Surface *s)
 
80
{
 
81
    Uint32 velocity = SDL_MapRGBA(s->format, 0xff, 0x88, 0x88, 0xff);
 
82
    Uint32 scoreBG = SDL_MapRGBA(s->format, 0, 0, 0, 0xff);
 
83
    Uint32 probeColor = probe->GetProbeColor();
 
84
    SDL_Rect r = {0,0,9,9};
 
85
    SDL_FillRect(s, 0, scoreBG);
 
86
    rectangleColor(s, 2, 2, s->w-2, s->h-2, probeColor);
 
87
    double speed = probe->GetPushOffSpeed();
 
88
    if (speed > probe->GetMAXSPEED()) speed = probe->GetMAXSPEED();
 
89
    r.x = 10;
 
90
    r.y = s->h -10;
 
91
    for(double i = 0; i < speed; i += probe->GetMAXSPEED() / 8)
 
92
    {
 
93
        r.y -= 10;
 
94
        SDL_FillRect(s, &r, velocity);
 
95
    }
 
96
    stringConverter.str("");
 
97
    stringConverter << score;
 
98
    // align the bottom of the font is 10 pixels above the bottom of the box
 
99
    int y = s->h - (scoreFont->getHeight() + 10);
 
100
    // and 10 pixelsfrom the right
 
101
    int x = s->w - (scoreFont->getTextWidth(stringConverter.str())+10);
 
102
    scoreFont->write(s, stringConverter.str(), 50, y );
 
103
    // check to see if limited lives should show.
 
104
    if (game_manager->getMaxLives() >= 0) {
 
105
        // clear the string again to write the lives remaining
 
106
        stringConverter.str("");
 
107
        stringConverter << game_manager->getMaxLives() - probe->GetDeaths();
 
108
        // align the bottom of the font is 10 pixels above the top of the score
 
109
        y = s->h - (scoreFont->getHeight() + lifeFont->getHeight() + 10);
 
110
        // and 10 pixelsfrom the right
 
111
        x = s->w - (lifeFont->getTextWidth(stringConverter.str())+10);
 
112
        lifeFont->write(s, stringConverter.str(), 50, y);
 
113
    }
 
114
}