Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 3a31b8d

Browse files
committed
Started on player health sync
1 parent b024039 commit 3a31b8d

6 files changed

Lines changed: 62 additions & 10 deletions

File tree

src/@types/crosscode/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ declare namespace ig {
6464
RhombusParticle: ig.EntityType;
6565
HiddenSkyBlock: ig.EntityType;
6666
Enemy: ig.EntityType;
67+
Effect: ig.EntityType;
68+
Particle: ig.EntityType;
69+
CopyParticle: ig.EntityType;
6770
}
6871
interface TargetableEntity extends Entity {
6972

src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface IConnection {
2121

2222
updateEntityPosition(id: number, pos: ig.Vector3): void;
2323
updateEntityAnimation(id: number, face: ig.Vector2, anim: string): void;
24-
updateEntityHealth(id: number, health: number): void;
24+
updateEntityHealth(id: number | null, health: number): void;
2525
updateEntityState(id: number, state: string): void;
2626
updateEntityTarget(id: number, target: string | number | null): void;
2727

@@ -53,5 +53,5 @@ export interface IConnection {
5353
onUpdateEntityTarget(callback:
5454
(id: number, target: string | number | null) => void): void;
5555
onUpdateEntityHealth(callback:
56-
(id: number, health: number) => void): void;
56+
(id: number | string, health: number) => void): void;
5757
}

src/connectors/SocketIOConnector.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class SocketIoConnector implements IConnection {
132132
public updateEntityAnimation(id: number, face: ig.Vector2, anim: string): void {
133133
this.socket.emit('updateEntityAnimation', {id, face, anim});
134134
}
135-
public updateEntityHealth(id: number, health: number): void {
135+
public updateEntityHealth(id: number | null, health: number): void {
136136
this.socket.emit('updateEntityHealth', {id, hp: health});
137137
}
138138
public updateEntityState(id: number, state: string): void {
@@ -205,7 +205,7 @@ export class SocketIoConnector implements IConnection {
205205
callback(data.id, data.target);
206206
});
207207
}
208-
public onUpdateEntityHealth(callback: (id: number, health: number) => void): void {
208+
public onUpdateEntityHealth(callback: (id: number | string, health: number) => void): void {
209209
this.socket.on('updateEntityHealth', (data: any) => {
210210
callback(data.id, data.hp);
211211
});

src/listeners/connection/onUpdateEntityHealth.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IMultiplayerEntity } from '../../mpEntity';
12
import { Multiplayer } from '../../multiplayer';
23

34
export class OnUpdateEntityHealthListener {
@@ -9,13 +10,28 @@ export class OnUpdateEntityHealthListener {
910
this.main.connection.onUpdateEntityHealth(this.onUpdateEntityHealth.bind(this));
1011
}
1112

12-
public onUpdateEntityHealth(id: number, health: number): void {
13-
if (!this.main.entities[id]) {
13+
public onUpdateEntityHealth(id: number | string, health: number): void {
14+
const entity = this.getEntity(id);
15+
16+
if (!entity) {
1417
return;
1518
}
1619

1720
console.log('[multiplayer] Set ' + id + '\'s health to ' + health);
1821

19-
simplify.setCurrentHp(this.main.entities[id], health);
22+
simplify.setCurrentHp(entity, health);
23+
}
24+
25+
private getEntity(id: number | string): IMultiplayerEntity | null {
26+
if (typeof id === 'number') {
27+
return this.main.entities[id];
28+
}
29+
30+
const player = this.main.players[id];
31+
if (player) {
32+
return player.entity;
33+
}
34+
35+
return null;
2036
}
2137
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { IMultiplayerEntity } from '../../mpEntity';
2+
import { Multiplayer } from '../../multiplayer';
3+
import { EntityListener } from './entityListener';
4+
import { PlayerListener } from './playerListener';
5+
6+
export class OnPlayerHealthChangeListener {
7+
private last = 0;
8+
9+
constructor(
10+
private main: Multiplayer,
11+
) { }
12+
13+
public register(playerListener: PlayerListener): void {
14+
const instance = this;
15+
playerListener.addChild((player: ig.Player) => {
16+
instance.onUpdate(player);
17+
});
18+
}
19+
20+
public onPlayerHealthChanged(health: number): void {
21+
this.main.connection.updateEntityHealth(null, health);
22+
}
23+
24+
private onUpdate(player: ig.Player): void {
25+
const health = simplify.getCurrentHp(player);
26+
27+
if (health !== this.last) {
28+
this.onPlayerHealthChanged(health);
29+
this.last = health;
30+
}
31+
}
32+
}

src/multiplayer.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { PlayerListener } from './listeners/game/playerListener';
3030
import { LoadScreenHook } from './loadScreenHook';
3131
import { IMultiplayerEntity } from './mpEntity';
3232
import { IPlayer } from './player';
33+
import { OnPlayerHealthChangeListener } from './listeners/game/onPlayerHealthChange';
3334

3435
export class Multiplayer {
3536
public futureEntities: IEntityDefinition[] = [];
@@ -43,7 +44,6 @@ export class Multiplayer {
4344
public entities: IMultiplayerEntity[] = [];
4445

4546
private loadScreen!: () => void;
46-
private startGame!: () => void;
4747
private nextEID = 1;
4848
private entitySpawnListener!: OnEntitySpawnListener;
4949
private loadScreenHook = new LoadScreenHook();
@@ -178,8 +178,7 @@ export class Multiplayer {
178178
// buttons.splice(buttonNumber, 2);
179179
// buttons[2].a.g.y = 80;
180180
buttons[buttonNumber][cc.ig.GUI.renameTextButton]('Connect', true);
181-
this.startGame = buttons[0][cc.ig.GUI.callbackFunction];
182-
this.loadScreen = buttons[2][cc.ig.GUI.callbackFunction];
181+
this.loadScreen = buttons[buttonNumber][cc.ig.GUI.callbackFunction];
183182
buttons[buttonNumber][cc.ig.GUI.callbackFunction] = this.startConnect.bind(this);
184183
}
185184

@@ -192,13 +191,15 @@ export class Multiplayer {
192191

193192
const playerMove = new OnPlayerMoveListener(this);
194193
const playerAnimation = new OnPlayerAnimationListener(this);
194+
const playerHealth = new OnPlayerHealthChangeListener(this);
195195
const entityMove = new OnEntityMoveListener(this);
196196
const entityAnimation = new OnEntityAnimationListener(this);
197197
const entityHealthChange = new OnEntityHealthChangeListener(this);
198198
const entityTargetChange = new OnEntityTargetChangeListener(this);
199199

200200
playerMove.register(playerListener);
201201
playerAnimation.register(playerListener);
202+
playerHealth.register(playerListener);
202203
entityMove.register(entityListener);
203204
entityAnimation.register(entityListener);
204205
entityHealthChange.register(entityListener);

0 commit comments

Comments
 (0)