Skip to content

Commit

Permalink
Multiplayer: Send system graphic for nick text
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghabry committed Dec 15, 2021
1 parent dab2f84 commit 47f5f5e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
52 changes: 50 additions & 2 deletions src/game_multiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "game_multiplayer.h"
#include "chat_multiplayer.h"
#include "game_system.h"
#include "output.h"
#include "game_player.h"
#include "sprite_character.h"
Expand All @@ -32,10 +33,15 @@ class ChatName : public Drawable {

void Draw(Bitmap& dst) override;

void SetSystemGraphic(StringView sys_name);

private:
Player& player;
std::string nickname;
BitmapRef nick_img;
BitmapRef sys_graphic;
std::shared_ptr<int> request_id;
bool dirty = true;
};

struct Player {
Expand All @@ -53,10 +59,11 @@ void ChatName::Draw(Bitmap& dst) {
auto sprite = player.sprite.get();
if (!nicks_visible || nickname.empty() || !sprite) {
nick_img.reset();
dirty = true;
return;
}

if (!nick_img) {
if (dirty) {
// Up to 3 utf-8 characters
Utils::UtfNextResult utf_next;
utf_next.next = nickname.data();
Expand All @@ -77,12 +84,34 @@ void ChatName::Draw(Bitmap& dst) {

nick_img = Bitmap::Create(rect.width + 1, rect.height + 1, true);

Text::Draw(*nick_img, 0, 0, *Font::Default(), *Cache::SystemOrBlack(), ((int)nick_trim[0]) % 20, nick_trim);
BitmapRef sys;
if (sys_graphic) {
sys = sys_graphic;
} else {
sys = Cache::SystemOrBlack();
}

Text::Draw(*nick_img, 0, 0, *Font::Default(), *sys, 0, nick_trim);

dirty = false;
}

int x = player.ch->GetScreenX() - nick_img->GetWidth() / 2 - 1;
int y = player.ch->GetScreenY() - TILE_SIZE * 2;
dst.Blit(x, y, *nick_img, nick_img->GetRect(), Opacity::Opaque());
}

void ChatName::SetSystemGraphic(StringView sys_name) {
FileRequestAsync* request = AsyncHandler::RequestFile("System", sys_name);
request_id = request->Bind([this](FileRequestResult* result) {
if (!result->success) {
return;
}
sys_graphic = Cache::System(result->file);
dirty = true;
});
request->SetGraphicFile(true);
request->Start();
};

std::string multiplayer__my_name = "";
Expand Down Expand Up @@ -154,6 +183,11 @@ namespace {
TrySend(msg);
}

void SendSystemName(StringView sys) {
std::string msg = "sys" + delimchar + ToString(sys);
TrySend(msg);
}

//this assumes that the player is stopped
void MovePlayerToPos(std::unique_ptr<Game_PlayerOther> &player, int x, int y) {
if (!player->IsStopping()) {
Expand Down Expand Up @@ -314,6 +348,16 @@ namespace {

players[id].ch->SetSpriteGraphic(v[2], idx);
}
else if (v[0] == "sys") {
if (v.size() < 3) {
return EM_FALSE;
}

auto chat_name = players[id].chat_name.get();
if (chat_name) {
chat_name->SetSystemGraphic(v[2]);
}
}
else if (v[0] == "name") { // nickname
if (v.size() < 3) {
return EM_FALSE;
Expand Down Expand Up @@ -416,6 +460,10 @@ void Game_Multiplayer::MainPlayerChangedSpriteGraphic(std::string name, int inde
SendMainPlayerSprite(name, index);
}

void Game_Multiplayer::SystemGraphicChanged(StringView sys) {
SendSystemName(sys);
}

void Game_Multiplayer::Update() {
for (auto& p : players) {
auto& q = p.second.mvq;
Expand Down
2 changes: 2 additions & 0 deletions src/game_multiplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define EP_GAME_MULTIPLAYER_H

#include <string>
#include "string_view.h"

namespace Game_Multiplayer {
void Connect(int map_id);
Expand All @@ -10,6 +11,7 @@ namespace Game_Multiplayer {
void MainPlayerMoved(int dir);
void MainPlayerChangedMoveSpeed(int spd);
void MainPlayerChangedSpriteGraphic(std::string name, int index);
void SystemGraphicChanged(StringView sys);
}

#endif
3 changes: 2 additions & 1 deletion src/game_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,13 @@ void Game_Player::Update() {
}
}

if (Game_Clock::now() - em_timer > 5s) {
if (Game_Clock::now() - em_timer > 10s) {
Game_Multiplayer::MainPlayerChangedSpriteGraphic(GetSpriteName(), GetSpriteIndex());
Game_Multiplayer::MainPlayerChangedMoveSpeed(GetMoveSpeed());
if (!IsMoving()) {
Game_Multiplayer::MainPlayerMoved(GetDirection());
}
Game_Multiplayer::SystemGraphicChanged(Main_Data::game_system->GetSystemName());
em_timer = Game_Clock::now();
}
}
Expand Down
1 change: 1 addition & 0 deletions src/game_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ StringView Game_System::GetSystemName() {

void Game_System::OnChangeSystemGraphicReady(FileRequestResult* result) {
Cache::SetSystemName(result->file);
Game_Multiplayer::SystemGraphicChanged(result->file);
bg_color = Cache::SystemOrBlack()->GetBackgroundColor();

Scene_Map* scene = (Scene_Map*)Scene::Find(Scene::Map).get();
Expand Down

0 comments on commit 47f5f5e

Please # to comment.