1
0
Fork 0
mirror of https://github.com/demostf/demo.js synced 2026-06-04 00:54:14 +02:00

extract activeWeapon

This commit is contained in:
Robin Appelman 2017-02-22 23:50:30 +01:00
commit eebc2e15cd
2 changed files with 33 additions and 24 deletions

View file

@ -1,6 +1,8 @@
import {UserInfo} from "./UserInfo";
import {Vector} from "./Vector";
import {PlayerCondition} from "./PlayerCondition";
import {Weapon} from "./Weapon";
import {Match} from "./Match";
export enum LifeState {
ALIVE = 0,
@ -9,15 +11,26 @@ export enum LifeState {
RESPAWNABLE = 3
}
export interface Player {
export class Player {
match: Match;
user: UserInfo;
position: Vector;
health: number;
maxHealth: number;
classId: number;
team: number;
viewAngle: number;
weapons: number[];
ammo: number[];
lifeState: LifeState
position: Vector = new Vector(0, 0, 0);
health: number = 0;
maxHealth: number = 0;
classId: number = 0;
team: number = 0;
viewAngle: number = 0;
weaponIds: number[] = [];
ammo: number[] = [];
lifeState: LifeState = LifeState.DEATH;
activeWeapon: number = 0;
constructor(match: Match, userInfo: UserInfo) {
this.match = match;
this.user = userInfo;
}
get weapons(): Weapon[] {
return this.weaponIds.map(id => this.match.weaponMap[this.match.outerMap[id]]);
}
}

View file

@ -74,22 +74,12 @@ function handleEntity(entity: PacketEntity, match: Match) {
"DT_TFPlayerSharedLocal.m_nDesiredDisguiseTeam": 0,
"DT_TFPlayerSharedLocal.m_nDesiredDisguiseClass": 0,
"DT_TFPlayerShared.m_iKillStreak": 0,
"DT_BaseCombatCharacter.m_hActiveWeapon": 2097151,
"DT_TFPlayerShared.m_flCloakMeter": 100,
*/
const player: Player = (match.playerMap[entity.entityIndex]) ? match.playerMap[entity.entityIndex] : {
user: match.getUserInfoForEntity(entity),
position: new Vector(0, 0, 0),
maxHealth: 0,
health: 0,
classId: 0,
team: 0,
viewAngle: 0,
weapons: [],
ammo: [],
lifeState: LifeState.DEATH
};
const player: Player = (match.playerMap[entity.entityIndex]) ?
match.playerMap[entity.entityIndex] :
new Player(match, match.getUserInfoForEntity(entity));
if (!match.playerMap[entity.entityIndex]) {
match.playerMap[entity.entityIndex] = player;
match.players.push(player);
@ -98,7 +88,7 @@ function handleEntity(entity: PacketEntity, match: Match) {
for (const prop of entity.props) {
if (prop.definition.ownerTableName === 'm_hMyWeapons') {
if (prop.value !== 2097151) {
player.weapons[parseInt(prop.definition.name, 10)] = <number>prop.value;
player.weaponIds[parseInt(prop.definition.name, 10)] = <number>prop.value;
}
}
if (prop.definition.ownerTableName === 'm_iAmmo') {
@ -137,6 +127,12 @@ function handleEntity(entity: PacketEntity, match: Match) {
case 'DT_BasePlayer.m_lifeState':
player.lifeState = <number>prop.value;
break;
case 'DT_BaseCombatCharacter.m_hActiveWeapon':
for (let i = 0; i < player.weapons.length; i++) {
if (player.weaponIds[i] === prop.value) {
player.activeWeapon = i;
}
}
}
}
break;