Skip to content

Commit 44f16eb

Browse files
authored
Merge pull request #1 from PierrickLP/master
A quick and dirty Sokoban to test the engine.
2 parents b83baa7 + 4517b90 commit 44f16eb

36 files changed

+574
-0
lines changed
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
local Directions = {
2+
LEFT = { x = -1, y = 0, cmp = "_LESS", use = "x" },
3+
RIGHT = { x = 1, y = 0, cmp = "_MORE", use = "x" },
4+
DOWN = { x = 0, y = 1, cmp = "_MORE", use = "y" },
5+
UP = { x = 0, y = -1, cmp = "_LESS", use = "y" }
6+
}
7+
local CompOps = {
8+
_LESS = function(a, b) return a <= b; end,
9+
_MORE = function(a, b) return a >= b; end
10+
}
11+
12+
StartWalking = 0;
13+
Walking = 1;
14+
EndWalking = 2;
15+
NotWalking = 3;
16+
17+
function Local.Init(position)
18+
Terrain = Scene:getGameObject("Terrain");
19+
Object.pos = { x = position.x, y = position.y};
20+
Object.direction = "NONE";
21+
Object.walking = NotWalking;
22+
Object.sprSize = This:LevelSprite():getSize().x;
23+
Object.speed = 0.5;
24+
Object.activated = false;
25+
26+
local pVec = obe.UnitVector(
27+
position.x * This:LevelSprite():getSize().x,
28+
position.y * This:LevelSprite():getSize().y
29+
);
30+
This:LevelSprite():setPosition(pVec);
31+
end
32+
33+
function Object:getType()
34+
return This:getType();
35+
end
36+
37+
function Object:getSprSize()
38+
return This:LevelSprite():getSize();
39+
end
40+
41+
function Object:move(direction)
42+
--if Object.walking == NotWalking then
43+
Object.direction = direction;
44+
Object.bound = {
45+
x = Directions[Object.direction].x,
46+
y = Directions[Object.direction].y,
47+
cmp = Directions[Object.direction].cmp,
48+
use = Directions[Object.direction].use
49+
}
50+
Object.walking = Walking;
51+
Object.pos = {
52+
x = Object.pos.x + Directions[Object.direction].x,
53+
y = Object.pos.y + Directions[Object.direction].y;
54+
};
55+
Terrain.elements[Object.pos.y+1][Object.pos.x+1][2] = This:access();
56+
--end
57+
end
58+
59+
function Global.Game.Update(dt)
60+
if Object.activated == false and Terrain.elements[Object.pos.y+1][Object.pos.x+1][1]:getType() == "Objective" then
61+
Object.activated = true;
62+
This:LevelSprite():loadTexture("Sprites/GameObjects/Barrel/Barrel_activated.png");
63+
elseif Object.activated ~= false and Terrain.elements[Object.pos.y+1][Object.pos.x+1][1]:getType() ~= "Objective" then
64+
Object.activated = false;
65+
This:LevelSprite():loadTexture("Sprites/GameObjects/Barrel/Barrel.png");
66+
end
67+
if Object.walking == Walking then
68+
local mx, my;
69+
if Object.bound.use == "x" then
70+
mx, my = Object.bound.x * dt * Object.speed, 0;
71+
else
72+
mx, my = 0, Object.bound.y * dt * Object.speed;
73+
end
74+
local move = obe.UnitVector(mx, my);
75+
This:LevelSprite():move(move);
76+
local newpos = This:LevelSprite():getPosition();
77+
if CompOps[Object.bound.cmp](newpos[Object.bound.use], Object.pos[Object.bound.use] * Object.sprSize) then
78+
Object.walking = NotWalking;
79+
end
80+
else
81+
local spritePos = obe.UnitVector(
82+
Object.pos.x * This:LevelSprite():getSize().x,
83+
Object.pos.y * This:LevelSprite():getSize().y
84+
);
85+
This:LevelSprite():setPosition(spritePos);
86+
end
87+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Include (Obe);
2+
3+
Barrel:
4+
LevelSprite:
5+
path:"Sprites/GameObjects/Barrel/Barrel.png"
6+
rect:Rect<SceneUnits>(0.0, 0.0, 0.1, 0.1)
7+
xTransform:"Camera"
8+
yTransform:"Camera"
9+
layer:1.0
10+
z-depth:1.0
11+
Script:
12+
priority:0.0
13+
source:"Data/GameObjects/Barrel/Barrel.lua"
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function Local.Init(position)
2+
local pVec = obe.UnitVector(
3+
position.x * This:LevelSprite():getSize().x,
4+
position.y * This:LevelSprite():getSize().y
5+
);
6+
This:LevelSprite():setPosition(pVec);
7+
end
8+
9+
function Object:getType()
10+
return This:getType();
11+
end
12+
13+
function Object:getSprSize()
14+
return This:LevelSprite():getSize();
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Include (Obe);
2+
3+
Floor:
4+
LevelSprite:
5+
path:"Sprites/GameObjects/Floor/Floor.png"
6+
rect:Rect<SceneUnits>(0.0, 0.0, 0.1, 0.1)
7+
xTransform:"Camera"
8+
yTransform:"Camera"
9+
layer:1.0
10+
z-depth:1.0
11+
Script:
12+
priority:0.0
13+
source:"Data/GameObjects/Floor/Floor.lua"
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function Local.Init()
2+
InitializeBindings();
3+
Object.level = 1;
4+
Object.victory = false;
5+
Scene:createLevelSprite("victory_sprite");
6+
victory_sprite = Scene:getLevelSprite("victory_sprite");
7+
victory_sprite:setParentId(This:getId());
8+
victory_sprite:setVisible(false);
9+
victory_sprite:loadTexture("Sprites/LevelSprites/victory.png");
10+
victory_sprite:setZDepth(0.0)
11+
victory_sprite:useTextureSize()
12+
Object.ratio_victory_sprite = {x=victory_sprite:getSize().x/Scene:getCamera():getSize().x, y=victory_sprite:getSize().y/Scene:getCamera():getSize().y}
13+
14+
Scene:loadFromFile("Sokoban.map.vili", function()
15+
Scene:getGameObject("Terrain"):init(obe.Path("Data/Maps/level" .. Object.level .. ".txt"):find())
16+
end);
17+
18+
end
19+
20+
function InitializeBindings()
21+
Global.Actions["Reset"] = function()
22+
if Scene:getLevelName() == "Sokoban" then
23+
Object.victory = false;
24+
Scene:getGameObject("Terrain"):uninit();
25+
Scene:reload(function() Scene:getGameObject("Terrain"):init(obe.Path("Data/Maps/level" .. Object.level .. ".txt"):find()) end);
26+
end
27+
end
28+
Global.Actions["Next"] = function()
29+
if Scene:getLevelName() == "Sokoban" and Object.victory then
30+
Object.level = Object.level + 1;
31+
Object.victory = false;
32+
Scene:getGameObject("Terrain"):uninit();
33+
Scene:reload(function()
34+
Scene:getGameObject("Terrain"):init(obe.Path("Data/Maps/level" .. Object.level .. ".txt"):find());
35+
end);
36+
end
37+
end
38+
end
39+
40+
41+
function Global.Game.Update(dt)
42+
if Scene:doesGameObjectExists("Terrain") and Scene:getGameObject("Terrain").initialized then
43+
if not Object.victory then
44+
victory_sprite:setVisible(false);
45+
Object.victory = true
46+
for i, v in pairs(Scene:getGameObject("Terrain").elements) do
47+
for j, v2 in pairs(v) do
48+
if v2 ~= nil and v2[2] ~= nil and v2[2]:getType() == "Barrel" then
49+
Object.victory = v2[2].activated and Object.victory or false
50+
end
51+
end
52+
end
53+
end
54+
if Object.victory then
55+
victory_sprite:setSize(obe.UnitVector(Object.ratio_victory_sprite.x * Scene:getCamera():getSize().x, Object.ratio_victory_sprite.y * Scene:getCamera():getSize().y), obe.Referencial.Center)
56+
victory_sprite:setPosition(Scene:getCamera():getPosition(obe.Referencial.Center), obe.Referencial.Center);
57+
victory_sprite:setVisible(true);
58+
end
59+
end
60+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Include (Obe);
2+
3+
Game:
4+
Script:
5+
source:"Data/GameObjects/Game/Game.lua"
6+
permanent:True
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function Local.Init(position)
2+
local pVec = obe.UnitVector(
3+
position.x * This:LevelSprite():getSize().x,
4+
position.y * This:LevelSprite():getSize().y
5+
);
6+
This:LevelSprite():setPosition(pVec);
7+
end
8+
9+
function Object:getType()
10+
return This:getType();
11+
end
12+
13+
function Object:getSprSize()
14+
return This:LevelSprite():getSize();
15+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Include (Obe);
2+
3+
Objective:
4+
LevelSprite:
5+
path:"Sprites/GameObjects/Objective/Objective.png"
6+
rect:Rect<SceneUnits>(0.0, 0.0, 0.1, 0.1)
7+
xTransform:"Camera"
8+
yTransform:"Camera"
9+
layer:1.0
10+
z-depth:2.0
11+
Script:
12+
priority:0.0
13+
source:"Data/GameObjects/Objective/Objective.lua"
+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
local Directions = {
2+
LEFT = { x = -1, y = 0, cmp = "_LESS", use = "x" },
3+
RIGHT = { x = 1, y = 0, cmp = "_MORE", use = "x" },
4+
DOWN = { x = 0, y = 1, cmp = "_MORE", use = "y" },
5+
UP = { x = 0, y = -1, cmp = "_LESS", use = "y" }
6+
}
7+
local CompOps = {
8+
_LESS = function(a, b) return a <= b; end,
9+
_MORE = function(a, b) return a >= b; end
10+
}
11+
12+
StartWalking = 0;
13+
Walking = 1;
14+
EndWalking = 2;
15+
NotWalking = 3;
16+
17+
function Local.Init(position)
18+
Terrain = Scene:getGameObject("Terrain");
19+
Object.pos = { x = position.x, y = position.y};
20+
Object.direction = "NONE";
21+
Object.walking = NotWalking;
22+
Object.sprSize = This:LevelSprite():getSize().x;
23+
Object.speed = 0.5;
24+
InitializeBindings();
25+
26+
local pVec = obe.UnitVector(
27+
position.x * This:LevelSprite():getSize().x,
28+
position.y * This:LevelSprite():getSize().y
29+
);
30+
This:LevelSprite():setPosition(pVec);
31+
end
32+
33+
function Object:getType()
34+
return This:getType();
35+
end
36+
37+
function Object:getSprSize()
38+
return This:LevelSprite():getSize();
39+
end
40+
41+
function InitializeBindings()
42+
for k, v in pairs(Directions) do
43+
Global.Actions[k] = function()
44+
if Object.walking == NotWalking and not Scene:getGameObject("Game").victory then
45+
Object.direction = k;
46+
Object.walking = StartWalking;
47+
This:LevelSprite():loadTexture("Sprites/GameObjects/Robot/Robot_".. k ..".png");
48+
end
49+
end
50+
end
51+
end
52+
53+
function Global.Game.Update(dt)
54+
if Object.walking == StartWalking then
55+
Object.bound = {
56+
x = Directions[Object.direction].x,
57+
y = Directions[Object.direction].y,
58+
cmp = Directions[Object.direction].cmp,
59+
use = Directions[Object.direction].use
60+
}
61+
Object.walking = Walking;
62+
local xProj = Object.pos.x + Directions[Object.direction].x;
63+
local yProj = Object.pos.y + Directions[Object.direction].y;
64+
local tileOnFuturePosition = Terrain.elements[yProj+1][xProj+1][1]:getType();
65+
local barrelToMove = Terrain.elements[yProj+1][xProj+1][2];
66+
if tileOnFuturePosition == "Wall" then
67+
Object.walking = NotWalking;
68+
else
69+
if barrelToMove ~= nil then
70+
xProj = Object.pos.x + 2*Directions[Object.direction].x;
71+
yProj = Object.pos.y + 2*Directions[Object.direction].y;
72+
tileOnFuturePosition = Terrain.elements[yProj+1][xProj+1][1]:getType();
73+
local barrelOnFuturePosition = Terrain.elements[yProj+1][xProj+1][2];
74+
if tileOnFuturePosition == "Wall" or barrelOnFuturePosition ~= nil then
75+
Object.walking = NotWalking;
76+
else
77+
barrelToMove:move(Object.direction);
78+
Terrain.elements[Object.pos.y+1][Object.pos.x+1][2] = nil;
79+
Object.pos = {
80+
x = Object.pos.x + Directions[Object.direction].x,
81+
y = Object.pos.y + Directions[Object.direction].y
82+
};
83+
Terrain.elements[Object.pos.y+1][Object.pos.x+1][2] = This:access();
84+
end
85+
else
86+
Terrain.elements[Object.pos.y+1][Object.pos.x+1][2] = nil;
87+
Object.pos = {
88+
x = Object.pos.x + Directions[Object.direction].x,
89+
y = Object.pos.y + Directions[Object.direction].y
90+
};
91+
Terrain.elements[Object.pos.y+1][Object.pos.x+1][2] = This:access();
92+
end
93+
end
94+
95+
elseif Object.walking == Walking then
96+
local mx, my;
97+
if Object.bound.use == "x" then
98+
mx, my = Object.bound.x * dt * Object.speed, 0;
99+
else
100+
mx, my = 0, Object.bound.y * dt * Object.speed;
101+
end
102+
local move = obe.UnitVector(mx, my);
103+
This:LevelSprite():move(move);
104+
local newpos = This:LevelSprite():getPosition();
105+
if CompOps[Object.bound.cmp](newpos[Object.bound.use], Object.pos[Object.bound.use] * Object.sprSize) then
106+
Object.walking = NotWalking;
107+
end
108+
--[[elseif Object.walking == EndWalking then
109+
if not Hook.InputManager:getAction(Object.direction):check() then
110+
Object.walking = NotWalking;
111+
else
112+
Object.walking = StartWalking;
113+
end]]
114+
else
115+
local spritePos = obe.UnitVector(
116+
Object.pos.x * This:LevelSprite():getSize().x,
117+
Object.pos.y * This:LevelSprite():getSize().y
118+
);
119+
This:LevelSprite():setPosition(spritePos);
120+
end
121+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Include (Obe);
2+
3+
Robot:
4+
LevelSprite:
5+
path:"Sprites/GameObjects/Robot/Robot_DOWN.png"
6+
rect:Rect<SceneUnits>(0.0, 0.0, 0.1, 0.1)
7+
xTransform:"Camera"
8+
yTransform:"Camera"
9+
layer:1.0
10+
z-depth:1.0
11+
Script:
12+
priority:0.0
13+
source:"Data/GameObjects/Robot/Robot.lua"
14+

0 commit comments

Comments
 (0)