Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Backup of a work in progress
Browse files Browse the repository at this point in the history
- Implementing user experience (not complete at all)
* Now the users are all blocks (they have smooth animations when walking now)
- Camera is the 1:1 by default (stretch auto)
- Changed display_async to display (as planned)
- Added "update in real time first", so the user sees the latest keyboard action in screen even before server confirmation
- Randomized more the spawn of users
- Added filter if user keyboard update isn't a move it won't send empty "packs_user_update"
- Added release_plans.txt (the releases tab should follow this ruleset)

Still only localhost mode for now. This will change in the future (allowing you to host your own 8v8)
  • Loading branch information
Lohkdesgds committed Nov 21, 2021
1 parent e9c1f4a commit 08655b8
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 91 deletions.
8 changes: 7 additions & 1 deletion Core/Modules/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using namespace Lunaris;

constexpr size_t world_map_width = 32;
constexpr size_t world_map_height = 18;
constexpr double world_delta_update_time_package = 1.0 / 20;
constexpr double world_delta_update_time_package = 1.0 / 80;
constexpr double user_update_max_rate = 1.0 / 1000;

constexpr uint32_t max_users_amount = 8;
Expand All @@ -15,5 +15,11 @@ constexpr size_t username_length_max = 64;
const std::string default_host_ip = "localhost";
constexpr u_short default_host_port = 36963;


const std::string masterkey_config = "keybinds";
const size_t num_of_keys_wasd = 4;
const std::string key_wasd_keys[num_of_keys_wasd] = { "up", "left", "down", "right" };
const int32_t key_wasd_default[num_of_keys_wasd] = { ALLEGRO_KEY_W, ALLEGRO_KEY_A, ALLEGRO_KEY_S, ALLEGRO_KEY_D };

#define IABS(X) (X > 0 ? X : -X)
#define TOONESCALE(X) (X > 0 ? 1 : (X < 0 ? -1 : 0))
6 changes: 6 additions & 0 deletions Core/Modules/gamecore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include <Lunaris/all.h>
#include "defaults.h"

using namespace Lunaris;
101 changes: 82 additions & 19 deletions Core/Modules/gamedraw.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
#include "gamedraw.h"

UserInMap::UserInMap()
{
reset();
}

void UserInMap::set_name(const std::string& str)
{
//txt.set<safe_data<std::string>>(enum_text_safe_string_e::STRING, str);
}

void UserInMap::set_color(const color& c)
{
blk.set<color>(enum_sprite_color_e::DRAW_DRAW_BOX, c);
}

void UserInMap::reset()
{
blk.set<bool>(enum_sprite_boolean_e::DRAW_DRAW_BOX, true);
blk.set<float>(enum_sprite_float_e::SCALE_X, 2.0f / world_map_width);
blk.set<float>(enum_sprite_float_e::SCALE_Y, 2.0f / world_map_height);
set_color(color(127, 127, 127));
user_id = 0;
set_name("");
}


void GameDraw::draw_map_function()
{
if (textures.size() == 0) {
Expand All @@ -9,37 +35,71 @@ void GameDraw::draw_map_function()

color(16,16,16).clear_to_this();

const float map_x = disp.get_width() * 1.0f / connection.get_width();
const float map_y = disp.get_height() * 1.0f / connection.get_height();
//const float map_x = disp.get_width() * 1.0f / connection.get_width();
//const float map_y = disp.get_height() * 1.0f / connection.get_height();

// for now let's just draw

transform easy;
easy.identity();
easy.build_classic_fixed_proportion_stretched_auto(1.0f);
easy.apply();

connection.iterate_map_auto(
[this, &map_x, &map_y](const uint32_t xx, const uint32_t yy, const int32_t blkid)
[this](const uint32_t xx, const uint32_t yy, const int32_t blkid)
{
const auto ref = std::find_if(textures.begin(), textures.end(), [&](TextureMap& tm) {return tm.block_id == blkid; });
if (ref == textures.end()) {
cout << console::color::YELLOW << "UNKNOWN TEXTURE ID " << blkid;
}
else {
ref->bmp->draw_scaled_at(((xx * 2.0f / world_map_width) - 1.0f), ((yy * 2.0f / world_map_height) - 1.0f), (2.0f / world_map_width), (2.0f / world_map_height));
}
}
);

std::vector<int32_t> usrs;
connection.iterate_users_auto(
[this, &usrs](const WorldUser& usr)
{
usrs.push_back(usr.user_id);
}
);

ref->bmp->draw_scaled_at(xx * map_x, yy * map_y, map_x, map_y);

for (auto& it : users_transl) {
if (std::find_if(usrs.begin(), usrs.end(), [&](int32_t& e) { return e == it.user_id; }) == usrs.end())
{
it.reset(); // reset config of this thing
}
}

//ref->bmp->draw_scaled_at(xx * map_x, yy * map_y, map_x, map_y);
//al_draw_filled_rectangle(xx * map_x, yy * map_y, map_x, map_y, color((int)(70 + random() % 50), 70 + random() % 50, 70 + random() % 50));
for (const auto& it : usrs) {
if (auto fnd = std::find_if(std::begin(users_transl), std::end(users_transl), [&](UserInMap& e) { return e.user_id == it; }); fnd == std::end(users_transl))
{
for (auto& e : users_transl) {
if (e.user_id == 0) {
e.user_id = it;
e.set_color(e.user_id == connection.get_my_id() ? color(235, 235, 235) : color(127, 127, 127));
e.blk.set<float>(enum_sprite_float_e::RO_DRAW_PROJ_POS_X, 0.0f); // from center
e.blk.set<float>(enum_sprite_float_e::RO_DRAW_PROJ_POS_Y, 0.0f); // from center
break;
}
}
}
);
}

connection.iterate_users_auto(
[this, &map_x, &map_y](const WorldUser& usr)
[this](const WorldUser& usr)
{
al_draw_filled_rectangle(usr.posx * 1.0f * map_x, usr.posy * 1.0f * map_y, (usr.posx + 1) * 1.0f * map_x, (usr.posy + 1) * 1.0f * map_y, usr.user_id == connection.get_my_id() ? color(235, 235, 235) : color(127, 127, 127));
if (auto fnd = std::find_if(std::begin(users_transl), std::end(users_transl), [&](UserInMap& e) { return e.user_id == usr.user_id; }); fnd != std::end(users_transl))
{
cout << usr.user_id << " [" << usr.posx << ";" << usr.posy << "]";

fnd->blk.set<float>(enum_sprite_float_e::POS_X, (((usr.posx * 2.0f / world_map_width) - 1.0f) + 0.5f * fnd->blk.get<float>(enum_sprite_float_e::SCALE_X)));
fnd->blk.set<float>(enum_sprite_float_e::POS_Y, (((usr.posy * 2.0f / world_map_height) - 1.0f) + 0.5f * fnd->blk.get<float>(enum_sprite_float_e::SCALE_Y)));
//fnd->blk.think();
fnd->blk.draw();
}
//al_draw_filled_rectangle(usr.posx * 1.0f * map_x, usr.posy * 1.0f * map_y, (usr.posx + 1) * 1.0f * map_x, (usr.posy + 1) * 1.0f * map_y, usr.user_id == connection.get_my_id() ? color(235, 235, 235) : color(127, 127, 127));
}
);
}
Expand All @@ -56,17 +116,17 @@ void GameDraw::on_key_send_function(const keys::key_event& ev)
if (kbkeys.is_key_pressed(a_key)) leftright = -1;
if (kbkeys.is_key_pressed(d_key)) leftright = leftright != 0 ? 0 : 1;

connection.make_move_player(leftright, updown);
if (leftright != 0 || updown != 0) connection.make_move_player(leftright, updown);
}

GameDraw::GameDraw(display_async& d, config& c)
GameDraw::GameDraw(display& d, config& c)
: disp(d), conf_keys(c)
{
}

void GameDraw::add_texture(hybrid_memory<texture> textur, uint32_t id)
{
if (is_display_drawing_this) throw std::runtime_error("You should have set this before connect_and_enable_draw_and_keyboard!");
if (is_display_drawing_this) throw std::runtime_error("You should have set this before connect_and_enable_keyboard!");

for (auto& it : textures) {
if (it.block_id == id) {
Expand All @@ -79,32 +139,35 @@ void GameDraw::add_texture(hybrid_memory<texture> textur, uint32_t id)

void GameDraw::set_user_name(const std::string& nam)
{
if (is_display_drawing_this) throw std::runtime_error("You should have set this before connect_and_enable_draw_and_keyboard!");
if (is_display_drawing_this) throw std::runtime_error("You should have set this before connect_and_enable_keyboard!");
user_name = nam.substr(0, username_length_max - 1);
}

void GameDraw::check_config()
{
if (is_display_drawing_this) throw std::runtime_error("You should have set this before connect_and_enable_draw_and_keyboard!");
if (is_display_drawing_this) throw std::runtime_error("You should have set this before connect_and_enable_keyboard!");
for (size_t p = 0; p < num_of_keys_wasd; p++) conf_keys.ensure(masterkey_config, key_wasd_keys[p], key_wasd_default[p], config::config_section_mode::SAVE); // save config
w_key = conf_keys.get_as<int32_t>(masterkey_config, key_wasd_keys[0]); // load each
a_key = conf_keys.get_as<int32_t>(masterkey_config, key_wasd_keys[1]); // load each
s_key = conf_keys.get_as<int32_t>(masterkey_config, key_wasd_keys[2]); // load each
d_key = conf_keys.get_as<int32_t>(masterkey_config, key_wasd_keys[3]); // load each
}

bool GameDraw::connect_and_enable_draw_and_keyboard()
bool GameDraw::connect_and_enable_keyboard()
{
if (!connection.connect(user_name)) return false;
kbkeys.hook_event([this](const keys::key_event& ev) { on_key_send_function(ev); });
disp.hook_draw_function([this](const auto&) { draw_map_function(); });
is_display_drawing_this = true;
return true;
}

void GameDraw::draw_this()
{
draw_map_function();
}

void GameDraw::disconnect()
{
disp.unhook_draw_function();
kbkeys.unhook_event();
connection.disconnect();
}
24 changes: 16 additions & 8 deletions Core/Modules/gamedraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,42 @@

using namespace Lunaris;

const std::string masterkey_config = "keybinds";

const size_t num_of_keys_wasd = 4;
const std::string key_wasd_keys[num_of_keys_wasd] = {"up", "left", "down", "right"};
const int32_t key_wasd_default[num_of_keys_wasd] = {ALLEGRO_KEY_W, ALLEGRO_KEY_A, ALLEGRO_KEY_S, ALLEGRO_KEY_D};

struct TextureMap {
uint32_t block_id = 0;
hybrid_memory<texture> bmp;
};

struct UserInMap {
uint32_t user_id = 0;
block blk; // draw smooth on screen
//text txt; // its name // non implemented yet

UserInMap();
void set_name(const std::string&);
void set_color(const color&);
void reset();
};

class GameDraw {
WorldClient connection;
std::vector<TextureMap> textures;
bool is_display_drawing_this = false;
display_async& disp;
display& disp;
config& conf_keys;
keys kbkeys;
std::string user_name;
int32_t w_key = 0, a_key = 0, s_key = 0, d_key = 0; // shortcut
UserInMap users_transl[max_users_amount];

void draw_map_function();
void on_key_send_function(const keys::key_event&);
public:
GameDraw(display_async&, config&);
GameDraw(display&, config&);
void add_texture(hybrid_memory<texture>, uint32_t);
void set_user_name(const std::string&);
void check_config(); // guarantee WASD
bool connect_and_enable_draw_and_keyboard();
bool connect_and_enable_keyboard();
void draw_this();
void disconnect();
};
75 changes: 37 additions & 38 deletions Core/Modules/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ void Host::loop_listen()
for (uint32_t p = 0; p < this->worldmap.get_user_amount(); p++) {
auto& eache = worldmap.get_user_map()[p];
if (eache.user_id == 0) {
eache.user_id = cli.user_id;
memcpy_s(&eache.name, sizeof(eache.name), &cli.name, sizeof(cli.name));
eache.posx = 1 + random() % 10;
eache.posy = 1 + random() % 10;
eache.posx = 1 + random() % (world_map_width - 2);
eache.posy = 1 + random() % (world_map_height - 2);
eache.user_id = cli.user_id;
found_one_good = true;
return;
}
Expand Down Expand Up @@ -87,41 +87,6 @@ void Host::loop_update_users_and_map_gen()
{
clients.safe([this](std::vector<EachClient>& vec) {

// generate new info
package pkg = this->worldmap.generate_pack().make_package(); // most up to date EVER

// update users
for(size_t p = 0; p < vec.size(); p++)
{
auto& it = vec[p];

if (!it.client.send(pkg)) {
if (!it.client.has_socket()) {
cout << "Client \"" << it.name << "\" aka ID '" << it.user_id << "' disconnected.";

for (uint32_t p = 0; p < this->worldmap.get_user_amount(); p++) {
auto& eache = worldmap.get_user_map()[p];
if (eache.user_id == it.user_id) {
eache.user_id = 0;
break;
}
}

it.client.close_socket(); // disconnect
vec.erase(vec.begin() + p);
p--;
continue;
}
else {
cout << "Client \"" << it.name << "\" aka ID '" << it.user_id << "' is having buffering issues.";
}
}
else {
//cout << console::color::DARK_AQUA << "- update map #" << it.user_id << " ->";
}
}


// update user stuff they sent
for (size_t p = 0; p < vec.size(); p++)
{
Expand Down Expand Up @@ -157,6 +122,40 @@ void Host::loop_update_users_and_map_gen()
}
}
}

// generate new info
package pkg = this->worldmap.generate_pack().make_package(); // most up to date EVER

// update users
for(size_t p = 0; p < vec.size(); p++)
{
auto& it = vec[p];

if (!it.client.send(pkg)) {
if (!it.client.has_socket()) {
cout << "Client \"" << it.name << "\" aka ID '" << it.user_id << "' disconnected.";

for (uint32_t p = 0; p < this->worldmap.get_user_amount(); p++) {
auto& eache = worldmap.get_user_map()[p];
if (eache.user_id == it.user_id) {
eache.user_id = 0;
break;
}
}

it.client.close_socket(); // disconnect
vec.erase(vec.begin() + p);
p--;
continue;
}
else {
cout << "Client \"" << it.name << "\" aka ID '" << it.user_id << "' is having buffering issues.";
}
}
else {
//cout << console::color::DARK_AQUA << "- update map #" << it.user_id << " ->";
}
}
});
}

Expand Down
5 changes: 5 additions & 0 deletions Core/Modules/worldclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ void WorldClient::make_move_player(int32_t x, int32_t y)
for (uint32_t p = 0; p < this->get_user_amount(); p++) {
WorldUser& each = user_map[p];
if (each.user_id == usrupd.id) {
usrupd.posx = each.posx + x;
usrupd.posy = each.posy + y;

if (each.posx == 0 && x < 0) return; // not allowed
if (x > 0 && each.posx + x >= get_width()) return; // not allowed
Expand All @@ -96,6 +98,9 @@ void WorldClient::make_move_player(int32_t x, int32_t y)
usrupd.posx = each.posx + x;
usrupd.posy = each.posy + y;

each.posx = usrupd.posx;
each.posy = usrupd.posy;

PACK pkg;
pkg.make_this_user_update(usrupd);
if (user.client.send(pkg.make_package())) {
Expand Down
1 change: 1 addition & 0 deletions Core/TheBlast.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
<ItemGroup>
<ClInclude Include="Modules\defaults.h" />
<ClInclude Include="Modules\eachclient.h" />
<ClInclude Include="Modules\gamecore.h" />
<ClInclude Include="Modules\gamedraw.h" />
<ClInclude Include="Modules\host.h" />
<ClInclude Include="Modules\pack.h" />
Expand Down
Loading

0 comments on commit 08655b8

Please sign in to comment.