Changed render engine, fixed health bug display bug, lowered rendering range
This commit is contained in:
51
server/src/Player.ts
Normal file
51
server/src/Player.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import socketIO from 'socket.io'
|
||||
|
||||
export default class Player {
|
||||
|
||||
private name: string
|
||||
private position: {x: number, y: number}
|
||||
private color: string
|
||||
private health: number
|
||||
|
||||
constructor(private _socket: socketIO.Socket) {
|
||||
this.name = 'Player#' + Math.floor(Math.random() * 10000)
|
||||
this.position = {
|
||||
x: Math.random()*1000 - 500,
|
||||
y: Math.random()*1000 - 500,
|
||||
}
|
||||
this.color = '#' + (Math.floor(Math.random() * 256)).toString(16) + (Math.floor(Math.random() * 256)).toString(16) + (Math.floor(Math.random() * 256)).toString(16)
|
||||
this.health = Math.floor(Math.random() * 50) + 50
|
||||
}
|
||||
|
||||
get socket() {
|
||||
return this._socket
|
||||
}
|
||||
|
||||
getHealth() {
|
||||
return this.health
|
||||
}
|
||||
|
||||
takeDamage(damage: number) {
|
||||
this.health -= damage
|
||||
}
|
||||
|
||||
getPlayerDescription() {
|
||||
return {
|
||||
name: this.name,
|
||||
color: this.color,
|
||||
position: this.position,
|
||||
id: this.socket.id,
|
||||
health: this.health
|
||||
}
|
||||
}
|
||||
|
||||
setHealth(hp: number) {
|
||||
this.health = hp
|
||||
}
|
||||
|
||||
setPosition(x: number, y: number) {
|
||||
this.position.x = x
|
||||
this.position.y = y
|
||||
}
|
||||
|
||||
}
|
||||
57
server/src/Players.ts
Normal file
57
server/src/Players.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import Player from './Player'
|
||||
|
||||
export default class Players {
|
||||
|
||||
private players: {
|
||||
socketID: string,
|
||||
player: Player
|
||||
}[]
|
||||
|
||||
constructor() {
|
||||
this.players = []
|
||||
}
|
||||
|
||||
addPlayer(id: string, player: Player) {
|
||||
this.players.push({
|
||||
socketID: id,
|
||||
player: player
|
||||
})
|
||||
}
|
||||
|
||||
removePlayer(id: string) {
|
||||
this.players = this.players.filter((player) => {
|
||||
return player.socketID !== id
|
||||
})
|
||||
}
|
||||
|
||||
takeDamage(id: string, damage: number) {
|
||||
for (let player of this.players) {
|
||||
if (player.socketID === id) {
|
||||
player.player.takeDamage(damage)
|
||||
if (player.player.getHealth() <= 0) {
|
||||
player.player.socket.emit('ded')
|
||||
player.player.socket.disconnect()
|
||||
return player.player.getHealth()
|
||||
}
|
||||
return player.player.getHealth()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getPlayers(): Player[] {
|
||||
let playerList = []
|
||||
for (let player of this.players) {
|
||||
playerList.push(player.player)
|
||||
}
|
||||
return playerList
|
||||
}
|
||||
|
||||
updatePlayerPosition(id: string, position: {x: number, y: number}) {
|
||||
for (let player of this.players) {
|
||||
if (player.socketID === id) {
|
||||
player.player.setPosition(position.x, position.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,100 +4,13 @@ import socketIO from 'socket.io'
|
||||
import fs from 'fs'
|
||||
import cors from 'cors'
|
||||
import path from 'path'
|
||||
import Players from './Players'
|
||||
import Player from './Player'
|
||||
|
||||
const app = express()
|
||||
const server = new http.Server(app)
|
||||
const io = new socketIO.Server(server)
|
||||
|
||||
class Players {
|
||||
|
||||
private players: {
|
||||
socketID: string,
|
||||
player: Player
|
||||
}[]
|
||||
|
||||
constructor() {
|
||||
this.players = []
|
||||
}
|
||||
|
||||
addPlayer(id: string, player: Player) {
|
||||
this.players.push({
|
||||
socketID: id,
|
||||
player: player
|
||||
})
|
||||
}
|
||||
|
||||
removePlayer(id: string) {
|
||||
this.players = this.players.filter((player) => {
|
||||
return player.socketID !== id
|
||||
})
|
||||
}
|
||||
|
||||
updatePlayerHealth(id: string, health: number) {
|
||||
for (let player of this.players) {
|
||||
if (player.socketID === id) {
|
||||
player.player.setHealth(health)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getPlayers(): Player[] {
|
||||
let playerList = []
|
||||
for (let player of this.players) {
|
||||
playerList.push(player.player)
|
||||
}
|
||||
return playerList
|
||||
}
|
||||
|
||||
updatePlayerPosition(id: string, position: {x: number, y: number}) {
|
||||
for (let player of this.players) {
|
||||
if (player.socketID === id) {
|
||||
player.player.setPosition(position.x, position.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Player {
|
||||
|
||||
private name: string
|
||||
private position: {x: number, y: number}
|
||||
private color: string
|
||||
private health: number
|
||||
|
||||
constructor(private socket: socketIO.Socket) {
|
||||
this.name = 'Player#' + Math.floor(Math.random() * 10000)
|
||||
this.position = {
|
||||
x: Math.random()*500 - 250,
|
||||
y: Math.random()*500 - 250,
|
||||
}
|
||||
this.color = '#' + (Math.floor(Math.random() * 256)).toString(16) + (Math.floor(Math.random() * 256)).toString(16) + (Math.floor(Math.random() * 256)).toString(16)
|
||||
this.health = 100
|
||||
}
|
||||
|
||||
getPlayerDescription() {
|
||||
return {
|
||||
name: this.name,
|
||||
color: this.color,
|
||||
position: this.position,
|
||||
id: this.socket.id,
|
||||
health: this.health
|
||||
}
|
||||
}
|
||||
|
||||
setHealth(hp: number) {
|
||||
this.health = hp
|
||||
}
|
||||
|
||||
setPosition(x: number, y: number) {
|
||||
this.position.x = x
|
||||
this.position.y = y
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const players = new Players()
|
||||
|
||||
app.use(cors())
|
||||
@@ -151,9 +64,9 @@ io.on('connection', function(socket: socketIO.Socket) {
|
||||
io.emit('shoot', {id: socket.id, position: data.position, direction: data.direction})
|
||||
})
|
||||
|
||||
socket.on('health-update', (data: {player: string, health: number}) => {
|
||||
players.updatePlayerHealth(data.player, data.health)
|
||||
io.emit('health-update', {id: data.player, health: data.health})
|
||||
socket.on('hit-player', (data: {player: string, damage: number}) => {
|
||||
let hpLeft = players.takeDamage(data.player, data.damage)
|
||||
io.emit('health-update', {id: data.player, health: hpLeft})
|
||||
})
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user