Skip to content

Commit f6de109

Browse files
committed
suppression topdown , ameliration physique 2d platformer, optimisation platformer , correction camera , ajout table lua en json
1 parent 6161f8b commit f6de109

26 files changed

Lines changed: 592 additions & 554 deletions

‎.github/workflows/build_macos.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ jobs:
6767
run: |
6868
git config user.name "github-actions"
6969
git config user.email "actions@github.com"
70-
git add bin/release/**
71-
git add bin/debug/**
70+
git add bin/release/GrnGameAppMacOs
71+
git add bin/debug/GrnGameAppMacOs
7272
git commit -m "Ajout des binaires macOS depuis le workflow" || echo "Aucun changement à committer"
7373
7474
# Pousser le commit

‎bin/debug/GrnGameAppLinux‎

-20 KB
Binary file not shown.

‎bin/debug/libGrnGame.a‎

-269 KB
Binary file not shown.

‎bin/release/GrnGameAppLinux‎

-11.9 KB
Binary file not shown.

‎bin/release/GrnGameAppWindows.exe‎

-11.5 KB
Binary file not shown.

‎bin/release/libGrnGame.a‎

-269 KB
Binary file not shown.

‎exemples/JsonExempleAndEncryption/src/main.lua‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- to your source code to further protect it from tampering.
33

44
-- Log configuration: 0 = Debug, 1 = Info, etc.
5-
utils.setLogLvl(0)
5+
utils.setLogLvl(1)
66

77
local init = false
88
local r, g, b = 0, 0, 0
@@ -16,6 +16,26 @@ local function setupSecurity()
1616
json.setIv(15, 1)
1717
end
1818

19+
-- little table test
20+
if not json.exists("test.json") then
21+
json.load("test.json")
22+
json.writeVariable("test.json", {
23+
name = "Alice",
24+
hp = 30,
25+
sword = { name = "sword", dmg = 30 }
26+
})
27+
else
28+
json.load("test.json")
29+
30+
local table = json.readVariable("test.json", "sword")
31+
table.name = "sword in name sword"
32+
-- if you want to put a table in value object already exist, you need to delete it ( you can add double or int in string object without delete it)
33+
json.deleteVariable("test.json", "sword.name")
34+
json.writeVariable("test.json", "sword.name", table)
35+
end
36+
37+
json.save("test.json")
38+
1939
if json.exists(FILE_NAME) then
2040
-- If the file exists, we must set the key BEFORE loading to DECRYPT it.
2141
setupSecurity()

‎exemples/hitbox2d/src/main.lua‎

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,48 @@ local jumpUp = { 33, 35 }
55
local jumpDown = { 36, 37 }
66
-- arg0,arg1 are the center of the screen, arg2 is the smooth factor and arg3,arg4 are the size of the screen view
77
game.createCamera(90, 90, 2.0, 180, 180)
8-
--arg0,arg1 are the position of player,arg2,arg3 are the hitbox,arg4 are the powerjump of player
9-
local player = game.EntityPlatformer(50.0, 50.0, 24, 24, -200)
8+
-- x,y,w,h,jumpforce,grativy,max speedx,maxfallspeed,wall correction,initial speed x,acceleration factor
9+
local player = game.EntityPlatformer(50.0, 50.0, 24, 24, -250, 500, 2, 500, 150, 0.3, 10.0)
1010
-- create a list of blocks you can also do : {game.block(...),game.block(...)....}
1111
local blocks = game.Blocks()
1212
--add somes blocks
13-
for i = -20, 20 do
13+
for i = -30, 30 do
1414
blocks:add(game.Block(i * 20.0, 148.0, 20, 20, 1))
1515
end
1616
blocks:add(game.Block(100.0, 100.0, 20, 20, 1)) -- Platform
1717
blocks:add(game.Block(120.0, 100.0, 20, 20, 1)) -- Platform
1818
blocks:add(game.Block(180.0, 70.0, 20, 20, 3)) -- Decorative block (Type 3)
19-
2019
local frame = 9
2120
local animTimer = 0.0
2221
local animSpeed = 0.15
2322
local flip = false
24-
2523
-- Load Assets
2624
image.loadFolder("assets/")
2725
local sprite = image.Sprite("assets/sprite.png", 32, 32)
28-
2926
-- Set Universe Size
3027
window.setUniversSize(180, 180)
31-
3228
-- Main Game Loop
3329
utils.setUpdateCallback(function()
3430
local dt = var.delta()
3531
local moving = false
3632
--leftLock is for prevent bugs
37-
if input.keyPressed("q") and not player.leftLock then
38-
-- use int value for moving the player (better camera) you can also using delta
39-
player.x = player.x - 2
33+
if input.keyPressed("q") then
34+
player.requestLeft = true
4035
flip = true
4136
moving = true
4237
end
43-
--rightLock is for prevent bugs
44-
if input.keyPressed("d") and not player.rightLock then
45-
-- use int value for moving the player (better camera) you can also using delta
46-
player.x = player.x + 2
38+
if input.keyPressed("d") then
39+
player.requestRight = true
4740
flip = false
4841
moving = true
4942
end
5043
if input.keyJustPressed("space") then
51-
-- send 1 requestJump
5244
player.requestJump = true
5345
end
5446
-- block type 3 is ignored
55-
game.hitboxPlatformer(player, blocks, 300.0, 150.0, { 3 })
47+
game.hitboxPlatformer(player, blocks, 1 << 3, dt)
5648
-- updateCamera is important
5749
game.updateCamera(player.x, player.y, dt)
58-
5950
local currentAnim = idle
6051
local loopAnim = true
6152
-- player in sky ?
@@ -71,9 +62,7 @@ utils.setUpdateCallback(function()
7162
if frame < currentAnim[1] or frame > currentAnim[2] then
7263
frame = currentAnim[1]
7364
end
74-
frame, animTimer = game.animate(
75-
frame,
76-
animTimer,
65+
frame, animTimer = game.animate(frame, animTimer,
7766
currentAnim[1], -- Start
7867
currentAnim[2], -- End
7968
animSpeed,

‎src/bindings_lua/bindings_sol2/fichier_json.cpp‎

Lines changed: 129 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/*
22
* Enregistrement des bindings GrnGame (JSON) dans la VM Lua.
33
*/
4+
#include "sol/forward.hpp"
5+
#include "sol/table.hpp"
46
#include <sol/sol.hpp>
7+
#include <string>
58

69
extern "C" {
710
#include "../../moteur/json/json.h"
11+
#include "../../moteur/logging/logging.h"
812
#include <lauxlib.h>
913
#include <lua.h>
1014
#include <lualib.h>
@@ -30,31 +34,85 @@ void lua_sauvegarder_json_fichier(const std::string &filename) {
3034
void lua_supprimer_json_fichier(const std::string &filename) {
3135
supprimer_fichier_json(filename.c_str());
3236
}
37+
/* ecrit recursivement du json */
38+
void ecrire_recursif_json(const std::string &filename, sol::object data,
39+
std::string chemin_actuel) {
40+
/* si la donnee est une table on boucle a linterieur */
41+
if (data.is<sol::table>()) {
42+
sol::table tbl = data.as<sol::table>();
43+
for (auto &elem : tbl) {
44+
std::string cle;
3345

34-
/* ecrit une variable dans le json en detectant automatiquement le type */
35-
void lua_ecrire_variable(const std::string &filename, const std::string &varName,
36-
sol::object value) {
37-
if (value.is<bool>()) {
38-
ecrire_int_json(filename.c_str(), varName.c_str(), value.as<bool>() ? 1 : 0);
39-
} else if (value.is<std::string>()) {
40-
ecrire_string_json(filename.c_str(), varName.c_str(), value.as<std::string>().c_str());
41-
} else if (value.is<double>()) {
42-
double val = value.as<double>();
43-
if (val == (int)val) {
44-
ecrire_int_json(filename.c_str(), varName.c_str(), (int)val);
45-
} else {
46-
ecrire_double_json(filename.c_str(), varName.c_str(), val);
46+
/* securite : on convertit la cle en string (pour eviter les crashs avec les tableaux
47+
* lua) */
48+
if (elem.first.is<std::string>()) {
49+
cle = elem.first.as<std::string>();
50+
} else if (elem.first.is<int>()) {
51+
cle = std::to_string(elem.first.as<int>());
52+
} else {
53+
continue; /* on ignore les cles non supportees */
54+
}
55+
56+
sol::object valeur = elem.second;
57+
std::string nouveau_chemin;
58+
59+
/* on construit le chemin en fonction de sil est vide ou pas */
60+
if (chemin_actuel.empty()) {
61+
nouveau_chemin = cle;
62+
} else {
63+
nouveau_chemin = chemin_actuel + "." + cle;
64+
}
65+
66+
/* appel recursif avec le nouveau chemin */
67+
ecrire_recursif_json(filename, valeur, nouveau_chemin);
68+
}
69+
}
70+
/* sinon cest une valeur simple on letablit au bout du chemin */
71+
else {
72+
/* on ne peut pas ecrire une valeur simple a la racine sans chemin */
73+
if (chemin_actuel.empty())
74+
return;
75+
76+
if (data.is<bool>()) {
77+
ecrire_int_json(filename.c_str(), chemin_actuel.c_str(), data.as<bool>() ? 1 : 0);
78+
} else if (data.is<std::string>()) {
79+
ecrire_string_json(filename.c_str(), chemin_actuel.c_str(),
80+
data.as<std::string>().c_str());
81+
} else if (data.is<double>()) {
82+
double val = data.as<double>();
83+
if (val == (int)val) {
84+
ecrire_int_json(filename.c_str(), chemin_actuel.c_str(), (int)val);
85+
} else {
86+
ecrire_double_json(filename.c_str(), chemin_actuel.c_str(), val);
87+
}
4788
}
4889
}
4990
}
5091

92+
/* expose a lua : ecrit une variable ou une table dans le json */
93+
void lua_ecrire_variable(const std::string &filename, sol::object arg1,
94+
sol::optional<sol::object> arg2) {
95+
96+
/* cas 1 : on donne un chemin en argument 1 et une valeur en argument 2 (peut etre une table) */
97+
if (arg1.is<std::string>() && arg2.has_value()) {
98+
std::string chemin = arg1.as<std::string>();
99+
sol::object valeur = arg2.value();
100+
ecrire_recursif_json(filename, valeur, chemin);
101+
}
102+
/* cas 2 : argument 1 est une table lua donc on ne donne pas dargument 2 */
103+
else if (arg1.is<sol::table>()) {
104+
ecrire_recursif_json(filename, arg1, "");
105+
}
106+
/* cas derreur : mauvais arguments depuis lua */
107+
else {
108+
log_fmt(NiveauLogErreur, "you give wrong arguments in json.writeVariable");
109+
}
110+
}
51111
/* Recupere une variable depuis le json avec detection auto */
52112
sol::object lua_lire_variable(const std::string &filename, const std::string &varName,
53113
sol::this_state s) {
54114
sol::state_view lua(s);
55-
56115
int type = recuperer_type_json(filename.c_str(), varName.c_str());
57-
58116
switch (type) {
59117
case JSON_TYPE_NOMBRE: {
60118
double val = recuperer_double_json(filename.c_str(), varName.c_str());
@@ -65,17 +123,67 @@ sol::object lua_lire_variable(const std::string &filename, const std::string &va
65123
}
66124
case JSON_TYPE_STRING: {
67125
char *val = recuperer_string_json(filename.c_str(), varName.c_str());
68-
if (val)
69-
return sol::make_object(lua, std::string(val));
126+
if (val) {
127+
sol::object obj = sol::make_object(lua, std::string(val));
128+
return obj;
129+
}
130+
break;
131+
}
132+
case JSON_TYPE_OBJECT: {
133+
/* recupere les chemins a partir du chemin de base */
134+
cJSON *tab = lister_tous_les_chemins(filename.c_str(), varName.c_str());
135+
/* table lua */
136+
sol::table table_resultat = lua.create_table();
137+
if (tab != NULL && cJSON_IsArray(tab)) {
138+
cJSON *element = NULL;
139+
/* parcours tableau cJSON */
140+
cJSON_ArrayForEach(element, tab) {
141+
if (cJSON_IsString(element) && element->valuestring) {
142+
std::string sous_chemin = element->valuestring;
143+
/* chemin complet ou la on peut recuperer la variable */
144+
std::string chemin_complet =
145+
varName.empty() ? sous_chemin : varName + "." + sous_chemin;
146+
147+
sol::object valeur_finale = lua_lire_variable(filename, chemin_complet, s);
148+
/* gestion de la table lua */
149+
std::stringstream ss(sous_chemin);
150+
std::string token;
151+
std::vector<std::string> cles;
152+
while (std::getline(ss, token, '.')) {
153+
cles.push_back(token);
154+
}
155+
sol::table curseur = table_resultat;
156+
for (size_t i = 0; i < cles.size(); ++i) {
157+
if (i == cles.size() - 1) {
158+
curseur[cles[i]] = valeur_finale;
159+
} else {
160+
sol::object enfant = curseur[cles[i]];
161+
if (!enfant.is<sol::table>()) {
162+
curseur[cles[i]] = lua.create_table();
163+
}
164+
curseur = curseur[cles[i]];
165+
}
166+
}
167+
}
168+
}
169+
/* liberation du tableau cJSON obligatoire */
170+
cJSON_Delete(tab);
171+
return table_resultat;
172+
} else {
173+
log_fmt(NiveauLogErreur, "Path error '%s' invalid in %s", varName.c_str(),
174+
filename.c_str());
175+
if (tab)
176+
cJSON_Delete(tab);
177+
}
70178
break;
71179
}
72-
default:
73-
/* affiche lerreur si le chemin nest pas valide */
180+
default: {
181+
/* affiche l'erreur si le chemin n'est pas valide, essaye double par defaut */
74182
double val = recuperer_double_json(filename.c_str(), varName.c_str());
75-
/* essaye de renvoyer la valeur quand meme */
76183
return sol::make_object(lua, val);
77-
break;
78184
}
185+
}
186+
79187
return sol::make_object(lua, sol::nil);
80188
}
81189

0 commit comments

Comments
 (0)