Saving while it works xD

This commit is contained in:
2021-01-31 02:05:40 +02:00
parent 0780c1ed52
commit c7de79fbc4
19 changed files with 642 additions and 1898 deletions

1666
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -21,12 +21,14 @@
"cors": "^2.8.5",
"express": "^4.17.1",
"p5": "^1.2.0",
"socket.io": "^3.1.0"
"socket.io": "^3.1.0",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/express": "^4.17.11",
"@types/socket.io": "^2.1.13",
"@types/p5": "^0.9.1",
"@types/socket.io": "^2.1.13",
"@types/uuid": "^8.3.0",
"typescript": "^4.1.3"
}
}

18
server/src/Item.ts Normal file
View File

@@ -0,0 +1,18 @@
export default class Item {
constructor(
private _image: "healthPack",
private _type: "HEAL" | "DAMAGE" | "DEFENCE"
) {
}
get image() {
return this._image
}
get type() {
return this._type
}
}

27
server/src/Pickable.ts Normal file
View File

@@ -0,0 +1,27 @@
import Item from "./Item";
import { Position } from "./types";
export default class Pickable {
constructor(
private _id: string,
private _position: Position,
private _item: Item,
) {
}
get id() {
return this._id
}
get position() {
return this._position
}
get item() {
return this._item
}
}

View File

@@ -11,13 +11,6 @@ export default class Player {
private defence: number
private keysPressed: Map<number, boolean>
private KEY_W = 87
private KEY_A = 65
private KEY_S = 83
private KEY_D = 68
private MOVEMENT_SPEED = 1
constructor(private _socket: socketIO.Socket) {
this.name = 'Player#' + Math.floor(Math.random() * 10000)
this.position = {
@@ -27,7 +20,7 @@ export default class Player {
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
this.damage = 10
this.defence = 10
this.defence = 0
this.keysPressed = new Map<number, boolean>()
}
@@ -77,7 +70,10 @@ export default class Player {
color: this.color,
position: this.position,
id: this.socket.id,
health: this.health
health: this.health,
damage: this.damage,
defence: this.defence,
keysPressed: this.keysPressed
}
}
@@ -97,22 +93,4 @@ export default class Player {
}
}
move(delta: number) {
if (this.keysPressed.get(this.KEY_A)) {
this.position.x -= this.MOVEMENT_SPEED * delta
}
if (this.keysPressed.get(this.KEY_D)) {
this.position.x += this.MOVEMENT_SPEED * delta
}
if (this.keysPressed.get(this.KEY_W)) {
this.position.y -= this.MOVEMENT_SPEED * delta
}
if (this.keysPressed.get(this.KEY_S)) {
this.position.y += this.MOVEMENT_SPEED * delta
}
}
}

28
server/src/PlayerList.ts Normal file
View File

@@ -0,0 +1,28 @@
import Player from "./Player";
export default class PlayerList {
private players: Map<string, Player>
constructor() {
this.players = new Map<string, Player>()
}
getPlayer(id: string) {
return this.players.get(id)
}
getPlayers() {
return this.players
}
addPlayer(id: string, player: Player) {
if (this.players.has(id)) return
this.players.set(id, player)
}
removePlayer(id: string) {
this.players.delete(id)
}
}

View File

@@ -5,12 +5,19 @@ import cors from 'cors'
import path from 'path'
import Players from './Players'
import Player from './Player'
import PlayerList from './PlayerList'
import { Position } from './types'
import Pickable from './Pickable'
import Item from './Item'
import * as uuid from 'uuid'
const app = express()
const server = new http.Server(app)
const io = new socketIO.Server(server)
const players = new Players()
const items: Pickable[] = []
const playerList = new PlayerList()
app.use(cors())
@@ -41,114 +48,171 @@ io.on('connection', function(socket: socketIO.Socket) {
socket.on('greetings', (data: {name: string}) => {
let player = new Player(socket)
player.setName(data.name)
players.addPlayer(socket.id, player)
playerList.addPlayer(socket.id, player)
socket.emit('your-info', player.getPlayerDescription())
io.emit('new-player-joined', player.getPlayerDescription())
})
socket.on('get-other-players', () => {
let otherPlayers = []
for (let p of players.getPlayers()) {
if (socket.id !== p.getPlayerDescription().id) {
otherPlayers.push(p.getPlayerDescription())
}
playerList.getPlayers().forEach((player) => {
io.emit('new-player-joined', player.getPlayerDescription())
})
for (let item of items) {
io.emit('new-item', {id: item.id, position: item.position, item: {image: item.item.image, type: item.item.type}})
}
socket.emit('players-info', otherPlayers)
})
// socket.on('i-moved', (position: {x: number, y: number}) => {
// players.updatePlayerPosition(socket.id, {x: position.x, y: position.y})
// io.emit('player-moved', {id: socket.id, position: position})
// })
socket.on('disconnect', () => {
console.log(socket.id, 'disconnected')
players.removePlayer(socket.id)
console.log(socket.id, 'disconnected', (playerList.getPlayers().size - 1))
playerList.removePlayer(socket.id)
io.emit('player-disconnected', socket.id)
})
socket.on('shoot', (data: {position: {x: number, y: number}, direction: {x: number, y: number}}) => {
io.emit('shoot', {id: socket.id, position: data.position, direction: data.direction})
})
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})
})
// ITEM ACTIONS //
socket.on('item-heal', () => {
let p = players.getPlayer(socket.id)
if (p) {
p.player.setHealth(100)
io.emit('health-update', {id: p.player.socket.id, health: p.player.getHealth()})
}
})
socket.on('item-defence', () => {
let p = players.getPlayer(socket.id)
if (p) {
p.player.setDefence(p.player.getDefence() + 5)
}
})
socket.on('item-damage', () => {
let p = players.getPlayer(socket.id)
if (p) {
p.player.setDamage(p.player.getDamage() + 5)
}
})
socket.on('key-action', (data: {key: number, isDown: boolean}) => {
let p = players.getPlayer(socket.id)
if (p) {
p.player.keyAction(data.key, data.isDown)
}
socket.broadcast.emit('key-action', {id: socket.id, key: data.key, isDown: data.isDown})
})
socket.on('player-moved', (data: {position: Position}) => {
let player = playerList.getPlayer(socket.id)
if (player) {
player.setPosition(data.position.x, data.position.y)
}
socket.broadcast.emit('player-moved', {id: socket.id, position: data.position})
})
socket.on('hit-player', (data: {id: string, health: number}) => {
let player = playerList.getPlayer(data.id)
if (player) {
if (data.health <= 0) {
player.socket.disconnect()
return
}
player.setHealth(data.health)
}
socket.broadcast.emit('hit-player', {id: data.id, health: data.health})
})
socket.on('shoot', (data: {id: string, position: Position, direction: Position}) => {
socket.broadcast.emit('shoot', {id: socket.id, position: data.position, direction: data.direction})
})
socket.on('item-picked', (data: {id: string}) => {
socket.broadcast.emit('item-picked', {id: data.id})
})
// socket.on('shoot', (data: {position: {x: number, y: number}, direction: {x: number, y: number}}) => {
// console.log(socket.id, 'shoot')
// io.emit('shoot', {id: socket.id, position: data.position, direction: data.direction})
// })
// socket.on('hit-player', (data: {player: string, damage: number}) => {
// console.log(socket.id, 'hit-player')
// let hpLeft = players.takeDamage(data.player, data.damage)
// io.emit('health-update', {id: data.player, health: hpLeft})
// })
// // ITEM ACTIONS //
// socket.on('item-heal', () => {
// console.log(socket.id, 'item-heal')
// let p = players.getPlayer(socket.id)
// if (p) {
// p.player.setHealth(100)
// io.emit('health-update', {id: p.player.socket.id, health: p.player.getHealth()})
// }
// })
// socket.on('item-defence', () => {
// console.log(socket.id, 'item-defence')
// let p = players.getPlayer(socket.id)
// if (p) {
// p.player.setDefence(p.player.getDefence() + 5)
// }
// })
// socket.on('item-damage', () => {
// console.log(socket.id, 'item-damage')
// let p = players.getPlayer(socket.id)
// if (p) {
// p.player.setDamage(p.player.getDamage() + 5)
// }
// })
// socket.on('key-action', (data: {key: number, isDown: boolean}) => {
// console.log(socket.id, 'key-action')
// let p = playerList.getPlayer(socket.id)
// if (p) {
// p.keyAction(data.key, data.isDown)
// }
// })
});
let time = Date.now()
const getDeltaTime = () => {
let t = Date.now()
let dt = t - time
time = t
return dt
}
setInterval(() => {
// game tick
let delta = getDeltaTime()
for (let player of players.getPlayers()) {
if (player.getHealth() <= 0) {
player.socket.disconnect()
}
// player movement
let previosPosition = player.getPosition()
player.move(delta)
if (previosPosition.x !== player.getPosition().x || previosPosition.y !== player.getPosition().y) {
io.emit('player-moved', {id: player.socket.id, position: player.getPosition()})
}
// endof: player movement
const minx = -1600
const maxx = 1500
const miny = -2000
const maxy = 1900
if (items.length < 20) {
let item = new Item('healthPack', "HEAL")
let pickable = new Pickable(
uuid.v4(),
{
x: Math.floor(Math.random() * (Math.abs(minx) + maxx) + minx),
y: Math.floor(Math.random() * (Math.abs(miny) + maxy) + miny),
},
item
)
items.push(pickable)
io.emit('new-item', {id: pickable.id, position: pickable.position, item: {image: pickable.item.image, type: pickable.item.type}})
}
}, 15000)
}, 1000/60)
// let time = Date.now()
// const getDeltaTime = () => {
// let t = Date.now()
// let dt = t - time
// time = t
// return dt
// }
// setInterval(() => {
// // game tick
// let delta = getDeltaTime()
// playerList.getPlayers().forEach((player) => {
// if (player.getHealth() <= 0) {
// player.socket.disconnect()
// }
// // player movement
// let previosPosition = player.getPosition()
// player.move(delta)
// if (previosPosition.x !== player.getPosition().x || previosPosition.y !== player.getPosition().y) {
// io.emit('player-moved', {id: player.socket.id, position: player.getPosition()})
// }
// // endof: player movement
// })
// }, 1000/60)