testmodules/minigame: Use new type table format
This commit is contained in:
parent
2d637cee68
commit
7a19735e6f
@ -20,32 +20,35 @@ local player_boxes = {}
|
|||||||
local field_boxes = {}
|
local field_boxes = {}
|
||||||
|
|
||||||
buildat.sub_packet("minigame:update", function(data)
|
buildat.sub_packet("minigame:update", function(data)
|
||||||
values = cereal.binary_input(data, {
|
log:info("data="..buildat.dump(buildat.bytes(data)))
|
||||||
"int32", "int32", "int32", "int32"
|
values = cereal.binary_input(data, {"object",
|
||||||
})
|
{"peer", "int32_t"},
|
||||||
local peer = values[1]
|
{"players", {"unordered_map",
|
||||||
local num_players = values[2]
|
"int32_t",
|
||||||
local field_w = values[3]
|
{"object",
|
||||||
local field_h = values[4]
|
{"peer", "int32_t"},
|
||||||
values = cereal.binary_input(data, {
|
{"x", "int32_t"},
|
||||||
"int32", "int32", "int32", "int32",
|
{"y", "int32_t"},
|
||||||
{"int32", field_w*field_h}, {"int32", num_players*3}
|
},
|
||||||
|
}},
|
||||||
|
{"playfield", {"object",
|
||||||
|
{"w", "int32_t"},
|
||||||
|
{"h", "int32_t"},
|
||||||
|
{"tiles", {"array", "int32_t"}},
|
||||||
|
}},
|
||||||
})
|
})
|
||||||
--log:info("values="..dump(values))
|
--log:info("values="..dump(values))
|
||||||
local new_field = {}
|
|
||||||
for i=1,field_w*field_h do
|
field = values.playfield
|
||||||
table.insert(new_field, values[4+i])
|
|
||||||
end
|
|
||||||
field = new_field
|
|
||||||
log:info("field="..dump(field))
|
log:info("field="..dump(field))
|
||||||
|
|
||||||
for _, box in ipairs(field_boxes) do
|
for _, box in ipairs(field_boxes) do
|
||||||
scene:removeEntity(box)
|
scene:removeEntity(box)
|
||||||
end
|
end
|
||||||
field_boxes = {}
|
field_boxes = {}
|
||||||
for y=1,field_h do
|
for y=1,field.h do
|
||||||
for x=1,field_w do
|
for x=1,field.w do
|
||||||
local v = field[(y-1)*field_w + (x-1) + 1]
|
local v = field.tiles[(y-1)*field.w + (x-1) + 1]
|
||||||
if v ~= 0 then
|
if v ~= 0 then
|
||||||
box = g3d.ScenePrimitive(g3d.ScenePrimitive.TYPE_BOX, 1,0.5*v,1)
|
box = g3d.ScenePrimitive(g3d.ScenePrimitive.TYPE_BOX, 1,0.5*v,1)
|
||||||
box:loadTexture("minigame/green_texture.png")
|
box:loadTexture("minigame/green_texture.png")
|
||||||
@ -56,18 +59,12 @@ buildat.sub_packet("minigame:update", function(data)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local new_players = {}
|
local player_map = values.players
|
||||||
local players_start = 5+field_w*field_h
|
|
||||||
for i=1,num_players do
|
|
||||||
local player = {
|
|
||||||
peer = values[players_start+(i-1)*3+0],
|
|
||||||
x = values[players_start+(i-1)*3+1],
|
|
||||||
y = values[players_start+(i-1)*3+2],
|
|
||||||
}
|
|
||||||
table.insert(new_players, player)
|
|
||||||
end
|
|
||||||
local old_players = players
|
local old_players = players
|
||||||
players = new_players
|
players = {}
|
||||||
|
for k, player in pairs(player_map) do
|
||||||
|
table.insert(players, player)
|
||||||
|
end
|
||||||
log:info("players="..dump(players))
|
log:info("players="..dump(players))
|
||||||
|
|
||||||
for _, player in ipairs(players) do
|
for _, player in ipairs(players) do
|
||||||
@ -85,7 +82,7 @@ buildat.sub_packet("minigame:update", function(data)
|
|||||||
scene:addEntity(box)
|
scene:addEntity(box)
|
||||||
player_boxes[player.peer] = box
|
player_boxes[player.peer] = box
|
||||||
end
|
end
|
||||||
local v = field[(player.y)*field_w + (player.x) + 1] or 0
|
local v = field.tiles[(player.y)*field.w + (player.x) + 1] or 0
|
||||||
player_boxes[player.peer]:setPosition(player.x-5, 0.5+v*0.5, player.y-5)
|
player_boxes[player.peer]:setPosition(player.x-5, 0.5+v*0.5, player.y-5)
|
||||||
end
|
end
|
||||||
for _, old_player in ipairs(old_players) do
|
for _, old_player in ipairs(old_players) do
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#include "network/api.h"
|
#include "network/api.h"
|
||||||
#include "core/log.h"
|
#include "core/log.h"
|
||||||
#include <cereal/archives/portable_binary.hpp>
|
#include <cereal/archives/portable_binary.hpp>
|
||||||
|
#include <cereal/types/unordered_map.hpp>
|
||||||
|
#include <cereal/types/vector.hpp>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
using interface::Event;
|
using interface::Event;
|
||||||
@ -31,6 +33,12 @@ struct Playfield
|
|||||||
return;
|
return;
|
||||||
tiles[i] = v;
|
tiles[i] = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void serialize(Archive &archive){
|
||||||
|
archive((int32_t)w, (int32_t)h);
|
||||||
|
archive(tiles);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Player
|
struct Player
|
||||||
@ -38,7 +46,13 @@ struct Player
|
|||||||
int peer = 0;
|
int peer = 0;
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
|
|
||||||
Player(int peer = 0, int x = 0, int y = 0): peer(peer), x(x), y(y){}
|
Player(int peer = 0, int x = 0, int y = 0): peer(peer), x(x), y(y){}
|
||||||
|
|
||||||
|
template<class Archive>
|
||||||
|
void serialize(Archive &archive){
|
||||||
|
archive((int32_t)peer, (int32_t)x, (int32_t)y);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Module: public interface::Module
|
struct Module: public interface::Module
|
||||||
@ -92,17 +106,8 @@ struct Module: public interface::Module
|
|||||||
{
|
{
|
||||||
cereal::PortableBinaryOutputArchive ar(os);
|
cereal::PortableBinaryOutputArchive ar(os);
|
||||||
ar((int32_t)peer);
|
ar((int32_t)peer);
|
||||||
ar((int32_t)m_players.size());
|
ar(m_players);
|
||||||
ar((int32_t)m_playfield.w, (int32_t)m_playfield.h);
|
ar(m_playfield);
|
||||||
// TODO: A way for Lua to read vectors directly
|
|
||||||
for(int t : m_playfield.tiles)
|
|
||||||
ar(t);
|
|
||||||
for(auto &pair : m_players){
|
|
||||||
auto &player = pair.second;
|
|
||||||
ar((int32_t)player.peer);
|
|
||||||
ar((int32_t)player.x);
|
|
||||||
ar((int32_t)player.y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
network::access(m_server, [&](network::Interface * inetwork){
|
network::access(m_server, [&](network::Interface * inetwork){
|
||||||
inetwork->send(peer, "minigame:update", os.str());
|
inetwork->send(peer, "minigame:update", os.str());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user