Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/boxes backend #71

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions docs/api/box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Сообщения Box'a

## BoxPickUp
**Имя сообщения**: `boxPickUp`

**Broadcast**: `+`

**Структура**:
```
{
With: {
id: 1,
entityClass: 'box',
dimensions: 30
}
}
```

## BoxRespawn
**Имя сообщения**: `boxRespawn`

**Broadcast**: `+`

**Структура**:
```
{
With: {
id: 1,
entityClass: 'box',
dimensions: 30
},

// Position
Vector: {
x: 0,
y: 0
}
}
```
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ to be continued
- [Player](./docs/api/player.md)
- [Effect](./docs/api/effect.md)
- [Missile](./docs/api/missile.md)
- [Box](./docs/api/box.md)
57 changes: 57 additions & 0 deletions server/core/box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
let Entity = require('../core/entity');
let BoxMessages = require('../messages/box');

// Размер ящика
const BOX_SIZE = 30;
// Промежуток времени в сек., через который возможнен респавн ящика.
const BOX_RESPAWN_TIME_MIN = 25;
const BOX_RESPAWN_TIME_MAX = 35;

module.exports = class Box extends Entity {
constructor() {
super(BOX_SIZE);

this.__isDead = true;

let respTime = Math.random() * (BOX_RESPAWN_TIME_MAX - BOX_RESPAWN_TIME_MIN) + BOX_RESPAWN_TIME_MIN;

this.__reSpawnTimeout = setTimeout(() => {
this.reSpawn();
}, respTime * 1000);
}

move() {
// nothing
}

onDamaged() {
// nothing
}

onCollide(collidedWithEntity) {
this.onDeath(collidedWithEntity);
}

onDeath(killer){
super.onDeath(killer);

let respTime = Math.random() * (BOX_RESPAWN_TIME_MAX - BOX_RESPAWN_TIME_MIN) + BOX_RESPAWN_TIME_MIN;

this.__reSpawnTimeout = setTimeout(() => {
this.reSpawn();
}, respTime * 1000);

(new BoxMessages.BoxPickUp())
.withEntity(this)
.send();
}

reSpawn() {
super.reSpawn();

(new BoxMessages.BoxRespawn())
.withEntity(this)
.withVector(this.position)
.send();
}
};
13 changes: 13 additions & 0 deletions server/core/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,17 @@ module.exports = class Entity {
this.randomPosition();
this.__isDead = false;
}

/**
* Действия при лечении сущности
*
* @param {Number} hp - кол-во восстанавливаемого здоровья
*/
heal(hp){
if(hp + this.health < this.maxHealth) {
this.__health += hp;
} else {
this.__health = this.__maxHealth;
}
}
};
13 changes: 13 additions & 0 deletions server/core/health-box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let Box = require('./box');

module.exports = class HealthBox extends Box {
constructor() {
super();
}

onCollide(collidedWithEntity) {
super.onCollide(collidedWithEntity);

collidedWithEntity.heal(collidedWithEntity.maxHealth);
}
};
18 changes: 18 additions & 0 deletions server/messages/box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let Message = require('../core/message');

/**
* Сообщения для ящиков
*/
module.exports = {
BoxPickUp: class BoxPickUpMassage extends Message {
constructor() {
super('boxPickUp');
}
},

BoxRespawn: class BoxRespawnMessage extends Message {
constructor() {
super('boxRespawn');
}
}
};
5 changes: 5 additions & 0 deletions server/warlock-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let app = require('http').createServer(require('./core/static-handler'));
let io = require('socket.io')(app);
let World = require('./entities/world');
let PlayerMessages = require('./messages/player');
let HealthBox = require('./core/health-box');

class WarlockServer {
constructor(io) {
Expand Down Expand Up @@ -48,6 +49,10 @@ class WarlockServer {
let world = new World();
world.x = world.dimensions / 2;
world.y = world.dimensions / 2;

new HealthBox();
new HealthBox();
new HealthBox();
}

broadcast(event, data) {
Expand Down