Tux.activate();
}
-// Initialize keys
+/***************************************
+ * Handling of the "keys" in the world *
+ ***************************************/
if(! ("world2_keys" in state))
state.world2_keys <- {}
local keys = state.world2_keys;
if(! ("brass" in keys))
keys.brass <- false;
+if(! ("iron" in keys))
+ keys.iron <- false;
+if(! ("bronze" in keys))
+ keys.bronze <- false;
+if(! ("silver" in keys))
+ keys.silver <- false;
if(! ("gold" in keys))
keys.gold <- false;
+/// this function updates the key images (call this if tux has collected a key)
+function update_keys()
+{
+ local keys = state.world2_keys;
+ key_brass.set_action(keys.brass ? "display" : "outline");
+ key_iron.set_action(keys.iron ? "display" : "outline");
+ key_bronze.set_action(keys.bronze ? "display" : "outline");
+ key_silver.set_action(keys.silver ? "display" : "outline");
+ key_gold.set_action(keys.gold ? "display" : "outline");
+}
+
local x = 10;
local y = 10;
key_brass <- FloatingImage("images/objects/keys/key_brass.sprite");
-key_brass.set_anchor_point(ANCHOR_TOPLEFT);
+key_brass.set_anchor_point(ANCHOR_TOP_LEFT);
key_brass.set_pos(x, y);
key_brass.set_visible(true);
-key_brass.set_action(keys.brass ? "display" : "outline");
+x += 30;
+
+key_iron <- FloatingImage("images/objects/keys/key_iron.sprite");
+key_iron.set_anchor_point(ANCHOR_TOP_LEFT);
+key_iron.set_pos(x, y);
+key_iron.set_visible(true);
+x += 30;
+
+key_bronze <- FloatingImage("images/objects/keys/key_bronze.sprite");
+key_bronze.set_anchor_point(ANCHOR_TOP_LEFT);
+key_bronze.set_pos(x, y);
+key_bronze.set_visible(true);
+x += 30;
+
+key_silver <- FloatingImage("images/objects/keys/key_silver.sprite");
+key_silver.set_anchor_point(ANCHOR_TOP_LEFT);
+key_silver.set_pos(x, y);
+key_silver.set_visible(true);
x += 30;
key_gold <- FloatingImage("images/objects/keys/key_gold.sprite");
-key_gold.set_anchor_point(ANCHOR_TOPLEFT);
+key_gold.set_anchor_point(ANCHOR_TOP_LEFT);
key_gold.set_pos(x, y);
key_gold.set_visible(true);
-key_gold.set_action(keys.gold ? "display" : "outline");
x += 30;
+update_keys();
(name "main")
(music "music/forest2.ogg")
(gravity 10.000000)
+ (init-script "import(\"levels/world2/default.nut\");")
(tilemap
(layer "background")
(solid #f)
(sector
(name "main")
(music "music/forestmap.ogg")
+ (init-script "import(\"levels/world2/default.nut\");")
(tilemap
(width 70)
(height 50)
* This script gets loaded into the squirrel root vm in supertux. So functions
* and variables you define here can be used in all threads
*/
+
+
+// TODO: move this to world2
function end_level()
{
Sound.play_music("music/leveldone.ogg");
print("\n");
}
+if(! ("state" in this))
+ state <- {};
+
PlayerStatus::add_coins(int count)
{
coins = std::min(coins + count, MAX_COINS);
- if(count > 100)
+ if(count >= 100)
sound_manager->play("sounds/lifeup.wav");
else
sound_manager->play("sounds/coin.wav");
#include <config.h>
#include <assert.h>
+#include <stdexcept>
#include "floating_image.hpp"
#include "sector.hpp"
#include "object/floating_image.hpp"
+#include "worldmap/worldmap.hpp"
namespace Scripting
{
FloatingImage::FloatingImage(const std::string& spritefile)
{
- assert(Sector::current() != NULL);
+ using namespace WorldMapNS;
+
floating_image = new _FloatingImage(spritefile);
- Sector::current()->add_object(floating_image);
+ if(Sector::current() != NULL) {
+ Sector::current()->add_object(floating_image);
+ } else if(WorldMap::current() != NULL) {
+ WorldMap::current()->add_object(floating_image);
+ } else {
+ delete floating_image;
+ throw new std::runtime_error("Neither sector nor worldmap active");
+ }
}
FloatingImage::~FloatingImage()
// create a new squirrel table for the sector
using namespace Scripting;
+ sq_collectgarbage(global_vm);
+
sq_newtable(global_vm);
sq_pushroottable(global_vm);
if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
sq_release(global_vm, &object);
}
sq_release(global_vm, §or_table);
+ sq_collectgarbage(global_vm);
update_game_objects();
assert(gameobjects_new.size() == 0);
_current->deactivate();
_current = this;
- // register sectortable as current_sector in scripting
+ // register sectortable as sector in scripting
HSQUIRRELVM vm = Scripting::global_vm;
sq_pushroottable(vm);
sq_pushstring(vm, "sector", -1);
worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World"));
load(filename);
+
+ // create a new squirrel table for the worldmap
+ using namespace Scripting;
+
+ sq_collectgarbage(global_vm);
+ sq_newtable(global_vm);
+ sq_pushroottable(global_vm);
+ if(SQ_FAILED(sq_setdelegate(global_vm, -2)))
+ throw Scripting::SquirrelError(global_vm, "Couldn't set worldmap_table delegate");
+
+ sq_resetobject(&worldmap_table);
+ if(SQ_FAILED(sq_getstackobj(global_vm, -1, &worldmap_table)))
+ throw Scripting::SquirrelError(global_vm, "Couldn't get table from stack");
+
+ sq_addref(global_vm, &worldmap_table);
+ sq_pop(global_vm, 1);
}
WorldMap::~WorldMap()
HSQOBJECT& object = *i;
sq_release(global_vm, &object);
}
+ sq_release(global_vm, &worldmap_table);
+
+ sq_collectgarbage(global_vm);
if(current_ == this)
current_ = NULL;
add_object(new Background(*(iter.lisp())));
} else if(iter.item() == "music") {
iter.value()->get(music);
+ } else if(iter.item() == "init-script") {
+ iter.value()->get(init_script);
} else if(iter.item() == "worldmap-spawnpoint") {
SpawnPoint* sp = new SpawnPoint(iter.lisp());
spawn_points.push_back(sp);
current_ = this;
load_state();
+
+ // register worldmap_table as worldmap in scripting
+ using namespace Scripting;
+
+ sq_pushroottable(global_vm);
+ sq_pushstring(global_vm, "worldmap", -1);
+ sq_pushobject(global_vm, worldmap_table);
+ if(SQ_FAILED(sq_createslot(global_vm, -3)))
+ throw SquirrelError(global_vm, "Couldn't set worldmap in roottable");
+ sq_pop(global_vm, 1);
+
+ if(init_script != "") {
+ std::istringstream in(init_script);
+ run_script(in, "WorldMap::init");
+ }
+}
+
+void
+WorldMap::leave()
+{
+ // remove worldmap_table from roottable
+ using namespace Scripting;
+
+ sq_pushroottable(global_vm);
+ sq_pushstring(global_vm, "worldmap", -1);
+ if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse)))
+ throw SquirrelError(global_vm, "Couldn't unset worldmap in roottable");
+ sq_pop(global_vm, 1);
}
static void store_float(HSQUIRRELVM vm, const char* name, float val)
HSQUIRRELVM vm = object_to_vm(object);
+ // set worldmap_table as roottable for the thread
+ sq_pushobject(vm, worldmap_table);
+ sq_setroottable(vm);
+
compile_and_run(vm, in, sourcename);
return vm;
std::string name;
std::string music;
+ std::string init_script;
typedef std::vector<GameObject*> GameObjects;
GameObjects game_objects;
Statistics total_stats;
+ HSQOBJECT worldmap_table;
typedef std::vector<HSQOBJECT> ScriptList;
ScriptList scripts;
{ return current_; }
virtual void setup();
+ virtual void leave();
/** Update worldmap state */
virtual void update(float delta);