Skip to content

Commit

Permalink
fixed codestyle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
a.gutnikov committed Nov 14, 2016
1 parent 7e28d2e commit 758ec6b
Show file tree
Hide file tree
Showing 29 changed files with 2,615 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules
.idea
.project
npm-debug.log
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# webrtc-shooter
A short experiment on making peer to peer game with webrtc and impactjs

# how to test it

1. In cloned repo directory: npm i
2. npm run signalling
3. npm run http
4. visit http://localhost:8080
315 changes: 315 additions & 0 deletions game.min.js

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<title>Impact Game</title>
<style type="text/css">
html,body {
background-color: #333;
color: #fff;
font-family: helvetica, arial, sans-serif;
margin: 0;
padding: 0;
font-size: 12pt;
}

#canvas {
width: 480px;
height: 320px;
border: 1px solid #3b3d42;
image-rendering: optimizeSpeed;
-webkit-interpolation-mode: nearest-neighbor;
}

#noCanvas {
padding: 90px 32px;
font-weight: bold;
border: 1px solid #3b3d42;
background-color: #000;
}
</style>

<script type="text/javascript">
if (!window.location.search) {
var roomName = prompt('Enter room name to enter, or leave blank for random');
window.location.search = roomName || Math.random().toString(16).slice(-10);
}
var roomName = window.location.search;
</script>
<script type="text/javascript" src="lib/net/socket.io.js"></script>
<script type="text/javascript" src="lib/net/adapter.js"></script>
<script type="text/javascript" src="lib/messages.js"></script>
<script type="text/javascript" src="game.min.js"></script>
</head>
<body>
<canvas id="canvas"></canvas>
<div>
0. Use arrows to move <br/>
1. X - jump<br/>
2. C - shoot<br/>
3. Collect tubes to recharge weapon<br/>
4. Be careful of acid burns!<br/>
5. Invite your friends by sending them a link to page url<br/>
</div>
</body>
</html>
305 changes: 305 additions & 0 deletions lib/game/entities/player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
// jscs:disable validateIndentation
ig.module(
'game.entities.player'
).requires(
'impact.entity',
'game.entities.particle'
).defines(function() {
EntityPlayer = ig.Entity.extend({
MAX_WEAPONS: 8,
size: {
x: 8,
y: 14
},
offset: {
x: 4,
y: 2
},
maxVel: {
x: 60,
y: 240
},
accelDef: {
ground: 400,
air: 200
},
frictionDef: {
ground: 400,
air: 100
},
jump: 120,
bounciness: 0,
health: 30,
type: ig.Entity.TYPE.A,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.PASSIVE,
flip: false,
flippedAnimOffset: 24,
idle: false,
moved: false,
wasStanding: false,
canHighJump: false,
highJumpTimer: null,
idleTimer: null,
weaponsLeft: 3,
// sfxPlasma: new ig.Sound('media/sounds/plasma.ogg'),
// sfxDie: new ig.Sound('media/sounds/die-respawn.ogg', false),
animSheet: null,
init: function(x, y, settings) {
this.animSheet = new ig.AnimationSheet('media/sprites/player-red.png', 16, 16),
this.friction.y = 0;
this.parent(x, y, settings);
this.idleTimer = new ig.Timer();
this.highJumpTimer = new ig.Timer();
this.addAnim('idle', 1, [0]);
this.addAnim('scratch', 0.3, [2, 1, 2, 1, 2], true);
this.addAnim('shrug', 0.3, [3, 3, 3, 3, 3, 3, 4, 3, 3], true);
this.addAnim('run', 0.07, [6, 7, 8, 9, 10, 11]);
this.addAnim('jump', 1, [15]);
this.addAnim('fall', 0.4, [12, 13]);
this.addAnim('land', 0.15, [14]);
this.addAnim('die', 0.07, [18, 19, 20, 21, 22, 23, 16, 16, 16]);
this.addAnim('spawn', 0.07, [16, 16, 16, 23, 22, 21, 20, 19, 18]);
},
update: function() {
// If spawns - wait for spawn animation finished
// then goto Idle
if (this.currentAnim == this.anims.spawn) {
this.currentAnim.update();
if (this.currentAnim.loopCount) {
this.currentAnim = this.anims.idle.rewind();
} else {
return;
}
}
// Same for Die, but at the end of animation
// do die ))
if (this.currentAnim == this.anims.die) {
this.currentAnim.update();
if (this.currentAnim.loopCount) {
this.kill();
}
return;
}
this.moved = false;
this.friction.x = this.standing ? this.frictionDef.ground : this.frictionDef.air;

// left or right button is pressed ( or holding )
// set x-axis acceleration
if (ig.input.state('left')) {
this.accel.x = -(this.standing ? this.accelDef.ground : this.accelDef.air);
this.flip = true;
this.moved = true;
} else if (ig.input.state('right')) {
this.accel.x = (this.standing ? this.accelDef.ground : this.accelDef.air);
this.flip = false;
this.moved = true;
} else {
this.accel.x = 0;
}

// fire button pressed
if (ig.input.pressed('shoot')) {
if (this.weaponsLeft > 0) {
this.weaponsLeft--;
ig.game.playerShoot();
}
// this.sfxPlasma.play();
}
this.wantsJump = this.wantsJump || ig.input.pressed('jump');
if (this.standing && (ig.input.pressed('jump') ||
(!this.wasStanding && this.wantsJump && ig.input.state('jump')))) {
ig.mark('jump');
this.wantsJump = false;
this.canHighJump = true;
this.highJumpTimer.set(0.14);
this.vel.y = -this.jump / 4;
} else if (this.canHighJump) {
var d = this.highJumpTimer.delta();
if (ig.input.state('jump')) {
var f = Math.max(0, d > 0 ? ig.system.tick - d : ig.system.tick);
this.vel.y -= this.jump * f * 6.5;
} else {
this.canHighJump = false;
}
if (d > 0) {
this.canHighJump = false;
}
}
this.wasStanding = this.standing;
this.parent();
this.setAnimation();
},
setAnimation: function() {
if ((!this.wasStanding && this.standing)) {
this.currentAnim = this.anims.land.rewind();
} else if (this.standing && (this.currentAnim != this.anims.land ||
this.currentAnim.loopCount > 0)) {
if (this.moved) {
if (this.standing) {
this.currentAnim = this.anims.run;
}
this.idle = false;
} else {
if (!this.idle || this.currentAnim.stop && this.currentAnim.loopCount > 0) {
this.idle = true;
this.idleTimer.set(Math.random() * 4 + 3);
this.currentAnim = this.anims.idle;
}
if (this.idleTimer.delta() > 0) {
this.idleTimer.reset();
this.currentAnim = [this.anims.scratch, this.anims.shrug].random().rewind();
}
}
} else if (!this.standing) {
if (this.vel.y < 0) {
this.currentAnim = this.anims.jump;
} else {
if (this.currentAnim != this.anims.fall) {
this.anims.fall.rewind();
}
this.currentAnim = this.anims.fall;
}
this.idle = false;
}
this.currentAnim.flip.x = this.flip;
if (this.flip) {
this.currentAnim.tile += this.flippedAnimOffset;
}
},
collideWith: function(other, axis) {
if (axis == 'y' && this.standing && this.currentAnim != this.anims.die) {
this.currentAnim.update();
this.setAnimation();
}
},
receiveDamage: function(amount, from) {
this.health -= amount;
if (this.health > 0) {
return;
}
if (from.userId) {
this.killerId = from.userId;
}
if (this.currentAnim != this.anims.die) {
this.currentAnim = this.anims.die.rewind();
for (var i = 0; i < 3; i++) {
ig.game.spawnEntity(EntityPlayerGib, this.pos.x, this.pos.y);
}
ig.game.spawnEntity(EntityPlayerGibGun, this.pos.x, this.pos.y);
// this.sfxDie.play();
}
},
kill: function() {
ig.game.playerDied(this.killerId);
this.parent();
},
addWeapons: function(weaponsCount) {
this.weaponsLeft = Math.min(this.MAX_WEAPONS, this.weaponsLeft + weaponsCount);
}
});
EntityPlayerGib = EntityParticle.extend({
lifetime: 0.8,
fadetime: 0.4,
friction: {
x: 0,
y: 0
},
vel: {
x: 30,
y: 80
},
gravityFactor: 0,
animSheet: new ig.AnimationSheet('media/sprites/player-red.png', 8, 8),
init: function(x, y, settings) {
this.addAnim('idle', 7, [82, 94]);
this.parent(x, y, settings);
},
update: function() {
this.parent();
}
});
EntityPlayerGibGun = EntityParticle.extend({
lifetime: 2,
fadetime: 0.4,
size: {
x: 8,
y: 8
},
friction: {
x: 30,
y: 0
},
vel: {
x: 60,
y: 50
},
animSheet: new ig.AnimationSheet('media/sprites/player-red.png', 8, 8),
init: function(x, y, settings) {
this.addAnim('idle', 0.5, [11]);
this.parent(x, y, settings);
this.currentAnim.flip.y = false;
}
});
EntityProjectile = ig.Entity.extend({
size: {
x: 6,
y: 3
},
offset: {
x: 1,
y: 2
},
maxVel: {
x: 200,
y: 0
},
gravityFactor: 0,
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.B,
collides: ig.Entity.COLLIDES.NEVER,
flip: false,
hasHit: false,
animSheet: new ig.AnimationSheet('media/sprites/projectile.png', 8, 8),
init: function(x, y, settings) {
this.parent(x, y, settings);
this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
this.addAnim('idle', 1, [0]);
this.addAnim('hit', 0.1, [0, 1, 2, 3, 4, 5], true);
},
update: function() {
if (this.hasHit && this.currentAnim.loopCount > 0) {
this.kill();
}
this.parent();
this.currentAnim.flip.x = this.flip;
},
handleMovementTrace: function(res) {
this.parent(res);
if (res.collision.x || res.collision.y) {
this.currentAnim = this.anims.hit;
this.hasHit = true;
}
},
check: function(other) {
if (!this.hasHit) {
this.hasHit = true;
this.currentAnim = this.anims.hit;
this.vel.x = 0;
}
}
});

EntityProjectileRemote = EntityProjectile.extend({
checkAgainst: ig.Entity.TYPE.A,
check: function(other) {
if (!this.hasHit) {
other.receiveDamage(10, this);
this.hasHit = true;
this.currentAnim = this.anims.hit;
this.vel.x = 0;
}
}
});

});
Loading

0 comments on commit 758ec6b

Please sign in to comment.