you can download the source code of my game.
Ok, i’ll show you one. In this level, you have a big room with a ball. when you come close to the ball, it run away. You have to put it on a special place in order to validate the level.
(i comment the code just after)
[java]
world:doFile(“Levels/level_helper.lua”)
function main()
level:setSize(30, 7, 3)
level:setLevelNumber(12)
createBoxAroundTheWorld(level, 1)
local saved_id
saved_id = putActionsBox(3, 3, 1, {“activate”, “ball”})
level:setMetaData(saved_id, “onActivate”, addBall)
for i = 7, level:width() - 5 do
local d, a
d = putActionsBox(i, 1, 2, {“walk”, “death”})
a = world:addActionsBox(1, 0, 2, {})
local f = temporaryStop(d, a)
level:setMetaData(d, "onStand", f)
end
world:physic():setPosition(playerId, 2, 2, 2)
local safe_id
safe_id = world:addActionsBox(-5, -5, -5, {})
level:setLevelMetaData(“safe”, safe_id)
local unsafe_id
unsafe_id = world:addActionsBox(-5, -4, -5, {“walk”, “death”})
level:setLevelMetaData(“unsafe”, unsafe_id)
saved_id = putActionsBox(29, 1, 2, {“ball”, “walk”, “death”})
level:setMetaData(saved_id, “onStand”, killBall)
saved_id = putActionsBox(28, 1, 2, {“walk”, “exit”})
level:setMetaData(saved_id, “onStand”, changeLevel)
end
function addBall()
local saved_id
saved_id = level:levelMetaData(“ball”)
if ( saved_id ~= nil ) then
world:kill(saved_id)
end
saved_id = world:addItem(“Ball”, 6, 3, 2)
level:setLevelMetaData(“ball”, saved_id)
local delay
delay = 2
world:delayedCall(impulseBall(saved_id), delay)
world:applyFunctionCD(addBall, delay-1)
end
function impulseBall(old_id)
return function ()
local ball_id
ball_id = level:levelMetaData(“ball”)
if ( ball_id ~= old_id ) then
return
end
world:physic():setMass(ball_id, 0.1)
world:physic():setFriction(ball_id, 0)
world:physic():applyCentralForce(ball_id, 30, 0, 0)
end
end
function sum(number)
return function(anothernumber)
return number + anothernumber
end
end
function temporaryStop(itemID, alternateID)
return function ()
local otherID = params:get(“otherId”)
if ( otherID == playerId ) then
killPlayer()
return
end
level:setMetaData(itemID, “onStand”, nil)
world:changeMaterial(level:levelMetaData(“safe”), itemID)
world:delayedCall(reenable(itemID, alternateID), 2)
end
end
function reenable(itemID, alternateID)
return function ()
level:setMetaData(itemID, “onStand”, temporaryStop(itemID, alternateID))
world:changeMaterial(level:levelMetaData(“unsafe”), itemID)
end
end
function alternate(itemID, otherID)
local pos_item
pos_item = world:physic():positionOf(itemID)
local pos_other
pos_other = world:physic():positionOf(otherID)
world:physic():setPosition(itemID, pos_other:getX(), pos_other:getY(), pos_other:getZ())
world:physic():setPosition(otherID, pos_item:getX(), pos_item:getY(), pos_item:getZ())
end
function killBall()
local saved_id
saved_id = params:get(“otherId”)
if ( saved_id == playerId ) then
return
end
world:kill(saved_id)
end
function changeLevel()
local saved_id
saved_id = params:get(“otherId”)
if ( saved_id == playerId ) then
world:changeLevel(“13”)
end
end
[/java]
ok, the “Levels/level_helper.lua” contains some helpfull functions, like “createBoxAroundTheWorld” which basically if a 3 loop for that create an empty box. Nothing really hard to understand.
All of my program is around an idea: each item has an id, then i can attach “onThings” on each item and when the “thing” happens it calls a function. Just like javascript, you know.
So, basically, the program is
create an environnement
create a box
add trigger "onActivate" on the box.
for this trigger : kill the precedent ball (if any) then add a new ball.
every time you add a ball, add a trigger “onNear” to this ball.
for this trigger if the player is near enough, push the ball in the opposite direction.
in the world, add a box with the trigger “onStand” (triggered when you walk/stand on it).
for this trigger, check the id of the item standing on the box. If it’s the ball, SUCCESS !
As you can see, this rely on a “good” core, and you’ll have a lot of works to do here. However, this is not really hard to do, it’s only a bit time consumming.
Here, i added a lot of functions like “create an item composed of boxes” (and you can implement this by using the “cubeworld” plugin as backend), change the gravity, load “custom items” (like the ball) apply physics things on items etc.
Plus the “trigger” part, which allow me to add dynamic as easily as you can do it with these yummy functions in javascript.
I’ll not hide the fact that you’ll have problem with triggered effect that happens in a level that is not here any longer. This happened when i added a “onTimeElapsed” (or something like that) trigger,
But nothing that you can’t solve (you only need to check that the level of the callback is still running).
(the addactionbox is a tool of my core that create a box with icons on faces. I used that as a hint for the player during game. You can read the wiki on the sourceforge).
This core is old, i developped a new one but … yeah, this approach is not really good when you want to create networked games.
And, btw, you have the lib “luaj” which i used and is exactly that : a binding for lua in java.
If you go this way, i can give you a lot of advice to solve a lot of bugs that can consume a lot of time to understand. For exemple, you cannot use the iterator method for Collection (list, set etc.) you need to do a little trick here.
But, once this is done, you can give an object to the lua script, and call method of this object from the lua script. AND, if these methods returns a java object, you can get it from the lua code as a normal return value from a method, and use it etc. approximatively, you can “do” java in lua at least for the “runtime” part.