Skip to content

Commit

Permalink
add switch states and allow abe to activate it
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSapps committed Sep 18, 2016
1 parent 62ff28d commit 5fa0352
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 40 deletions.
10 changes: 5 additions & 5 deletions data/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -58665,7 +58665,7 @@
]
}
],
"name": "SWITCH1.BAN_1009_AePc_0"
"name": "SwitchDeactivateRight"
},
{
"blend_mode": 1,
Expand All @@ -58691,7 +58691,7 @@
]
}
],
"name": "SWITCH1.BAN_1009_AePc_1"
"name": "SwitchIdle"
},
{
"blend_mode": 1,
Expand All @@ -58717,7 +58717,7 @@
]
}
],
"name": "SWITCH1.BAN_1009_AePc_2"
"name": "SwitchActivateLeft"
},
{
"blend_mode": 1,
Expand All @@ -58743,7 +58743,7 @@
]
}
],
"name": "SWITCH1.BAN_1009_AePc_3"
"name": "SwitchDeactivateLeft"
},
{
"blend_mode": 1,
Expand All @@ -58769,7 +58769,7 @@
]
}
],
"name": "SWITCH1.BAN_1009_AePc_4"
"name": "SwitchActivateRight"
},
{
"blend_mode": 1,
Expand Down
22 changes: 20 additions & 2 deletions data/scripts/abe.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ local function Run(s) MoveX(s, kRunSpeed) end

function init(self)
self.states = {}

self.states.name = "abe"

self.states.SayHelloPart1 =
{
animation = 'AbeStandSpeak1',
Expand Down Expand Up @@ -184,7 +185,24 @@ function init(self)
end

if (i:InputAction()) then
return 'PullLever'
local xpos = self.mXPos
if self:FacingLeft() then
xpos = xpos - (kGridWidth + 5)
else
xpos = xpos + (kGridWidth + 5)
end

local lever = GetMapObject(xpos, self.mYPos, "lever")
if (lever == nil) then
print("No lever at " .. xpos .. "," .. self.mYPos)
else
if (lever.states.CanBeActivated()) then
lever.states.Activate(self:FacingLeft())
return 'PullLever'
else
print("Lever is not in right state")
end
end
end

if (InputSameAsDirection(s, i)) then
Expand Down
4 changes: 2 additions & 2 deletions data/scripts/background_animation.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function init_with_data(self, stream)
function init_with_data(self, rect, stream)

self.states = {}

self.states.name = "background_animation"

local animId = stream:ReadU32()

Expand Down
4 changes: 2 additions & 2 deletions data/scripts/door.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function init_with_data(self, stream)
function init_with_data(self, rect, stream)

self.states = {}

self.states.name = "door"

self.states.Closed =
{
Expand Down
5 changes: 3 additions & 2 deletions data/scripts/mine.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function init_with_data(self, stream)
function init_with_data(self, rect, stream)

self.states = {}

self.states.name = "mine"

stream:ReadU32() -- Skip unused "num patterns"
stream:ReadU32() -- Skip unused "patterns"
local scale = stream:ReadU32()
Expand Down
4 changes: 2 additions & 2 deletions data/scripts/slam_door.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function init_with_data(self, stream)
function init_with_data(self, rect, stream)

self.states = {}

self.states.name = "slam_door"

self.states.Closed =
{
Expand Down
53 changes: 44 additions & 9 deletions data/scripts/switch.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
function init_with_data(self, stream)

function init_with_data(self, rect, stream)
self.mXPos = rect.x + 37
self.mYPos = rect.y + rect.h - 5
self.states = {}


self.states.WaitForActivate =
{
animation = "SWITCH1.BAN_1009_AePc_0",
tick = function(s, i)
-- TODO: Detect if activated and from which direction
end
animation = "SwitchIdle",
-- Do nothing, other objects will query CanBeActivated and call Activate
tick = function(s, i) end
}
self.states.DoActivateLeft =
{
animation = "SwitchActivateLeft",
tick = function(s, i) if (s:IsLastFrame()) then return 'DoDeactivateLeft' end end
}
self.states.DoDeactivateLeft =
{
animation = "SwitchDeactivateLeft",
tick = function(s, i) if (s:IsLastFrame()) then return 'WaitForActivate' end end
}
self.states.DoActivateRight =
{
animation = "SwitchActivateRight",
tick = function(s, i) if (s:IsLastFrame()) then return 'DoDeactivateRight' end end
}
self.states.DoDeactivateRight =
{
animation = "SwitchDeactivateRight",
tick = function(s, i) if (s:IsLastFrame()) then return 'WaitForActivate' end end
}
self.states.CanBeActivated = function()
print("CanBeActivated")
-- TODO: Only allow activate in certain states
--return self.states.Activate == self.states.WaitForActivate
return true
end
self.states.Activate = function(facingLeft)
print("Activate")
-- TODO: Activate whatever we are linked to
--GetObjectWithId(self.states.mId).Activate()
if facingLeft then
self.states.Active = self.states.DoActivateRight
else
self.states.Active = self.states.DoActivateLeft
end
self:SetAnimation(self.states.Active.animation)
end

self.states.name = "lever"
self:ScriptLoadAnimations()

self.states.Active = self.states.WaitForActivate
self:SetAnimation(self.states.Active.animation)
end
29 changes: 26 additions & 3 deletions include/gridmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,39 @@ namespace Physics
}

class Animation;


struct ObjRect
{
s32 x;
s32 y;
s32 w;
s32 h;

static void RegisterLuaBindings(sol::state& state)
{
state.new_usertype<ObjRect>("ObjRect",
"x", &ObjRect::x,
"y", &ObjRect::y,
"w", &ObjRect::h,
"h", &ObjRect::h);
}
};

class MapObject
{
public:
MapObject(sol::state& luaState, ResourceLocator& locator, const std::string& scriptName);
void Init();
void Init(Oddlib::IStream& objData);
void Init(const ObjRect& rect, Oddlib::IStream& objData);
void Update(const InputState& input);
void Render(Renderer& rend, GuiContext& gui, int x, int y, float scale);
void Input(const InputState& input);
static void RegisterLuaBindings(sol::state& state);

bool ContainsPoint(s32 x, s32 y) const;
const std::string& Name() const { return mName; }

// TODO: Shouldn't be part of this object
void SnapToGrid();

Expand All @@ -53,8 +75,7 @@ class MapObject
sol::state& mLuaState;
sol::table mStates;

void LoadScript(Oddlib::IStream* objData);

void LoadScript(const ObjRect* rect, Oddlib::IStream* objData);
private: // Actions
void SetAnimation(const std::string& animation);

Expand All @@ -73,6 +94,7 @@ class MapObject

ResourceLocator& mLocator;
std::string mScriptName;
std::string mName;
};

class Level
Expand Down Expand Up @@ -125,6 +147,7 @@ class GridMap
void Update(const InputState& input);
void Render(Renderer& rend, GuiContext& gui);
private:
MapObject* GetMapObject(s32 x, s32 y, const char* type);
void RenderDebug(Renderer& rend);
void RenderEditor(Renderer& rend, GuiContext& gui);
void RenderGame(Renderer& rend, GuiContext& gui);
Expand Down
4 changes: 2 additions & 2 deletions include/resourcemapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,8 +1902,8 @@ class Animation
u32 mCounter = 0;
s32 mFrameNum = -1;

s32 mXPos = 500;
s32 mYPos = 800;
s32 mXPos = 100;
s32 mYPos = 100;
f32 mScale = 3;

bool mIsLastFrame = false;
Expand Down
3 changes: 2 additions & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ void Engine::InitSubSystems()

mInputState.AddControllers();

mLuaState.open_libraries(sol::lib::base, sol::lib::string, sol::lib::jit, sol::lib::table, sol::lib::debug);
mLuaState.open_libraries(sol::lib::base, sol::lib::string, sol::lib::jit, sol::lib::table, sol::lib::debug, sol::lib::math);

// Redirect lua print()
mLuaState.set_function("print", LuaLog);

Oddlib::IStream::RegisterLuaBindings(mLuaState);
Actions::RegisterLuaBindings(mLuaState);
MapObject::RegisterLuaBindings(mLuaState);
ObjRect::RegisterLuaBindings(mLuaState);
}

// TODO: Using averaging value or anything that is more accurate than this
Expand Down
Loading

0 comments on commit 5fa0352

Please sign in to comment.