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

feat: add emotes support #11

Merged
merged 10 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/src/emote.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import $ from "jquery";
import { GameConfig } from "../../shared/gameConfig";
import * as PIXI from "pixi.js";
import { coldet } from "../../shared/utils/coldet";
import { util } from "../../shared/utils/util";
import { v2 } from "../../shared/utils/v2";
import { math } from "../../shared/utils/math";
import { device } from "./device";
Expand Down
2 changes: 2 additions & 0 deletions client/src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export class Game {
joinMessage.proxy = false; //* !/.*surviv\.io$/.test(window.location.hostname);
joinMessage.otherProxy = false; //* !proxy.authLocation();
joinMessage.bot = false;
joinMessage.emotes = _this.config.get("loadout").emotes;

_this.sendMessage(net.MsgType.Join, joinMessage, 8192);
};
this.ws.onmessage = function(e) {
Expand Down
47 changes: 46 additions & 1 deletion server/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@ import { GameConfig } from "../../shared/gameConfig";
import net from "../../shared/net";
import { type Explosion } from "./objects/explosion";
import { type Msg } from "../../shared/netTypings";

import { EmotesDefs } from "../../shared/defs/gameObjects/emoteDefs";

export class Emote {
playerId: number;
position: Vec2;
type: string;
isPing: boolean;

constructor(playerId: number, position: Vec2, type: string, isPing: boolean) {
this.playerId = playerId;
this.position = position;
this.type = type;
this.isPing = isPing;
}
}
export class Game {
stopped = false;
allowJoin = true;
Expand Down Expand Up @@ -68,6 +82,8 @@ export class Game {

logger: Logger;

emotes = new Set<Emote>();

constructor(id: number, config: ConfigType) {
this.id = id;
this.logger = new Logger(`Game #${this.id}`);
Expand Down Expand Up @@ -107,8 +123,17 @@ export class Game {
// this.serializationCache.update(this);

for (const player of this.connectedPlayers) {
if (this.emotes.size) {
for (const emote of this.emotes) {
if (emote.playerId === player.id || !emote.isPing) {
player.emotes.add(emote);
}
}
}

player.sendMsgs();
}
if (this.emotes.size) this.emotes = new Set<Emote>();
lmssiehdev marked this conversation as resolved.
Show resolved Hide resolved

//
// reset stuff
Expand Down Expand Up @@ -217,6 +242,20 @@ export class Game {
player.name = name;
player.joinedTime = Date.now();

const emotes = joinMsg.emotes;
for (let i = 0; i < emotes.length; i++) {
const emote = emotes[i];
if ((i < 4 && emote === "")) {
player.loadout.emotes.push("emote_logoswine");
continue;
}

if (EmotesDefs[emote as keyof typeof EmotesDefs] === undefined &&
emote != "") {
player.loadout.emotes.push("emote_logoswine");
} else player.loadout.emotes.push(emote);
}

this.newPlayers.push(player);
this.grid.addObject(player);
this.connectedPlayers.add(player);
Expand All @@ -225,6 +264,12 @@ export class Game {
this.aliveCountDirty = true;
break;
}
case net.MsgType.Emote: {
const emoteMsg = new net.EmoteMsg();
emoteMsg.deserialize(stream);

this.emotes.add(new Emote(player.id, emoteMsg.pos, emoteMsg.type, emoteMsg.isPing));
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions server/src/objects/player.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type WebSocket } from "uWebSockets.js";
import { type Game } from "../game";
import { type Emote, type Game } from "../game";
import { GameConfig } from "../../../shared/gameConfig";
import { collider } from "../../../shared/utils/collider";
import { type Vec2, v2 } from "../../../shared/utils/v2";
Expand Down Expand Up @@ -201,9 +201,11 @@ export class Player extends BaseGameObject {
loadout = {
heal: "heal_basic",
boost: "boost_basic",
emotes: [...GameConfig.defaultEmoteLoadout]
emotes: [] as string[]
};

emotes = new Set<Emote>();

damageTaken = 0;
damageDealt = 0;
joinedTime = 0;
Expand Down Expand Up @@ -418,6 +420,11 @@ export class Player extends BaseGameObject {
updateMsg.activePlayerData = this;
updateMsg.playerInfos = this._firstUpdate ? [...this.game.players] : this.game.newPlayers;

for (const emote of this.emotes) {
updateMsg.emotes.push(emote);
}

if (this.emotes.size) this.emotes = new Set();
let newBullets = [];
const extendedRadius = 1.1 * radius;
const radiusSquared = extendedRadius * extendedRadius;
Expand Down
26 changes: 22 additions & 4 deletions shared/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ class JoinMsg {
* @type {boolean}
*/
this.bot = false;
/**
* @type {string[]}
*/
this.emotes = [];
}

/**
Expand All @@ -904,7 +908,13 @@ class JoinMsg {
this.proxy = s.readBoolean();
this.otherProxy = s.readBoolean();
this.bot = s.readBoolean();
s.readAlignToNextByte();
lmssiehdev marked this conversation as resolved.
Show resolved Hide resolved
this.emotes = [];
const count = s.readUint8();

for (let i = 0; i < count; i++) {
const emote = s.readGameType();
this.emotes.push(emote);
}
}

/**
Expand All @@ -921,6 +931,11 @@ class JoinMsg {
s.writeBoolean(this.proxy);
s.writeBoolean(this.otherProxy);
s.writeBoolean(this.bot);

s.writeUint8(this.emotes.length);
for (const emote of this.emotes) {
s.writeGameType(emote);
}
s.writeAlignToNextByte();
}
}
Expand Down Expand Up @@ -1140,7 +1155,7 @@ class EmoteMsg {
this.pos = s.readVec(0, 0, 1024, 1024, 16);
this.type = s.readGameType();
this.isPing = s.readBoolean();
s.readBits(6);
lmssiehdev marked this conversation as resolved.
Show resolved Hide resolved
s.readBits(5);
}
}

Expand All @@ -1163,7 +1178,6 @@ class JoinedMsg {
s.writeUint8(this.teamMode);
s.writeUint16(this.playerId);
s.writeBoolean(this.started);

s.writeUint8(this.emotes.length);
for (const emote of this.emotes) {
s.writeGameType(emote);
Expand All @@ -1177,7 +1191,8 @@ class JoinedMsg {
this.teamMode = s.readUint8();
this.playerId = s.readUint16();
this.started = s.readBoolean();
for (let count = s.readUint8(), i = 0; i < count; i++) {
const count = s.readUint8();
for (let i = 0; i < count; i++) {
const emote = s.readGameType();
this.emotes.push(emote);
}
Expand Down Expand Up @@ -1612,6 +1627,9 @@ class UpdateMsg {
this.groupStatusDirty = false;
this.bullets = [];
this.explosions = [];
/**
* @type {Emote[]}
*/
this.emotes = [];
this.planes = [];
this.airstrikeZones = [];
Expand Down
Loading