Skip to content

Commit 0262e0c

Browse files
committed
Finished basic implementation of game server (still required a lot of optimizations and refactoring)
1 parent 025053e commit 0262e0c

File tree

10 files changed

+282
-63
lines changed

10 files changed

+282
-63
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Marcin Polak
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Battle City React Server
2+
==============
3+
This is the sample server for game that was built while learning React.
4+
5+
Game:
6+
https://github.com/coder-pm/battle-city-react
7+
8+
Released under MIT license.

src/game/enums/NetworkPacket.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export enum NetworkPacket {
77
BOARD_STATE_OBSTACLES = 'board.state.obstacles',
88
BOARD_STATE_TANKS = 'board.state.tanks',
99
BOARD_STATE_MISSILES = 'board.state.missiles',
10-
TANK_EVENT_KEYBOARD = 'tank.event.keyboard'
10+
TANK_EVENT_KEYBOARD = 'tank.event.keyboard',
11+
TANK_EVENT_FIRE = 'tank.event.fire'
1112
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import MissileMove from "../models/move/MissileMove";
2+
import {MISSILE_HEIGHT, MISSILE_MOVE_STEP, MISSILE_WIDTH} from "../../constants";
3+
import {Collision} from "../enums/Collision";
4+
import MissileModel from "../models/components/MissileModel";
5+
import World from "../classes/World";
6+
import Point from "../models/Point";
7+
8+
/**
9+
* Missile move handler.
10+
*/
11+
export const MISSILE_MOVE_HANDLER = (missile: MissileModel, world: World, direction: number, axis: string): MissileMove => {
12+
let move: MissileMove = {
13+
location: missile.location,
14+
hitObjects: []
15+
};
16+
17+
// calculate new coordinates and check if missile may move there
18+
const step = direction * MISSILE_MOVE_STEP;
19+
const newCoords = MISSILE_NEXT_COORDINATES(axis, step, missile.location);
20+
const hitObjects = world.isIntersecting({
21+
id: missile.id,
22+
location: {
23+
x: newCoords.x,
24+
y: newCoords.y
25+
},
26+
dimension: {
27+
width: MISSILE_WIDTH,
28+
height: MISSILE_HEIGHT
29+
}
30+
},
31+
Collision.BLOCK_SHOT
32+
);
33+
34+
// handle missile fell if it hits other object or continue moving
35+
if (hitObjects.length > 0) {
36+
move.hitObjects = hitObjects;
37+
} else {
38+
move.location = newCoords;
39+
world.updateObject(missile.id, move.location);
40+
}
41+
return move;
42+
};
43+
44+
/**
45+
* Calculate missile next coordinates.
46+
*
47+
* @param axis - axis (x or y)
48+
* @param step - how far missile will move
49+
* @param location - initial position
50+
*/
51+
export const MISSILE_NEXT_COORDINATES = (axis: string, step: number, location: Point): Point => {
52+
return {
53+
x: axis === 'x' ? (location.x + step) : location.x,
54+
y: axis === 'y' ? (location.y + step) : location.y
55+
};
56+
};
57+
58+
/**
59+
* Missile direction map.
60+
*/
61+
export const MISSILE_DIRECTION_MAP: { [index: number]: string } = {
62+
0: '-y',
63+
90: '+x',
64+
180: '+y',
65+
270: '-x'
66+
};

src/game/handlers/TankMoveHandler.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Collision} from "../enums/Collision";
33
import {TankActor} from "../enums/TankActor";
44
import World from "../classes/World";
55
import TankModel from "../models/components/TankModel";
6-
import Move from "../models/Move";
6+
import TankMove from "../models/move/TankMove";
77

88
/**
99
* Tank move handler.
@@ -13,16 +13,16 @@ import Move from "../models/Move";
1313
* @param isStuck - stuck state
1414
* @param activeKey - active key
1515
*/
16-
export const TANK_MOVE_HANDLER = (tank: TankModel, world: World, isStuck: boolean, activeKey: string): Move => {
17-
let move = {
16+
export const TANK_MOVE_HANDLER = (tank: TankModel, world: World, isStuck: boolean, activeKey: string): TankMove => {
17+
let move: TankMove = {
1818
location: tank.location,
1919
rotation: tank.rotation,
2020
isStuck: isStuck
2121
};
2222
if (activeKey) {
23-
let x = tank.location.x;
24-
let y = tank.location.y;
25-
let r = tank.rotation;
23+
let x = move.location.x;
24+
let y = move.location.y;
25+
let r = move.rotation;
2626
let initialDirection = r;
2727
let correctionAxis = 'x';
2828
switch (activeKey) {
@@ -73,7 +73,7 @@ export const TANK_MOVE_HANDLER = (tank: TankModel, world: World, isStuck: boolea
7373
move.location = {x: x, y: y};
7474
move.rotation = r;
7575
move.isStuck = false;
76-
world.updateObject(tank.id, tank.location);
76+
world.updateObject(tank.id, move.location);
7777
} else {
7878
move.isStuck = true;
7979
// just rotate in case of intersection

src/game/models/Move.ts

-10
This file was deleted.

src/game/models/components/MissileModel.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ import Structure from "../Structure";
66
export default interface MissileModel extends Structure {
77
tankId: string;
88
rotation: number;
9+
direction: number;
10+
axis: string;
911
}

src/game/models/move/MissileMove.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Point from "../Point";
2+
import Structure from "../Structure";
3+
4+
/**
5+
* Interface MissileMove - missile move model.
6+
*/
7+
export default interface MissileMove {
8+
location: Point;
9+
hitObjects: Array<Structure>
10+
}

src/game/models/move/TankMove.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Point from "../Point";
2+
3+
/**
4+
* Interface TankMove - tank move model.
5+
*/
6+
export default interface TankMove {
7+
location: Point;
8+
rotation: number;
9+
isStuck: boolean;
10+
}

0 commit comments

Comments
 (0)