summaryrefslogtreecommitdiffstats
path: root/ksnake/game.cpp
blob: 8008b9ee0b3179c95d100bd495e67a4b91a0611b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * Copyright Michel Filippi <mfilippi@sade.rhein-main.de>
 *           Robert Williams
 *           Andrew Chant <andrew.chant@utoronto.ca>
 *           André Luiz dos Santos <andre@netvision.com.br>
 *           Benjamin Meyer <ben+ksnake@meyerhome.net>
 *       
 * This file is part of the ksnake package
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/*
  note: the code to lookup and insert the pixmaps
  into the Options menu was copied and adapted
  from KReversi.
  thanks.
  */
#include <tqdir.h>
#include <tqregexp.h>


#include <tdelocale.h>
#include <tdeconfigdialog.h>

#include <kstdgameaction.h>
#include <kscoredialog.h>
#include <kstatusbar.h>
#include <tdemenubar.h>

#include "rattler.h"
#include "game.h"
#include "startroom.h"
#include "levels.h"
#include "progress.h"
#include "view.h"
#include "general.h"
#include "appearance.h"
#include "settings.h"

#define SCORE 1
#define LIVES 2

Game::Game(TQWidget *parent, const char *name) :  TDEMainWindow(parent,name)
{
	// create statusbar
	statusBar()->insertItem(i18n("Score: 0"), SCORE);
	statusBar()->insertItem(i18n("Lives: 0"), LIVES);

	levels = new Levels();

	view = new View(this, "View");
	rattler = view->rattler;
	rattler->reloadRoomPixmap();
	rattler->setFocus();

	connect(rattler, TQT_SIGNAL(setPoints(int)), this, TQT_SLOT(scoreChanged(int)));
	connect(rattler, TQT_SIGNAL(setTrys(int)), this, TQT_SLOT(setTrys(int)));
	connect(rattler, TQT_SIGNAL(rewind()), view->progress, TQT_SLOT(rewind()));
	connect(rattler, TQT_SIGNAL(advance()), view->progress, TQT_SLOT(advance()));
	connect(view->progress, TQT_SIGNAL(restart()), rattler, TQT_SLOT(restartTimer()));

	connect(rattler, TQT_SIGNAL(togglePaused()), this, TQT_SLOT(togglePaused()));
	connect(rattler, TQT_SIGNAL(setScore(int)), this, TQT_SLOT(gameEnd(int)));

	setCentralWidget(view);

	createMenu();
	setupGUI(TDEMainWindow::Save | StatusBar | Create );
}

Game::~Game()
{
	delete levels;
}

void Game::scoreChanged(int score){
	statusBar()->changeItem(i18n("Score: %1").arg(score), SCORE);
}

void Game::setTrys(int tries){
	statusBar()->changeItem(i18n("Lives: %1").arg(tries), LIVES);
}

void Game::gameEnd(int score){
	KScoreDialog di(KScoreDialog::Name | KScoreDialog::Score | KScoreDialog::Date, this);
	KScoreDialog::FieldInfo scoreInfo;
	TQString date = TQDateTime::currentDateTime().toString();
	scoreInfo.insert(KScoreDialog::Date, date);
	if (di.addScore(score, scoreInfo, true))
		di.exec();
}

void Game::showHighScores()
{
	KScoreDialog d(KScoreDialog::Name | KScoreDialog::Score | KScoreDialog::Date, this);
	d.exec();
}

void Game::createMenu()
{
	actionCollection()->setAutoConnectShortcuts(false);
	(void)new TDEAction(i18n("Move Up"), Key_Up, 0, 0, actionCollection(), "Pl1Up");
	(void)new TDEAction(i18n("Move Down"), Key_Down, 0, 0, actionCollection(), "Pl1Down");
	(void)new TDEAction(i18n("Move Right"), Key_Right, 0, 0, actionCollection(), "Pl1Right");
	(void)new TDEAction(i18n("Move Left"), Key_Left, 0, 0, actionCollection(), "Pl1Left");
	actionCollection()->setAutoConnectShortcuts(true);
	rattler->setActionCollection(actionCollection());

	KStdGameAction::gameNew(TQT_TQOBJECT(rattler), TQT_SLOT(restart()), actionCollection());
	pause = KStdGameAction::pause(TQT_TQOBJECT(rattler), TQT_SLOT(pause()), actionCollection());
	KStdGameAction::highscores(TQT_TQOBJECT(this), TQT_SLOT(showHighScores()), actionCollection());
	KStdGameAction::quit( TQT_TQOBJECT(this), TQT_SLOT(close()), actionCollection());

	KStdAction::preferences(TQT_TQOBJECT(this), TQT_SLOT(showSettings()), actionCollection());

	// TODO change and make custom function that pauses game or
	// modify widget to pause when loosing focus and remove this
	KStdAction::keyBindings(guiFactory(), TQT_SLOT(configureShortcuts()),
actionCollection());
}

void Game::togglePaused()
{
	static bool checked = false;
	checked = !checked;
	pause->setEnabled(checked);
}

/**
 * Show Settings dialog.
 */
void Game::showSettings(){
  if(TDEConfigDialog::showDialog("settings"))
    return;

  TDEConfigDialog *dialog = new TDEConfigDialog(this, "settings", Settings::self());
  dialog->addPage(new General(0, "General"), i18n("General"), "package_settings");

  Appearance *a = new Appearance(0, "Appearance");


  TQStringList list;

    if (rattler->backgroundPixmaps.count() == 0) {
	list.append(i18n("none"));
    } else {
        TQStringList::ConstIterator it = rattler->backgroundPixmaps.begin();
        for(unsigned i = 0; it != rattler->backgroundPixmaps.end(); i++, it++) {
	    // since the filename may contain underscore, they
	    // are replaced with spaces in the menu entry
            TQString s = TQFileInfo( *it ).baseName();
	    s = s.replace(TQRegExp("_"), " ");
	    list.append(s);
	}
    }

	a->kcfg_bgimage->insertStringList(list);

	dialog->addPage(a, i18n("Appearance"), "style");

	dialog->addPage(new StartRoom(0, "StartRoom"), i18n("First Level"), "folder_home");
	connect(dialog, TQT_SIGNAL(settingsChanged()), rattler, TQT_SLOT(loadSettings()));
	dialog->show();
}

#include "game.moc"