Skip to content

Instantly share code, notes, and snippets.

@Luke256

Luke256/Main.cpp Secret

Last active March 12, 2021 09:10
Show Gist options
  • Save Luke256/e06ce94a9a6d3a16e1e35cfec3702790 to your computer and use it in GitHub Desktop.
Save Luke256/e06ce94a9a6d3a16e1e35cfec3702790 to your computer and use it in GitHub Desktop.
Photonサンプル - 簡易チャット
#include "SceneMaster.hpp"
using MyScene = Utility::SceneMaster<s3d::String>;
class Chat : public MyScene::Scene {
private:
ExitGames::Common::Hashtable m_hashTable; //ゲームの情報
bool Connecting; // 接続状態
// 接続処理の後に行われる処理
void ConnectReturn(int errorCode, const ExitGames::Common::JString& errorString, const ExitGames::Common::JString& region, const ExitGames::Common::JString& cluster) override {
if (errorCode) { // エラー発生
s3d::Print(U"接続出来ませんでした");
return;
}
// 正常に接続
s3d::Print(U"接続しました");
GetClient().opJoinRandomRoom(m_hashTable, 2); // 第2引数でルームに参加できる人数を設定します。
}
// 切断処理の後に行われる処理
void DisconnectReturn() override {
Connecting=false;
s3d::Print(U"切断しました");
}
// 部屋の作成の処理の後に行われる処理
void CreateRoomReturn(int localPlayerNr,
const ExitGames::Common::Hashtable& roomProperties,
const ExitGames::Common::Hashtable& playerProperties,
int errorCode,
const ExitGames::Common::JString& errorString) override {
if (errorCode) { // エラー発生
s3d::Print(U"部屋を作成出来ませんでした");
Disconnect();
return;
}
// 正常に作成
Connecting=true;
s3d::Print(U"部屋を作成しました!");
}
// 部屋への接続処理の後に行われる処理
void JoinRandomRoomReturn(int localPlayerNr,
const ExitGames::Common::Hashtable& roomProperties,
const ExitGames::Common::Hashtable& playerProperties,
int errorCode,
const ExitGames::Common::JString& errorString) override {
if (errorCode) { // エラー発生(接続可能な部屋がない)
s3d::Print(U"部屋が見つかりませんでした");
// 部屋の作成
CreateRoom(L"", m_hashTable, 2);
s3d::Print(U"部屋を作成します...");
return;
}
// 正常に部屋に接続
Connecting=true;
s3d::Print(U"部屋に接続しました!");
}
// 誰かが部屋に接続した時の処理
void JoinRoomEventAction(int playerNr, const ExitGames::Common::JVector<int>& playernrs, const ExitGames::LoadBalancing::Player& player) override {
// 部屋に接続したのが自分の場合、早期リターン
if (GetClient().getLocalPlayer().getNumber() == player.getNumber()) return;
s3d::Print(U"相手が入室しました。");
}
// 誰かが部屋から切断した時の処理
void LeaveRoomEventAction(int playerNr, bool isInactive) override {
s3d::Print(U"相手が退室しました。");
}
// 情報を受け取った時の処理
void CustomEventAction(int playerNr, nByte eventCode, const ExitGames::Common::Object& eventContent) override {
// 受け取った内容を変換
auto dic = ExitGames::Common::ValueObject<ExitGames::Common::Dictionary<nByte,int32>>(eventContent).getDataCopy();
// 受け取った内容で分岐
switch (s3d::int32(*dic.getValue(1))){
case 1:{
s3d::Print(U"相手:こんにちは!");
break;
}
case 2:{
s3d::Print(U"相手:Hello!");
break;
}
}
}
public:
Chat(const InitData& init_)
: IScene(init_)
,Connecting(false){
// ゲーム情報の追加
m_hashTable.put(L"gameType", L"photonSample");
// タイムスタンプの取得
GetClient().fetchServerTimestamp();
}
void update() override {
if (s3d::SimpleGUI::ButtonAt(U"接続", s3d::Scene::Center().moveBy(0,-100)) && !Connecting) {
Connect(); // この関数を呼び出せば接続できる。
s3d::ClearPrint();
s3d::Print(U"接続中...");
}
// 接続中の処理
if (Connecting){
if (s3d::SimpleGUI::ButtonAt(U"こんにちは!", s3d::Scene::Center().moveBy(0,100),150)){
// 送信内容の作成
ExitGames::Common::Dictionary<nByte, int32> dic;
dic.put(1, 1);
// 送信
GetClient().opRaiseEvent(true, dic, 1);
s3d::Print(U"あなた:こんにちは!");
}
if (s3d::SimpleGUI::ButtonAt(U"Hello!", s3d::Scene::Center().moveBy(0,180),150)){
// 送信内容の作成
ExitGames::Common::Dictionary<nByte, int32> dic;
dic.put(1, 2);
// 送信
GetClient().opRaiseEvent(true, dic, 1);
s3d::Print(U"あなた:Hello!");
}
// 切断
if (s3d::SimpleGUI::ButtonAt(U"切断", s3d::Scene::Center().moveBy(0,-100),100)) {
Disconnect(); // この関数を呼び出せば切断できる。
}
}
// 切断中の処理
else{
// 接続
if (s3d::SimpleGUI::ButtonAt(U"接続", s3d::Scene::Center().moveBy(0,-100),100)) {
Connect(); // この関数を呼び出せば接続できる。
s3d::ClearPrint();
s3d::Print(U"接続中...");
}
}
}
void draw() const override {
}
};
void Main() {
// 使用するフォントアセットを登録
s3d::FontAsset::Register(U"Menu", 30, s3d::Typeface::Regular);
// シーンを設定
MyScene manager(L"/*ここを自分のappIDに変更してください*/", L"1.0");
manager.add<Chat>(U"Match");
while (s3d::System::Update()) {
if (!manager.update()) break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment