Skip to content

Commit

Permalink
minor, Player refactoring, add soundjs as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed Feb 26, 2018
1 parent 97646a3 commit 9c34c7d
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 196 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<script type="text/javascript" src="lib/keyboard.js"></script>
<script type="text/javascript" src="lib/peer.min.js"></script>
<script type="text/javascript" src="lib/webgl-utils.js"></script>
<script type="text/javascript" src="lib/createjs.js"></script>
<script type="text/javascript" src="node_modules/soundjs/lib/soundjs.min.js"></script>
</head>

<body>
Expand Down
18 changes: 0 additions & 18 deletions lib/createjs.js

This file was deleted.

5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"homepage": "https://github.com/VadimDez/CS#readme",
"dependencies": {
"@types/gl-matrix": "^2.4.0",
"gl-matrix": "^2.4.0"
"gl-matrix": "^2.4.0",
"soundjs": "^1.0.1"
},
"devDependencies": {
"html-webpack-plugin": "^2.30.1",
Expand Down
3 changes: 2 additions & 1 deletion src/GameInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { mat4 } from 'gl-matrix';
// import * as glMatrix from '../lib/gl-matrix';
// const mat4 = glMatrix.mat4;
import { Map } from './Map';
import { Player } from './Player';

export type GameInfoType = { gl: any, player: any, map: Map, mvMatrix: mat4, pMatrix: mat4 };
export type GameInfoType = { gl: any, player: Player, map: Map, mvMatrix: mat4, pMatrix: mat4 };

export const GameInfo: GameInfoType = {
gl: null,
Expand Down
2 changes: 0 additions & 2 deletions src/ModelRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import * as glMatrix from '../lib/gl-matrix';
// const vec3 = glMatrix.vec3;
const mat3 = glMatrix.mat3;
// const mat4 = glMatrix.mat4;
// const createjs = require('../lib/createjs.js');
const createjs = (window as any).createjs;
import { GameInfo } from './GameInfo';

/**
Expand Down
233 changes: 122 additions & 111 deletions src/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,39 @@ import { config } from './config';
import { CollisionDetection } from './CollisionDetection';
import { Weapon } from './Weapon';

export const Player = function(gl, x, y, z) {
this.x = x;
this.y = y;
this.z = z;
this.yAngle = 0;
this.xAngle = 0;
this.speed = 5;
// X and Y direction. Not necessarily normalized
let dir = vec3.fromValues(0, 0, 0);
let weapon = null;

this.position = function() {
export class Player {
x: number;
y: number;
z: number;
yAngle = 0;
xAngle = 0;
speed = 5;
dir: vec3;
weapon: Weapon = null;

constructor(public gl, x, y, z) {
this.x = x;
this.y = y;
this.z = z;

// X and Y direction. Not necessarily normalized
this.dir = vec3.fromValues(0, 0, 0);
this.setupKey();
}

position() {
return [this.x, this.y, this.z];
};
}

this.move = function() {
move() {
let onGround = CollisionDetection.isOnGround(this.position());
let normalDir = vec3.fromValues(0, 0, 0);
vec3.normalize(normalDir, dir);
vec3.normalize(normalDir, this.dir);

// Move forward
let newX = this.x + this.speed * normalDir[0] * Math.cos(this.yAngle);
let newY = this.y - this.speed * normalDir[0] * Math.sin(this.yAngle);
let newZ = this.z + 18 * dir[2];
let newZ = this.z + 18 * this.dir[2];

// Strafe
newY -= this.speed * normalDir[1] * Math.cos(Math.PI - this.yAngle);
Expand All @@ -49,17 +58,17 @@ export const Player = function(gl, x, y, z) {
// Apply gravity if we're not on the ground. TODO: Accelerate instead of subtracting a constant
if (!onGround) {
newZ -= config.GRAVITY;
dir[2] = Math.max(0, dir[2] - 0.1);
this.dir[2] = Math.max(0, this.dir[2] - 0.1);
}

let newPosition = CollisionDetection.move([this.x, this.y, this.z + config.MAX_Z_CHANGE], [newX, newY, newZ]);

this.x = newPosition[0];
this.y = newPosition[1];
this.z = newPosition[2];
};
}

this.rotate = function(xDelta, yDelta) {
rotate(xDelta, yDelta) {
let PI_HALF = Math.PI / 2.0;
let PI_TWO = Math.PI * 2.0;

Expand All @@ -82,108 +91,110 @@ export const Player = function(gl, x, y, z) {
if (this.xAngle > PI_HALF) {
this.xAngle = PI_HALF;
}
};

this.render = function() {
return weapon.render();
};

this.switchWeapon = function(weaponName) {
weapon = new Weapon(weaponName);
};

MouseJS.on('left', function() {
weapon.shoot();
}, function() {
weapon.idle();
});

MouseJS.on('right', function() {
weapon.special();
}, function() {
weapon.idle();
});

KeyboardJS.on('r', function(event, keys, combo) {
weapon.reload();
}, function(event, keys, combo) {});

// Handle w and s keys
KeyboardJS.on('w,s', function(event, keys, combo) {
// Is w down?
if (combo === 'w') {
dir[0] = 1;
}
// Is s down?
if (combo === 's') {
dir[0] = -1;
}

// Are both keys down?
if (keys.indexOf('w') !== -1 && keys.indexOf('s') !== -1) {
dir[0] = 0;
}
}, function(event, keys, combo) {
// Did we release the w key?
if (combo === 'w') {
// Yep! Is s still being pressed?
if (keys.indexOf('s') === -1) {
// Nope. Stop movement
dir[0] = 0;
}

render() {
return this.weapon.render();
}

switchWeapon(weaponName) {
this.weapon = new Weapon(weaponName);
}

setupKey() {
MouseJS.on('left', () => {
this.weapon.shoot();
}, () => {
this.weapon.idle();
});

MouseJS.on('right', () => {
this.weapon.special();
}, () => {
this.weapon.idle();
});

KeyboardJS.on('r', (event, keys, combo) => {
this.weapon.reload();
}, (event, keys, combo) => {
});

// Handle w and s keys
KeyboardJS.on('w,s', (event, keys, combo) => {
// Is w down?
if (combo === 'w') {
this.dir[0] = 1;
}
else {
dir[0] = -1;
// Is s down?
if (combo === 's') {
this.dir[0] = -1;
}
}

// Symmetric to the case above
if (combo === 's') {
if (keys.indexOf('w') === -1) {
dir[0] = 0;
// Are both keys down?
if (keys.indexOf('w') !== -1 && keys.indexOf('s') !== -1) {
this.dir[0] = 0;
}
else {
dir[0] = 1;
}, (event, keys, combo) => {
// Did we release the w key?
if (combo === 'w') {
// Yep! Is s still being pressed?
if (keys.indexOf('s') === -1) {
// Nope. Stop movement
this.dir[0] = 0;
}
else {
this.dir[0] = -1;
}
}
}
});

// Handle a and d keys
// Symmetric to the handling of w and s
KeyboardJS.on('a,d', function(event, keys, combo) {
if (combo === 'a') {
dir[1] = 1;
}
if (combo === 'd') {
dir[1] = -1;
}
// Symmetric to the case above
if (combo === 's') {
if (keys.indexOf('w') === -1) {
this.dir[0] = 0;
}
else {
this.dir[0] = 1;
}
}
});

if (keys.indexOf('a') !== -1 && keys.indexOf('d') !== -1) {
dir[1] = 0;
}
}, function(event, keys, combo) {
if (combo === 'a') {
if (keys.indexOf('d') === -1) {
dir[1] = 0;
// Handle a and d keys
// Symmetric to the handling of w and s
KeyboardJS.on('a,d', (event, keys, combo) => {
if (combo === 'a') {
this.dir[1] = 1;
}
else {
dir[1] = -1;
if (combo === 'd') {
this.dir[1] = -1;
}
}

if (combo === 'd') {
if (keys.indexOf('a') === -1) {
dir[1] = 0;
if (keys.indexOf('a') !== -1 && keys.indexOf('d') !== -1) {
this.dir[1] = 0;
}
else {
dir[1] = 1;
}, (event, keys, combo) => {
if (combo === 'a') {
if (keys.indexOf('d') === -1) {
this.dir[1] = 0;
} else {
this.dir[1] = -1;
}
}
}
});

KeyboardJS.on('space', function(event, keys, combo) {
let d = dir[2];
if (d < 0.0001 && d > -0.0001) {
dir[2] = 1;
}
}, function(event, keys, combo) {});
};
if (combo === 'd') {
if (keys.indexOf('a') === -1) {
this.dir[1] = 0;
} else {
this.dir[1] = 1;
}
}
});

KeyboardJS.on('space', (event, keys, combo) => {
let d = this.dir[2];
if (d < 0.0001 && d > -0.0001) {
this.dir[2] = 1;
}
}, function (event, keys, combo) {
});
}
}
Loading

0 comments on commit 9c34c7d

Please sign in to comment.