Skip to content

Commit d243ade

Browse files
author
Sygmei
committed
[chore] cleaning code
1 parent b85b12d commit d243ade

File tree

8 files changed

+87
-63
lines changed

8 files changed

+87
-63
lines changed

Sokoban/Data/GameObjects/Barrel/Barrel.lua

+26-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function Local.Init(position)
2222
Object.sprSize = This.Sprite:getSize().x;
2323
Object.speed = 0.5;
2424
Object.activated = false;
25+
Object.moved = false;
2526

2627
local pVec = obe.Transform.UnitVector(
2728
position.x * This.Sprite:getSize().x,
@@ -53,35 +54,37 @@ function Object:move(direction)
5354
y = Object.pos.y + Directions[Object.direction].y;
5455
};
5556
Terrain.elements[Object.pos.y+1][Object.pos.x+1][2] = This:access();
57+
Object.moved = true;
5658
--end
5759
end
5860

5961
function Event.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.Sprite: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.Sprite:loadTexture("Sprites/GameObjects/Barrel/Barrel.png");
66-
end
6762
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;
63+
if Object.activated == false and Terrain.elements[Object.pos.y+1][Object.pos.x+1][1]:getType() == "Objective" then
64+
Object.activated = true;
65+
This.Sprite:loadTexture("Sprites/GameObjects/Barrel/Barrel_activated.png");
66+
elseif Object.activated ~= false and Terrain.elements[Object.pos.y+1][Object.pos.x+1][1]:getType() ~= "Objective" then
67+
Object.activated = false;
68+
This.Sprite:loadTexture("Sprites/GameObjects/Barrel/Barrel.png");
7369
end
74-
local move = obe.Transform.UnitVector(mx, my);
75-
This.Sprite:move(move);
76-
local newpos = This.Sprite:getPosition();
77-
if CompOps[Object.bound.cmp](newpos[Object.bound.use], Object.pos[Object.bound.use] * Object.sprSize) then
78-
Object.walking = NotWalking;
70+
if Object.walking == Walking then
71+
local mx, my;
72+
if Object.bound.use == "x" then
73+
mx, my = Object.bound.x * dt * Object.speed, 0;
74+
else
75+
mx, my = 0, Object.bound.y * dt * Object.speed;
76+
end
77+
local move = obe.Transform.UnitVector(mx, my);
78+
This.Sprite:move(move);
79+
local newpos = This.Sprite:getPosition();
80+
if CompOps[Object.bound.cmp](newpos[Object.bound.use], Object.pos[Object.bound.use] * Object.sprSize) then
81+
Object.walking = NotWalking;
82+
local spritePos = obe.Transform.UnitVector(
83+
Object.pos.x * This.Sprite:getSize().x,
84+
Object.pos.y * This.Sprite:getSize().y
85+
);
86+
This.Sprite:setPosition(spritePos);
87+
end
7988
end
80-
else
81-
local spritePos = obe.Transform.UnitVector(
82-
Object.pos.x * This.Sprite:getSize().x,
83-
Object.pos.y * This.Sprite:getSize().y
84-
);
85-
This.Sprite:setPosition(spritePos);
8689
end
8790
end

Sokoban/Data/GameObjects/Game/Game.lua

+19-16
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ function Local.Init()
1515
y = victory_sprite:getSize().y / camera_size.y
1616
}
1717

18-
print("Loading Scene Sokoban.map.vili");
18+
-- print("Loading Scene Sokoban.map.vili");
1919
Engine.Scene:loadFromFile("Data/Maps/Sokoban.map.vili", function()
20-
print("================ OK SCENE LOADED ================")
20+
-- print("================ OK SCENE LOADED ================")
2121
Engine.Scene:getGameObject("Terrain"):init(
22-
obe.System.Path("Data/Maps/level" .. Object.level .. ".txt"):find())
22+
obe.System.Path("Data/Maps/level" .. Object.level .. ".txt"):find());
2323
end);
2424

2525
end
@@ -62,22 +62,25 @@ function Event.Game.Update(dt)
6262
end
6363
end
6464
end
65-
end
66-
if Object.victory then
67-
victory_sprite:setSize(obe.Transform.UnitVector(
68-
Object.ratio_victory_sprite.x *
69-
Engine.Scene:getCamera():getSize().x,
70-
Object.ratio_victory_sprite.y *
71-
Engine.Scene:getCamera():getSize().y),
72-
obe.Transform.Referential.Center)
73-
victory_sprite:setPosition(Engine.Scene:getCamera():getPosition(
74-
obe.Transform.Referential.Center),
65+
if Object.victory then
66+
local camera_size = Engine.Scene:getCamera():getSize();
67+
local victory_size = obe.Transform.UnitVector(
68+
Object.ratio_victory_sprite.x *
69+
camera_size.x,
70+
Object.ratio_victory_sprite.y *
71+
camera_size.y)
72+
victory_sprite:setSize(victory_size,
7573
obe.Transform.Referential.Center);
76-
victory_sprite:setVisible(true);
74+
victory_sprite:setPosition(
75+
Engine.Scene:getCamera():getPosition(
76+
obe.Transform.Referential.Center),
77+
obe.Transform.Referential.Center);
78+
victory_sprite:setVisible(true);
79+
end
7780
end
7881
end
7982
end
8083

8184
function Event.Game.Render()
82-
collectgarbage();
83-
end
85+
-- collectgarbage();
86+
end

Sokoban/Data/GameObjects/Robot/Robot.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ EndWalking = 2;
1515
NotWalking = 3;
1616

1717
function Local.Init(position)
18-
print("Beep boop");
19-
print("============= Initializing Robot", inspect(position));
18+
-- print("Beep boop");
19+
-- print("============= Initializing Robot", inspect(position));
2020
Terrain = Engine.Scene:getGameObject("Terrain");
2121
Object.pos = { x = position.x, y = position.y};
2222
Object.direction = "NONE";
@@ -30,7 +30,7 @@ function Local.Init(position)
3030
position.y * This.Sprite:getSize().y
3131
);
3232
This.Sprite:setPosition(pVec);
33-
print("Robot is done");
33+
-- print("Robot is done");
3434
end
3535

3636
function Object:getType()
@@ -42,7 +42,7 @@ function Object:getSprSize()
4242
end
4343

4444
function InitializeBindings()
45-
print("=============== Initializing Robot Bindings");
45+
-- print("=============== Initializing Robot Bindings");
4646
for k, v in pairs(Directions) do
4747
Event.Actions[k] = function()
4848
if Object.walking == NotWalking and not Engine.Scene:getGameObject("Game").victory then

Sokoban/Data/GameObjects/Robot/Robot.obj.vili

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Robot:
22
Sprite:
3-
path:"Sprites/GameObjects/Robot/Robot_DOWNd.png"
3+
path:"Sprites/GameObjects/Robot/Robot_DOWN.png"
44
rect:
55
x:0.0
66
y:0.0

Sokoban/Data/GameObjects/Terrain/Terrain.lua

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ function function_binding(toBind)
55
blocFunctions[k] = function(position)
66
local newObjs = {};
77
for i in string.gmatch(v, "%S+") do
8+
-- print("CREATING GAMEOBJECT", i)
89
table.insert(newObjs, Engine.Scene:createGameObject(i)({position = position}));
910
end
1011
return newObjs
@@ -17,16 +18,17 @@ function Object:uninit()
1718
end
1819

1920
function Object:init(path)
20-
print("Init terrain", path)
21+
local start_time = obe.Time.epoch();
22+
-- print("Init terrain", path)
2123
if not Object.initialized and file_exists(path) then
2224
This:initialize();
23-
print("Terrain initialized");
25+
-- print("Terrain initialized");
2426
local lines = lines_from(path)
2527
local maxY;
2628
local maxX;
2729
local sprSize;
2830
local load_table = {};
29-
print("Lines", inspect(lines));
31+
-- print("Lines", inspect(lines));
3032
for i, str in pairs(lines) do
3133
if maxY == nil or i>maxY then
3234
maxY = i;
@@ -40,14 +42,14 @@ function Object:init(path)
4042
load_table[i][j] = char;
4143
end
4244
end
43-
print("Loadtable", inspect(load_table));
45+
-- print("Loadtable", inspect(load_table));
4446
local offset = {x = (maxX/2), y = (maxY/2)};
4547
for i, v in pairs(load_table) do
4648
Object.elements[i] = {}
4749
for j, v2 in pairs(v) do
4850
if v2 ~= " " then
4951
local position = { x = j-1, y = i-1 };
50-
print("Loading element", v2);
52+
-- print("Loading element", v2);
5153
Object.elements[i][j] = blocFunctions[v2](position);
5254
if sprSize == nil then
5355
sprSize = Object.elements[i][j][1].getSprSize();
@@ -56,7 +58,7 @@ function Object:init(path)
5658
end
5759
end
5860

59-
print("Setting camera");
61+
-- print("Setting camera");
6062
local pVec = obe.Transform.UnitVector(
6163
offset.x * sprSize.x,
6264
offset.y * sprSize.y
@@ -69,9 +71,11 @@ function Object:init(path)
6971
local camera = Engine.Scene:getCamera();
7072
camera:setPosition(pVec, obe.Transform.Referential.Center);
7173
camera:scale(pSize, obe.Transform.Referential.Center);
72-
print("Terrain done :)");
74+
-- print("Terrain done :)");
7375
Object.initialized = true;
7476
end
77+
local total_time = obe.Time.epoch() - start_time;
78+
print("Scene loaded in", total_time, "seconds");
7579
end
7680

7781
function Local.Init(toBind)

Sokoban/Data/Maps/Bug.map.vili

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Meta:
2+
name:"Bug"
3+
4+
View:
5+
size:1.0
6+
position:
7+
x:0.0
8+
y:0.0
9+
unit:"SceneUnits"
10+
Referential:"TopLeft"
11+
12+
GameObjects:
13+
robot:
14+
type:"Robot"

Sokoban/Data/config.cfg.vili

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
KeyBinding:
1+
Input:
22
game:
3-
UP:"Hold:Z"
4-
LEFT:"Hold:Q"
5-
RIGHT:"Hold:D"
6-
DOWN:"Hold:S"
7-
Reset:"Pressed:R"
8-
Next:"Pressed:Return"
3+
UP: "Hold:Z"
4+
LEFT: "Hold:Q"
5+
RIGHT: "Hold:D"
6+
DOWN: "Hold:S"
7+
Reset: "Pressed:R"
8+
Next: "Pressed:Return"
99

1010
Debug:
11-
logLevel:1
11+
logLevel: info
12+
13+
Window:
14+
game:
15+
fullscreen: false

Sokoban/boot.lua

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
function Popipo()
2-
print("POPIPO")
3-
end
4-
51
function Game.Start()
62
Engine.Scene:loadFromFile("Data/Maps/Menu.map.vili");
73
end

0 commit comments

Comments
 (0)