improved editor (deletion of entities and better loading dialog)

main
Martin Felis (lola) 2011-01-02 21:04:11 +01:00
parent 965c4d9269
commit 3e05c967ab
7 changed files with 67 additions and 30 deletions

View File

@ -35,6 +35,9 @@ bool Controller::OnReceiveEvent (const Engine::EventBasePtr &event) {
void Controller::ResetPlayerEntity () { void Controller::ResetPlayerEntity () {
Engine::HaltSoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav")); Engine::HaltSoundLoop(Engine::GetResourceFullPath("/data/sounds/thrust.wav"));
// We definitely have to reset the player entity id
GetModel()->SetPlayerEntityId(Engine::NullEntityId);
Engine::EntityBase *player_entity = GetModel()->GetEntity(GetModel()->GetPlayerEntityId()); Engine::EntityBase *player_entity = GetModel()->GetEntity(GetModel()->GetPlayerEntityId());
// if we were unable to get the player then there is no need to invalidate // if we were unable to get the player then there is no need to invalidate
@ -46,7 +49,6 @@ void Controller::ResetPlayerEntity () {
for (i = 0; i < EntityControllerMaxKeyStates; i++) for (i = 0; i < EntityControllerMaxKeyStates; i++)
player_entity->UnsetControllerKeyState(i); player_entity->UnsetControllerKeyState(i);
GetModel()->SetPlayerEntityId(Engine::NullEntityId);
} }
void Controller::ResetLevel() { void Controller::ResetLevel() {

View File

@ -250,7 +250,7 @@ int Model::DoLoadLevel (const char* filename) {
std::fstream level_file (filename, std::ios::in); std::fstream level_file (filename, std::ios::in);
if (!level_file) { if (!level_file) {
Engine::LogError ("Unable to open file %s for writing!", filename); Engine::LogError ("Unable to open file %s for reading!", filename);
exit (-1); exit (-1);
} }

View File

@ -105,8 +105,10 @@ bool View::OnReceiveEvent (const Engine::EventBasePtr &event) {
break; break;
case EventLevelComplete: case EventLevelComplete:
if (GetModel()->GetGameState() == GameStateRunning) {
PushViewState(ViewStateLevelComplete); PushViewState(ViewStateLevelComplete);
GetController()->ResetPlayerEntity(); GetController()->ResetPlayerEntity();
}
break; break;
case EventGameOver: case EventGameOver:
@ -489,6 +491,7 @@ void View::DrawUiMainMenu() {
if (Engine::GUI::Button (6, "E", screen_right - 48, 20, 32, button_height)) { if (Engine::GUI::Button (6, "E", screen_right - 48, 20, 32, button_height)) {
PushViewState(ViewStateEditor); PushViewState(ViewStateEditor);
GetController()->ResetLevel();
} }
if (Engine::GUI::Button (4, "Quit", screen_right * 0.5 - 100, 380, button_width, button_height)) { if (Engine::GUI::Button (4, "Quit", screen_right * 0.5 - 100, 380, button_width, button_height)) {
@ -768,14 +771,6 @@ void View::DrawUiEditor() {
// DrawPageTitle ("Editor"); // DrawPageTitle ("Editor");
SelectFont ("console.ttf size=23"); 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 // The close button
if (Engine::GUI::Button (1, "X", screen_right - 48, 20, 32, button_height) if (Engine::GUI::Button (1, "X", screen_right - 48, 20, 32, button_height)
|| Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) { || Engine::GUI::CheckKeyPress(SDLK_ESCAPE)) {
@ -790,33 +785,42 @@ void View::DrawUiEditor() {
} }
if (mEditorState != EditorStateTest) { if (mEditorState != EditorStateTest) {
if (Engine::GUI::Button (2, "+", 30, 20, 32, button_height)) { if (Engine::GUI::Button (2, "Add", 30, 20, 50, button_height)) {
mEditorState = EditorStateAddEntity; mEditorState = EditorStateAddEntity;
} }
if (Engine::GUI::Button (3, "M", 70, 20, 32, button_height)) { if (Engine::GUI::Button (3, "Del", 85, 20, 50, button_height)) {
mEditorState = EditorStateDelEntity;
}
if (Engine::GUI::Button (4, "Move", 140, 20, 65, button_height)) {
mEditorState = EditorStateMoveEntity; mEditorState = EditorStateMoveEntity;
} }
if (Engine::GUI::Button (4, "V", 110, 20, 32, button_height)) { if (Engine::GUI::Button (5, "Spd", 210, 20, 50, button_height)) {
mEditorState = EditorStateEntityVelocity; mEditorState = EditorStateEntityVelocity;
} }
if (Engine::GUI::Button (5, "S", 150, 20, 32, button_height)) { if (Engine::GUI::Button (6, "Save", 265, 20, 65, button_height)) {
mEditorState = EditorStateSave; mEditorState = EditorStateSave;
} }
if (Engine::GUI::Button (6, "L", 190, 20, 32, button_height)) { if (Engine::GUI::Button (7, "Load", 335, 20, 65, button_height)) {
mEditorState = EditorStateLoad; mEditorState = EditorStateLoad;
} }
if (Engine::GUI::Button (7, "T", 230, 20, 32, button_height)) { // we only show the Test button when there is actually something to play
// (i.e. a player entity and at least one asteroid)
if (GetModel()->GetPlayerEntityId() != 0
&& GetModel()->mAsteroids.size() > 0) {
if (Engine::GUI::Button (8, "Test", 405, 20, 64, button_height)) {
mEditorState = EditorStateTest; mEditorState = EditorStateTest;
GetModel()->DoSaveLevel("level_edit_temp.txt"); GetModel()->DoSaveLevel("level_edit_temp.txt");
GetModel()->SetGameState(GameStateRunning); GetModel()->SetGameState(GameStateRunning);
} }
} }
}
std::ostringstream mouse_pos_str; std::ostringstream mouse_pos_str;
vector3d mouse_world_pos; vector3d mouse_world_pos;
@ -834,21 +838,32 @@ void View::DrawUiEditor() {
// an asteroid // an asteroid
Engine::EntityBase* entity = NULL; Engine::EntityBase* entity = NULL;
GameEntityType entity_type = GameEntityTypeShip; if (GetModel()->GetPlayerEntityId() != Engine::NullEntityId) {
if (GetModel()->GetPlayerEntityId() != 0) {
entity = Engine::CreateEntity(GameEntityTypeAsteroid); entity = Engine::CreateEntity(GameEntityTypeAsteroid);
entity->mPhysicState->SetVelocity (vector3d (0., 0., -1.)); entity->mPhysicState->SetVelocity (vector3d (0., 0., -1.));
} else { } else {
entity = Engine::CreateEntity(GameEntityTypeShip); entity = Engine::CreateEntity(GameEntityTypeShip);
GetModel()->SetPlayerEntityId(entity->mId); GetModel()->SetPlayerEntityId(entity->mId);
entity->mPhysicState->SetOrientation (vector3d (0., 90., 0.)); entity->mPhysicState->SetOrientation (vector3d (0., 90., 0.));
} }
// Now we want to insert an Asteroid at the given position // Now we want to insert an Asteroid at the given position
entity->mPhysicState->SetPosition (vector3d (mouse_world_pos[0], 0., mouse_world_pos[2])); entity->mPhysicState->SetPosition (vector3d (mouse_world_pos[0], 0., mouse_world_pos[2]));
}
}
mEditorState = EditorStateUnknown; if (mEditorState == EditorStateDelEntity) {
SelectFont ("console.ttf size=12");
Engine::GUI::Label (9999, "Deleting", 128, 16);
if (controller->GetButtonState(MouseButtonLeft)) {
// Check if there is an entity near the given position
Engine::EntityBase* entity = Engine::GetEntityAt (mouse_world_pos);
if (entity && entity->mType == GameEntityTypeAsteroid) {
Engine::LogMessage ("Killing entity with id %u", entity->mId);
GetModel()->UnregisterEntity (entity->mId);
}
} }
} }
@ -941,11 +956,13 @@ void View::DrawUiEditor() {
if (Engine::GUI::LineEdit(52, 145, 210, level_name, 32)) if (Engine::GUI::LineEdit(52, 145, 210, level_name, 32))
GetModel()->SetLevelName(level_name); GetModel()->SetLevelName(level_name);
if (Engine::FileExists (level_name + std::string(".txt"))) {
if (Engine::GUI::Button (54, "Load", 145, 300, button_width, button_height)) { if (Engine::GUI::Button (54, "Load", 145, 300, button_width, button_height)) {
GetModel()->DoLoadLevel((level_name + std::string(".txt")).c_str()); GetModel()->DoLoadLevel((level_name + std::string(".txt")).c_str());
Engine::LogMessage ("Load"); Engine::LogMessage ("Load");
mEditorState = EditorStateUnknown; mEditorState = EditorStateUnknown;
} }
}
if (Engine::GUI::Button (55, "Cancel", screen_right - 140 - 5 - button_width, 300, button_width, button_height)) if (Engine::GUI::Button (55, "Cancel", screen_right - 140 - 5 - button_width, 300, button_width, button_height))
mEditorState = EditorStateUnknown; mEditorState = EditorStateUnknown;

View File

@ -136,6 +136,7 @@ class View : public Engine::ViewBase {
enum EditorState { enum EditorState {
EditorStateUnknown = 0, EditorStateUnknown = 0,
EditorStateAddEntity, EditorStateAddEntity,
EditorStateDelEntity,
EditorStateMoveEntity, EditorStateMoveEntity,
EditorStateSave, EditorStateSave,
EditorStateEntityVelocity, EditorStateEntityVelocity,

View File

@ -19,6 +19,7 @@
#include <cstdarg> #include <cstdarg>
#include <cstdlib> #include <cstdlib>
#include <assert.h> #include <assert.h>
#include <boost/filesystem.hpp>
int vasprintf (char **result, const char *format, va_list *string) { int vasprintf (char **result, const char *format, va_list *string) {
return 0; return 0;
@ -381,5 +382,17 @@ std::string GetUserDirFullPath (const std::string &path) {
return EngineInstance->GetUserDirFullPath(path); return EngineInstance->GetUserDirFullPath(path);
} }
bool FileExists (const std::string &path) {
boost::filesystem::path file_path (path);
if (!boost::filesystem::exists(file_path))
return false;
if (boost::filesystem::is_directory(file_path))
return false;
return true;
}
} }

View File

@ -186,6 +186,10 @@ std::string GetResourceFullPath (const std::string &resource);
/** \brief Returns the path to a file by prepending the user data path to it /** \brief Returns the path to a file by prepending the user data path to it
*/ */
std::string GetUserDirFullPath (const std::string &path); std::string GetUserDirFullPath (const std::string &path);
/** \brief Checks whether a given file exists */
bool FileExists (const std::string &path);
} }
/* Include the globally visible declarations of the other modules */ /* Include the globally visible declarations of the other modules */

View File

@ -22,6 +22,7 @@ int ModelBase::OnInit (int argc, char* argv[]) {
mKilledEntities.clear (); mKilledEntities.clear ();
mEntityIdCounter = 0; mEntityIdCounter = 0;
mDeltaSec = 0.; mDeltaSec = 0.;
mPlayerEntityId = NullEntityId;
return 0; return 0;
} }
@ -192,7 +193,6 @@ EntityBase* ModelBase::GetEntityAt (const vector3d &pos) {
return NULL; return NULL;
} }
unsigned int ModelBase::CreateEntityId () { unsigned int ModelBase::CreateEntityId () {
if (mEntityIdCounter == NullEntityId - 1) if (mEntityIdCounter == NullEntityId - 1)
LogError ("Could not create valid entity id, reached maximum value of %u", mEntityIdCounter); LogError ("Could not create valid entity id, reached maximum value of %u", mEntityIdCounter);