Debian Codegraph @

src/game/GameAgent.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "GameAgent.h"
10
11 #include "StateManager.h"
12
13 #include "Log.h"
14 #include "WorldMap.h"
15 #include "InputAgent.h"
16 #include "KeyStroke.h"
17 #include "KeyBinder.h"
18 #include "SimpleMsg.h"
19 #include "IntMsg.h"
20 #include "Path.h"
21 #include "OptionAgent.h"
22 #include "LevelStatus.h"
23 #include "Level.h"
24
25 #include "SDL.h"
26
27 //-----------------------------------------------------------------
28 void
29 GameAgent::own_init()
30 {
31 m_manager = new StateManager();
32 std::string replayLevel = OptionAgent::agent()->getParam("replay_level");
33 if (replayLevel == "") {
34 Path pathMap = Path::dataReadPath(OptionAgent::agent()->getParam(
35 "worldmap", "script/worldmap.lua"));
36 WorldMap *worldmap = new WorldMap();
37 worldmap->initMap(pathMap);
38 m_manager->pushState(NULL, worldmap);
39 } else {
40 replaySolution(replayLevel);
41 }
42
43 keyBinding();
44 }
45 //-----------------------------------------------------------------
46 /**
47 * Replays the given solution instead of starting the game.
48 * It is used only for testing.
49 */
50 void
51 GameAgent::replaySolution(const std::string &codename)
52 {
53 static LevelStatus *levelStatus = NULL;
54 static DescFinder *desc = NULL;
55 if (levelStatus == NULL) {
56 levelStatus = new LevelStatus();
57 desc = new WorldMap();
58 }
59
60 levelStatus->prepareRun(codename, "", 0, "");
61 std::string moves = levelStatus->readSolvedMoves();
62
63 Path datafile = Path::dataReadPath(
64 "script/" + codename + "/init.lua");
65 Level *level = new Level(codename, datafile, 0);
66 level->fillStatus(levelStatus);
67 level->fillDesc(desc);
68
69 m_manager->pushState(NULL, level);
70 level->loadReplay(moves);
71 }
72 //-----------------------------------------------------------------
73 /**
74 * Update game.
75 * Play level or show menu.
76 */
77 void
78 GameAgent::own_update()
79 {
80 m_manager->updateGame();
81 }
82 //-----------------------------------------------------------------
83 /**
84 * Remember playtime.
85 */
86 void
87 GameAgent::own_shutdown()
88 {
89 OptionAgent *options = OptionAgent::agent();
90 int playtime = options->getAsInt("playtime");
91 playtime += SDL_GetTicks() / 1000;
92 options->setPersistent("playtime", playtime);
93
94 delete m_manager;
95 }
96
97 //-----------------------------------------------------------------
98 /**
99 * Global keystrokes.
100 */
101 void
102 GameAgent::keyBinding()
103 {
104 BaseMsg *msg;
105 KeyBinder *keyBinder = InputAgent::agent()->keyBinder();
106
107 // fullscreen
108 KeyStroke fs(SDLK_F11, KMOD_NONE);
109 msg = new SimpleMsg(Name::VIDEO_NAME, "fullscreen");
110 keyBinder->addStroke(fs, msg);
111
112 // log
113 KeyStroke log_plus(SDLK_KP_PLUS, KMOD_RALT);
114 msg = new SimpleMsg(Name::APP_NAME, "inc_loglevel");
115 keyBinder->addStroke(log_plus, msg);
116 KeyStroke log_minus(SDLK_KP_MINUS, KMOD_RALT);
117 msg = new SimpleMsg(Name::APP_NAME, "dec_loglevel");
118 keyBinder->addStroke(log_minus, msg);
119 }
120