Protobuf生成


1. 下載protobuf :  https://github.com/protocolbuffers/protobuf

2.解壓后,配置bin目錄到環境變量

3.cmd窗口執行protoc命令,檢查protobuf環境變量是否配置成功

4.protoc --java_out=java文件生成目錄 proto文件

proto文件格式:

syntax = "proto3";

package protobuf;
option java_package = "com.wangfajun.tinygame.netty.protobuf";

// 消息代號
enum MsgCode {
    USER_ENTRY_CMD = 0;
    USER_ENTRY_RESULT = 1;
    WHO_ELSE_IS_HERE_CMD = 2;
    WHO_ELSE_IS_HERE_RESULT = 3;
    USER_MOVE_TO_CMD = 4;
    USER_MOVE_TO_RESULT = 5;
    USER_QUIT_RESULT = 6;
    USER_STOP_CMD = 7;
    USER_STOP_RESULT = 8;
    USER_ATTK_CMD = 9;
    USER_ATTK_RESULT = 10;
    USER_SUBTRACT_HP_RESULT = 11;
    USER_DIE_RESULT = 12;
};

// 
// 用戶入場
///////////////////////////////////////////////////////////////////////
// 指令
message UserEntryCmd {
    // 用戶 Id
    uint32 userId = 1;
    // 英雄形象
    string heroAvatar = 2;
}

// 結果
message UserEntryResult {
    // 用戶 Id
    uint32 userId = 1;
    // 英雄形象
    string heroAvatar = 2;
}

//
// 還有誰在場
///////////////////////////////////////////////////////////////////////
// 指令
message WhoElseIsHereCmd {
}

// 結果
message WhoElseIsHereResult {
    // 用戶信息數組
    repeated UserInfo userInfo = 1;

    // 用戶信息
    message UserInfo {
        // 用戶 Id
        uint32 userId = 1;
        // 英雄形象
        string heroAvatar = 2;
    }
}

// 
// 用戶移動
///////////////////////////////////////////////////////////////////////
// 指令
message UserMoveToCmd {
    // 
    // XXX 注意: 用戶移動指令中沒有用戶 Id,
    // 這是為什么?
    // 
    // 移動到位置 X
    float moveToPosX = 1;
    // 移動到位置 Y
    float moveToPosY = 2;
}

// 結果
message UserMoveToResult {
    // 移動用戶 Id
    uint32 moveUserId = 1;
    // 移動到位置 X
    float moveToPosX = 2;
    // 移動到位置 Y
    float moveToPosY = 3;
}

// 
// 用戶退場
///////////////////////////////////////////////////////////////////////
// 
// XXX 注意: 用戶退場不需要指令, 因為是在斷開服務器的時候執行
// 
// 結果
message UserQuitResult {
    // 退出用戶 Id
    uint32 quitUserId = 1;
}

//
// 用戶停駐
///////////////////////////////////////////////////////////////////////
// 指令
message UserStopCmd {
}

// 結果
message UserStopResult {
    // 停駐用戶 Id
    uint32 stopUserId = 1;
    // 停駐在位置 X
    float stopAtPosX = 2;
    // 停駐在位置 Y
    float stopAtPosY = 3;
}

//
// 用戶攻擊
///////////////////////////////////////////////////////////////////////
// 指令
message UserAttkCmd {
    // 目標用戶 Id
    uint32 targetUserId = 1;
}

// 結果
message UserAttkResult {
    // 發動攻擊的用戶 Id
    uint32 attkUserId = 1;
    // 目標用戶 Id
    uint32 targetUserId = 2;
}

// 用戶減血結果
message UserSubtractHpResult {
    // 目標用戶 Id
    uint32 targetUserId = 1;
    // 減血量
    uint32 subtractHp = 2;
}

// 死亡結果
message UserDieResult {
    // 目標用戶 Id
    uint32 targetUserId = 1;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM