1 /*=============================================================================
2 Blobby Volley 2
3 Copyright (C) 2006 Jonathan Sieber (jonathan_sieber@yahoo.de)
4 Copyright (C) 2006 Daniel Knobe (daniel-knobe@web.de)
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 =============================================================================*/
20 
21 #pragma once
22 
23 #include <string>
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/shared_ptr.hpp>
26 
27 #include "GameLogic.h"
28 #include "Vector.h"
29 #include "PlayerInput.h"
30 #include "PlayerIdentity.h"
31 #include "BlobbyDebug.h"
32 
33 class InputSource;
34 struct DuelMatchState;
35 class PhysicWorld;
36 
37 /*! \class DuelMatch
38 	\brief class representing a blobby game.
39 	\details
40 	This class represents a single game between two players
41 	It applys the rules itself and provides an interface for querying
42 	different parameters. For this purpose it is designed as something
43 	similar to a singleton, but it can be instantiated
44 	multiple times on a server or be completely unavailable
45 */
46 class DuelMatch : public ObjectCounter<DuelMatch>
47 {
48 	public:
49 		// If remote is true, only physical responses will be calculated
50 		// but hit events and score events are received from network
51 
52 		DuelMatch(bool remote, std::string rules);
53 
54 		void setPlayers( PlayerIdentity lplayer, PlayerIdentity rplayer);
55 		void setInputSources(boost::shared_ptr<InputSource> linput, boost::shared_ptr<InputSource> rinput );
56 
57 		~DuelMatch();
58 
59 		void setRules(std::string rulesFile);
60 
61 		void reset();
62 
63 		// This steps through one frame
64 		void step();
65 
66 		// this methods allow external input
67 		// events triggered by the network
68 		void setScore(int left, int right);
69 		void resetBall(PlayerSide side);
70 
71 		void trigger(int event);
72 		void resetTriggeredEvents();
73 
74 		// This reports the index of the winning player and -1 if the
75 		// game is still running
76 		PlayerSide winningPlayer() const;
77 
78 		// This methods report the current game state and a useful for
79 		// the input manager, which needs information about the blob
80 		// positions and for lua export, which makes them accessable
81 		// for scripted input sources
82 
83 		int getScore(PlayerSide player) const;
84 		int getScoreToWin() const;
85 		PlayerSide getServingPlayer() const;
86 
87 		void setLastHitIntensity(float intensity);
88 
89 		int getHitcount(PlayerSide player) const;
90 
91 		Vector2 getBallPosition() const;
92 		Vector2 getBallVelocity() const;
93 		Vector2 getBlobPosition(PlayerSide player) const;
94 		Vector2 getBlobVelocity(PlayerSide player) const;
95 
getWorld()96 		const PhysicWorld& getWorld() const{ return *mPhysicWorld.get(); };
97 		const Clock& getClock() const;
98 		Clock& getClock();
99 
100 		bool getBallDown() const;
101 		bool getBallActive() const;
102 		bool canStartRound(PlayerSide servingPlayer) const;
103 
104 		void pause();
105 		void unpause();
106 
isPaused()107 		bool isPaused() const{ return mPaused; }
108 
109 		// This functions returns true if the player launched
110 		// and is jumping at the moment
111 		bool getBlobJump(PlayerSide player) const;
112 
113 		/// Set a new state using a saved DuelMatchState
114 		void setState(const DuelMatchState& state);
115 
116 		/// gets the current state
117 		DuelMatchState getState() const;
118 
119 		//Input stuff for recording and playing replays
120 		boost::shared_ptr<InputSource> getInputSource(PlayerSide player) const;
121 
122 		PlayerIdentity getPlayer(PlayerSide player) const;
123 		PlayerIdentity& getPlayer(PlayerSide player);
124 
125 		void setServingPlayer(PlayerSide side);
126 
getEvents()127 		int getEvents() const { return events; }
128 
129 	private:
130 
131 		boost::scoped_ptr<PhysicWorld> mPhysicWorld;
132 
133 		boost::shared_ptr<InputSource> mInputSources[MAX_PLAYERS];
134 		PlayerInput mTransformedInput[MAX_PLAYERS];
135 
136 		PlayerIdentity mPlayers[MAX_PLAYERS];
137 
138 		GameLogic mLogic;
139 
140 		bool mPaused;
141 
142 		int events;
143 		int external_events;
144 		bool mRemote;
145 };
146