quick and dirty editor works for now

This commit is contained in:
2011-01-02 18:25:20 +01:00
parent 88b622928e
commit 965c4d9269
19 changed files with 360 additions and 42 deletions
+1
View File
@@ -29,6 +29,7 @@ BEGIN_ENUM(ViewState)
DECL_ENUM_ELEMENT(ViewStateLevelComplete),
DECL_ENUM_ELEMENT(ViewStateShowHighscore),
DECL_ENUM_ELEMENT(ViewStateEnterPlayername),
DECL_ENUM_ELEMENT(ViewStateEditor),
DECL_ENUM_ELEMENT(ViewStateOptions),
DECL_ENUM_ELEMENT(ViewStateGameOver),
DECL_ENUM_LAST(ViewState)
+8
View File
@@ -49,4 +49,12 @@ void Controller::ResetPlayerEntity () {
GetModel()->SetPlayerEntityId(Engine::NullEntityId);
}
void Controller::ResetLevel() {
Engine::LogMessage ("Resetting level");
GetModel()->ClearEntities();
GetModel()->mAsteroids.clear();
ResetPlayerEntity();
}
}
+1
View File
@@ -23,6 +23,7 @@ class Controller : public Engine::ControllerBase {
Controller () {};
void ResetPlayerEntity ();
void ResetLevel();
protected:
/** \brief Set up basic keybindings */
virtual int OnInit (int argc, char* argv[]);
+1
View File
@@ -32,6 +32,7 @@ int Model::OnInit (int argc, char* argv[]) {
/// \TODO use <optional> or similar for initialization of mCurrentLevelIndex
mCurrentLevelIndex = 99999;
mLevelName = "";
if (InitLevelList() == 0)
Engine::LogError ("No levels found!");
+10
View File
@@ -23,6 +23,7 @@ class Model : public Engine::ModelBase {
/* Level loading etc. */
int DoLoadLevel (const char* filename);
int DoSaveLevel (const char* filename);
void ResetLevel ();
void ReloadLevel();
void ProceedToNextLevel ();
@@ -34,6 +35,12 @@ class Model : public Engine::ModelBase {
mPlayerName = name;
};
std::string GetLevelName() { return mLevelName; };
void SetLevelName(const std::string &name) {
Engine::LogMessage("new level name: %s", name.c_str());
mLevelName = name;
}
/* Highscore */
struct HighscoreEntry {
HighscoreEntry(): name ("unknown"), points (0) { };
@@ -74,12 +81,15 @@ class Model : public Engine::ModelBase {
int mPlayerLives;
unsigned int mPoints;
unsigned int mCurrentLevelIndex;
std::string mPlayerName;
std::vector<std::string> mLevelList;
std::string mLevelName;
virtual bool OnReceiveEvent (const Engine::EventBasePtr &event);
friend class View;
friend class Controller;
};
}
+254 -14
View File
@@ -2,6 +2,8 @@
#include "CameraBase.h"
#include "SimpleConsoleOverlay.h"
#include "IMGUIControls.h"
#include "keytable.h"
#include "DrawingsGL.h"
#include "Engine.h"
#include "Physics.h"
@@ -56,6 +58,10 @@ int View::OnInit (int argc, char* argv[]) {
mBackgroundStars.push_back (star);
}
// Editor default variables
mEditorEntityId = 0;
mEditorState = EditorStateUnknown;
mGUIShipSprite.LoadFromPNG(Engine::GetResourceFullPath("/data/textures/ship.png"));
mGUIShipSprite.SetScale (0.1);
@@ -240,7 +246,15 @@ void View::Draw() {
DrawAxis ();
*/
DrawWorld ();
std::map<unsigned int, Engine::EntityBase*>::iterator entity_iterator;
Model *game_model = static_cast<Model*> (mModel);
unsigned int game_state = game_model->GetGameState();
DrawStars ();
if ( game_state == GameStateRunning || GetViewState() == ViewStateEditor) {
DrawWorld ();
}
DrawUi ();
@@ -250,15 +264,8 @@ void View::Draw() {
void View::DrawWorld() {
std::map<unsigned int, Engine::EntityBase*>::iterator entity_iterator;
Model *game_model = static_cast<Model*> (mModel);
DrawStars ();
if ( game_model->GetGameState() != GameStateRunning) {
return;
}
ViewBase::DrawWorld();
for (entity_iterator = game_model->mEntities.begin ();
@@ -271,6 +278,15 @@ void View::DrawWorld() {
float world_width = game_physics->GetWorldWidth();
float world_height = game_physics->GetWorldHeight();
// if we are in Edit mode and are currently editing the velocity we must
// make sure that we display it
if (GetViewState() == ViewStateEditor) {
if (mEditorState == EditorStateEntityVelocity) {
DrawVector (entity->mPhysicState->GetPosition(),
entity->mPhysicState->GetPosition() + entity->mPhysicState->GetVelocity());
}
}
// Drawing at the original position:
glPushMatrix ();
glTranslatef (entity->mPhysicState->mPosition[0],
@@ -289,7 +305,6 @@ void View::DrawWorld() {
// If we move out the right side
if (entity->mPhysicState->mPosition[0] + entity->mPhysicState->mRadius * 2
>= world_width * 0.5) {
glPushMatrix ();
glTranslatef (entity->mPhysicState->mPosition[0] - world_width,
entity->mPhysicState->mPosition[1],
@@ -395,8 +410,8 @@ void View::DrawUi () {
ViewState current_view_state = GetViewState();
// SelectFont ("console.ttf size=12");
// Engine::GUI::Label (99999, GetStringViewState(current_view_state), 8, 16);
SelectFont ("console.ttf size=12");
Engine::GUI::Label (99999, GetStringViewState(current_view_state), 8, 16);
switch (current_view_state) {
case ViewStateMainMenu:
@@ -426,6 +441,9 @@ void View::DrawUi () {
case ViewStateGameOver:
DrawUiGameOver();
break;
case ViewStateEditor:
DrawUiEditor();
break;
default:
Engine::LogWarning ("Trying to draw unknown ViewState: %s (%d)",
GetStringViewState (game_state), game_state);
@@ -469,6 +487,10 @@ void View::DrawUiMainMenu() {
PushViewState(ViewStateShowHighscore);
}
if (Engine::GUI::Button (6, "E", screen_right - 48, 20, 32, button_height)) {
PushViewState(ViewStateEditor);
}
if (Engine::GUI::Button (4, "Quit", screen_right * 0.5 - 100, 380, button_width, button_height)) {
Engine::RunCommand("quit");
}
@@ -542,7 +564,19 @@ void View::DrawUiLevelComplete() {
if(Engine::GUI::Button (1, "Next level ...", (screen_right - button_width) * 0.5, screen_bottom * 0.5 + 60, button_width, button_height)) {
PopViewState();
GetModel()->ProceedToNextLevel();
// we have to take care when we are testing the level to not proceed to
// the next level...
if (GetViewState() == ViewStateEditor) {
GetModel()->SetGameState(GameStatePaused);
if (mEditorState == EditorStateTest) {
GetModel()->DoLoadLevel("level_edit_temp.txt");
mEditorState = EditorStateUnknown;
}
} else {
GetModel()->ProceedToNextLevel();
}
}
}
@@ -580,8 +614,20 @@ void View::DrawUiPlayerDied() {
if (Engine::GUI::Button (1, "Continue", screen_right * 0.5 - 100, 380, button_width, button_height)) {
PopViewState();
GetModel()->ReloadLevel();
GetModel()->SetGameState(GameStateRunning);
// we have to take care when we are testing the level to not proceed to
// the next level...
if (GetViewState() == ViewStateEditor) {
GetModel()->SetGameState(GameStatePaused);
if (mEditorState == EditorStateTest) {
GetModel()->DoLoadLevel("level_edit_temp.txt");
mEditorState = EditorStateUnknown;
}
} else {
GetModel()->ReloadLevel();
GetModel()->SetGameState(GameStateRunning);
}
}
}
@@ -718,6 +764,199 @@ void View::DrawUiEnterPlayername() {
}
}
void View::DrawUiEditor() {
// DrawPageTitle ("Editor");
SelectFont ("console.ttf size=23");
// Special case: if we get here and are currently in the editor state
// EditorStateTest, then we returned from our test. In this case we have to
// reset the editor state
if (mEditorState == EditorStateTest) {
// mEditorState = EditorStateUnknown;
// Engine::LogMessage ("Returned from testing");
}
// The close button
if (Engine::GUI::Button (1, "X", screen_right - 48, 20, 32, button_height)
|| Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) {
if (mEditorState == EditorStateTest) {
mEditorState = EditorStateUnknown;
GetModel()->DoLoadLevel("level_edit_temp.txt");
GetModel()->SetGameState(GameStatePaused);
} else {
PopViewState();
}
}
if (mEditorState != EditorStateTest) {
if (Engine::GUI::Button (2, "+", 30, 20, 32, button_height)) {
mEditorState = EditorStateAddEntity;
}
if (Engine::GUI::Button (3, "M", 70, 20, 32, button_height)) {
mEditorState = EditorStateMoveEntity;
}
if (Engine::GUI::Button (4, "V", 110, 20, 32, button_height)) {
mEditorState = EditorStateEntityVelocity;
}
if (Engine::GUI::Button (5, "S", 150, 20, 32, button_height)) {
mEditorState = EditorStateSave;
}
if (Engine::GUI::Button (6, "L", 190, 20, 32, button_height)) {
mEditorState = EditorStateLoad;
}
if (Engine::GUI::Button (7, "T", 230, 20, 32, button_height)) {
mEditorState = EditorStateTest;
GetModel()->DoSaveLevel("level_edit_temp.txt");
GetModel()->SetGameState(GameStateRunning);
}
}
std::ostringstream mouse_pos_str;
vector3d mouse_world_pos;
Controller* controller = GetController();
controller->GetMouseWorldPosition(mouse_world_pos);
mouse_pos_str << mouse_world_pos[0] << "," << mouse_world_pos[2];
if (mEditorState == EditorStateAddEntity) {
SelectFont ("console.ttf size=12");
Engine::GUI::Label (9999, "Adding", 128, 16);
if (Engine::GUI::CheckKeyPress(MouseButtonLeft)) {
// if there is no entity so far we create a player, otherwise we create
// an asteroid
Engine::EntityBase* entity = NULL;
GameEntityType entity_type = GameEntityTypeShip;
if (GetModel()->GetPlayerEntityId() != 0) {
entity = Engine::CreateEntity(GameEntityTypeAsteroid);
entity->mPhysicState->SetVelocity (vector3d (0., 0., -1.));
} else {
entity = Engine::CreateEntity(GameEntityTypeShip);
GetModel()->SetPlayerEntityId(entity->mId);
entity->mPhysicState->SetOrientation (vector3d (0., 90., 0.));
}
// Now we want to insert an Asteroid at the given position
entity->mPhysicState->SetPosition (vector3d (mouse_world_pos[0], 0., mouse_world_pos[2]));
mEditorState = EditorStateUnknown;
}
}
if (mEditorState == EditorStateMoveEntity) {
SelectFont ("console.ttf size=12");
Engine::GUI::Label (9999, "Moving", 128, 16);
if (controller->GetButtonState(MouseButtonLeft)) {
// Check if there is an entity near the given position
Engine::EntityBase* asteroid = Engine::GetEntityAt (mouse_world_pos);
if (asteroid) {
mEditorEntityId = asteroid->mId;
}
}
if (mEditorEntityId != 0) {
Engine::EntityBase* asteroid = Engine::GetEntity (mEditorEntityId);
asteroid->mPhysicState->SetPosition (mouse_world_pos);
}
if (!controller->GetButtonState(MouseButtonLeft)) {
if (mEditorEntityId != 0) {
mEditorEntityId = 0;
}
}
}
if (mEditorState == EditorStateEntityVelocity) {
SelectFont ("console.ttf size=12");
Engine::GUI::Label (9999, "Velocity", 128, 16);
if (mEditorEntityId == 0) {
if (controller->GetButtonState(MouseButtonLeft)) {
// Check if there is an entity near the given position
Engine::EntityBase* asteroid = Engine::GetEntityAt (mouse_world_pos);
if (asteroid) {
mEditorEntityId = asteroid->mId;
}
}
}
if (mEditorEntityId != 0) {
Engine::EntityBase* asteroid = Engine::GetEntity (mEditorEntityId);
vector3d new_velocity = mouse_world_pos - asteroid->mPhysicState->GetPosition();
new_velocity[1] = 0.;
asteroid->mPhysicState->SetVelocity (new_velocity);
if (!controller->GetButtonState(MouseButtonLeft)) {
if (mEditorEntityId != 0) {
mEditorEntityId = 0;
}
}
}
}
if (mEditorState == EditorStateSave) {
glColor3f (0.2, 0.2, 0.2);
Engine::GUI::DrawRoundedBlock (140, 150, screen_right - 280, 200);
Engine::GUI::Label (9999, "Saving", 128, 16);
SelectFont ("console.ttf size=23");
Engine::GUI::Label(51, "Filename: ", 145, 180);
std::string level_name = GetModel()->GetLevelName();
glColor3f (1., 1., 1.);
if (Engine::GUI::LineEdit(52, 145, 210, level_name, 32))
GetModel()->SetLevelName(level_name);
if (Engine::GUI::Button (54, "Save", 145, 300, button_width, button_height)) {
GetModel()->DoSaveLevel((level_name + std::string(".txt")).c_str());
Engine::LogMessage ("Save");
mEditorState = EditorStateUnknown;
}
if (Engine::GUI::Button (55, "Cancel", screen_right - 140 - 5 - button_width, 300, button_width, button_height))
mEditorState = EditorStateUnknown;
}
if (mEditorState == EditorStateLoad) {
glColor3f (0.2, 0.2, 0.2);
Engine::GUI::DrawRoundedBlock (140, 150, screen_right - 280, 200);
Engine::GUI::Label (9999, "Loading", 128, 16);
SelectFont ("console.ttf size=23");
Engine::GUI::Label(51, "Filename: ", 145, 180);
std::string level_name = GetModel()->GetLevelName();
glColor3f (1., 1., 1.);
if (Engine::GUI::LineEdit(52, 145, 210, level_name, 32))
GetModel()->SetLevelName(level_name);
if (Engine::GUI::Button (54, "Load", 145, 300, button_width, button_height)) {
GetModel()->DoLoadLevel((level_name + std::string(".txt")).c_str());
Engine::LogMessage ("Load");
mEditorState = EditorStateUnknown;
}
if (Engine::GUI::Button (55, "Cancel", screen_right - 140 - 5 - button_width, 300, button_width, button_height))
mEditorState = EditorStateUnknown;
}
if (controller->GetButtonState(MouseButtonRight)
|| controller->GetButtonState(SDLK_ESCAPE) ) {
mEditorState = EditorStateUnknown;
}
}
/*
* Entities
*/
@@ -733,6 +972,7 @@ void View::DrawEntity (Engine::EntityBase *entity) {
else {
Engine::LogError ("Cannot draw entity: unknown type '%d'", entity->mType);
}
}
/// \todo: Update of the animation ??
+16
View File
@@ -55,8 +55,11 @@ class View : public Engine::ViewBase {
void DrawUiOptions();
void DrawUiEnterPlayername();
void DrawUiEditor();
virtual void Draw ();
virtual void DrawWorld ();
void DrawEntities();
void DrawStars ();
void DrawEntity (Engine::EntityBase *entity);
@@ -130,6 +133,19 @@ class View : public Engine::ViewBase {
int button_width;
int button_height;
enum EditorState {
EditorStateUnknown = 0,
EditorStateAddEntity,
EditorStateMoveEntity,
EditorStateSave,
EditorStateEntityVelocity,
EditorStateLoad,
EditorStateTest
};
EditorState mEditorState;
unsigned int mEditorEntityId;
/// \brief can be used to perform some fading, etc.
float mFadeTimerSecValue;