quick and dirty editor works for now

main
Martin Felis (lola) 2011-01-02 18:25:20 +01:00
parent 88b622928e
commit 965c4d9269
19 changed files with 360 additions and 42 deletions

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)

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();
}
}

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[]);

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!");

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;
};
}

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 ();
*/
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,9 +564,21 @@ 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();
// 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();
}
}
}
void View::DrawUiGamePaused() {
DrawPageTitle ("Game Paused");
@ -580,10 +614,22 @@ void View::DrawUiPlayerDied() {
if (Engine::GUI::Button (1, "Continue", screen_right * 0.5 - 100, 380, button_width, button_height)) {
PopViewState();
// 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);
}
}
}
/** \brief Draws an entry in the form <name> .... <points>
*
@ -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 ??

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;

View File

@ -60,6 +60,7 @@ int ControllerBase::OnInit (int argc, char* argv[]) {
uistate.kbditem = 0;
uistate.last_keysym = SDLK_CLEAR;
uistate.last_unicode = 0;
uistate.last_key = SDLK_CLEAR;
uistate.lastwidget = 0;
@ -162,12 +163,14 @@ void ControllerBase::IMGUIFinish () {
// also reset the last keysym such that old values will not be reported
uistate.last_keysym = SDLK_FIRST;
uistate.last_key = SDLK_FIRST;
}
/** \brief Keyboard processing */
bool ControllerBase::OnKeyDown (const SDL_keysym &keysym) {
mButtonStates.set(keysym.sym, true);
uistate.last_keysym = keysym.sym;
uistate.last_key = keysym.sym;
// Only when Unicode processing is activated store the unicode value
if (SDL_EnableUNICODE(-1)) {
@ -208,6 +211,7 @@ bool ControllerBase::OnKeyUp (const SDL_keysym &keysym) {
bool ControllerBase::OnMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) {
MouseButton mouse_button = convert_sdl_button (button);
mButtonStates.set(mouse_button, true);
uistate.last_key = mouse_button;
if (mView->mOverlayManager.SendMouseButtonDown (button, xpos, ypos))
return true;

View File

@ -20,6 +20,7 @@ struct IMGUIState {
int kbditem;
SDLKey last_keysym;
Uint16 last_unicode;
int last_key;
int lastwidget;
};
@ -59,6 +60,11 @@ class ControllerBase : public Module {
pos_out[1] = mMouseWorldPosition[1];
pos_out[2] = mMouseWorldPosition[2];
}
void GetMouseWorldPosition (vector3d &pos_out) {
pos_out[0] = mMouseWorldPosition[0];
pos_out[1] = mMouseWorldPosition[1];
pos_out[2] = mMouseWorldPosition[2];
}
bool GetButtonState (unsigned int key) {
assert (key < BINDING_KEYS_LAST);
return mButtonStates.test(key);

View File

@ -224,7 +224,7 @@ void DrawVector(vector3d start, vector3d end) {
float rad_delta = 2. * M_PI / (float) segments;
double s,c;
glColor3f(0.2, 0.2, 0.5);
glColor3f(0.2, 0.8, 0.2);
glPointSize (10.);
glBegin (GL_TRIANGLE_FAN);
glVertex3f (end[0], end[1], end[2]);

View File

@ -70,10 +70,10 @@ struct EntityPhysicState {
vector3d &GetOrientation ();
float &GetAngleVelocity ();
void SetPosition (const vector3d &position);
void SetVelocity (const vector3d &velocity);
void SetOrientation (const vector3d &orientation);
void SetAngleVelocity (const float &angle_velocity);
void SetPosition (const vector3d &position) { mPosition = position; }
void SetVelocity (const vector3d &velocity) { mVelocity = velocity; }
void SetOrientation (const vector3d &orientation) { mOrientation = orientation; }
void SetAngleVelocity (const float &angle_velocity) { mAngleVelocity = angle_velocity; }
/** \brief Transforms the given vector in local space to world coordinates */
void Globalize (vector3d &vec);
@ -149,6 +149,8 @@ struct EntityBase {
}
};
typedef std::map<unsigned int, EntityBase*>::iterator EntityBaseIter;
}
#endif // _ENTITYBASE_H

View File

@ -127,7 +127,6 @@ void Label (int id, const char* caption, int x, int y) {
view = EngineGetView ();
assert (view);
glColor3f (1., 1., 1.);
view->DrawGLStringMeasure(caption, &width, &height);
view->DrawGLString(x , y + height * 0.5, caption);
}
@ -148,6 +147,7 @@ void LabelCentered (int id, const char* caption, int x, int y) {
view = EngineGetView ();
assert (view);
SelectFont("console.ttf size=23");
view->DrawGLStringMeasure(caption, &width, &height);
view->DrawGLString(x - 0.5 * width, y + height * 0.5, caption);
}
@ -334,6 +334,7 @@ bool LineEdit (int id, int x, int y, std::string &text_value, const int &maxleng
if (controller->uistate.kbditem == id && SDL_GetTicks() >> 9 & 1)
text_output += "_";
SelectFont("console.ttf size=23");
view->DrawGLStringMeasure(text_value.c_str(), &width, &height);
view->DrawGLString(textpos_x, textpos_y, text_output.c_str());
@ -507,7 +508,7 @@ float VerticalSlider (int id, int x, int y, int w, int h, float min_value, float
}
bool CheckKeyPress (int keycode) {
if (controller->uistate.last_keysym == keycode)
if (controller->uistate.last_key == keycode)
return true;
return false;

View File

@ -17,6 +17,8 @@ namespace GUI {
/** \brief Checks whether the mouse is in the given rectangle */
bool regionhit (int x, int y, int w, int h);
void DrawRoundedBlock (int x, int y, int w, int h);
/** \brief Draws a label at the given position with vertical center at y */
void Label (int id, const char* caption, int x, int y);
/** \brief Draws a label centered at the given position with vertical center at y */

View File

@ -175,6 +175,24 @@ EntityBase* ModelBase::GetEntity (const unsigned int id) {
return NULL;
}
EntityBase* ModelBase::GetEntityAt (const vector3d &pos) {
std::map<unsigned int, EntityBase*>::iterator iter = mEntities.begin();
while (iter != mEntities.end ()) {
EntityPhysicState* entity = iter->second->mPhysicState;
/// \todo fix const casting!
vector3d temp_pos (pos);
if ( (temp_pos - entity->GetPosition()).length() < entity->mRadius)
return iter->second;
iter++;
}
return NULL;
}
unsigned int ModelBase::CreateEntityId () {
if (mEntityIdCounter == NullEntityId - 1)
LogError ("Could not create valid entity id, reached maximum value of %u", mEntityIdCounter);
@ -315,6 +333,14 @@ EntityBase * GetEntity (unsigned int id) {
return ModelInstance->GetEntity (id);
}
EntityBase* GetEntityAt(const vector3d &pos) {
if (!ModelInstance) {
LogError ("Couldn't execute GetEntityAt(): Model not initialized!");
}
return ModelInstance->GetEntityAt (pos);
}
EntityPhysicState * GetEntityPhysicState (unsigned int id) {
EntityBase *entity = GetEntity (id);

View File

@ -68,6 +68,17 @@ class ModelBase : public Module {
EntityBase* CreateEntity (int type);
/** Returns the Entity with the given id */
EntityBase* GetEntity (const unsigned int id);
/** Returns the Entity at the given world coordinates */
EntityBase* GetEntityAt (const vector3d &pos);
EntityBaseIter GetEntityIterBegin () {
return mEntities.begin();
}
EntityBaseIter GetEntityIterEnd() {
return mEntities.end();
}
/** Returns a unused id for an Entity */
unsigned int CreateEntityId ();
/** Removes all Entities */

View File

@ -2,9 +2,14 @@
#define _MODELGLOBAL_H
namespace Engine {
class EntityBase;
/** \brief Adds the function callback as command for the given name*/
unsigned int GetPlayerEntityId ();
/** \brief Returns the entity at the given world coordinates or NULL */
EntityBase* GetEntityAt(const vector3d &pos);
/** \brief Assigns the player to an Entity. All controls will be redirected to that player
*
* This can be used to disable the controls of of the player by assinging

View File

@ -53,22 +53,6 @@ float& EntityPhysicState::GetAngleVelocity () {
return mAngleVelocity;
}
void EntityPhysicState::SetPosition (const vector3d &position) {
mPosition = position;
}
void EntityPhysicState::SetVelocity (const vector3d &velocity) {
mVelocity = velocity;
}
void EntityPhysicState::SetOrientation (const vector3d &orientation) {
mOrientation = orientation;
}
void EntityPhysicState::SetAngleVelocity (const float &angle_velocity) {
mAngleVelocity = angle_velocity;
}
void EntityPhysicState::Globalize (vector3d &vec) {
// make a copy of the local coordinates
vector3d local (vec);

View File

@ -362,7 +362,8 @@ void ViewBase::DrawGrid () {
xstep = 1.;
zstep = 1.;
glColor3f (1., 1., 1.);
glColor3f (0.8, 0.8, 0.8);
glLineWidth(1.);
glBegin (GL_LINES);
for (i = 0; i <= count_x; i++) {
glVertex3f (i * xstep + xmin, 0., zmin);
@ -373,7 +374,6 @@ void ViewBase::DrawGrid () {
glVertex3f (xmax, 0, i * zstep + zmin);
}
glEnd ();
}
void ViewBase::DrawWorld () {