Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Created December 20, 2010 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oberhamsi/748413 to your computer and use it in GitHub Desktop.
Save oberhamsi/748413 to your computer and use it in GitHub Desktop.
dersoldat gamejs file
var gamejs = require('gamejs');
var $v = require('gamejs/utils/vectors');
function main() {
gamejs.display.setCaption("Der Soldat");
// create background
var screen = gamejs.display.setMode([850, 500]);
var displayRect = screen.rect;
var bg = gamejs.image.load('images/ground.png');
var clouds = gamejs.image.load('images/clouds.png');
var heliGui = gamejs.image.load('images/heligui.png');
// test
var combatants = new gamejs.sprite.Group();
var bombs = new gamejs.sprite.Group();
var getCombatant = function(type) {
var tank = new gamejs.scene.MovingSprite();
var pos = [0,0];
if (Math.random() > 0.5) {
pos[0] = parseInt(Math.random() * displayRect.width);
} else {
pos[1] = parseInt(Math.random() * displayRect.height);
}
tank.setPosition(pos);
tank.setImage('images/' + type + '.png');
tank.setSpeed(type === 'tank' ? 5 : 2);
tank.goTo([Math.random() * displayRect.width, Math.random() * displayRect.height]);
return tank;
}
// sounds
/*
var pulse = new gamejs.mixer.Sound('sounds/lowpulse.ogg');
var explosion = new gamejs.mixer.Sound('sounds/explosion.ogg');
var kick =
*/
var craters = [];
/**
* bomb
*/
var Bomb = function(pos) {
var EXP_SIZE = [60, 60];
Bomb.superConstructor.apply(this, arguments);
this.rect = new gamejs.Rect([0,0], EXP_SIZE);
this.rect.center = pos;
this.image = null;
this.flightDuration = 0;
this.explosionDuration = 0;
this.isOnGround = false;
this.frameTime = 0;
this.currentFrame = 0;
this.frameDuration = 80;
this.frameDirection = 1;
(new gamejs.mixer.Sound('sounds/kick.ogg')).play();
(new gamejs.mixer.Sound('sounds/lowpulse.ogg')).play();
return this;
};
gamejs.utils.objects.extend(Bomb, gamejs.sprite.Sprite)
Bomb.prototype.draw = function(screen) {
if (!this.image) return;
screen.blit(this.image, this.rect);
return;
};
Bomb.prototype.update = function(msDuration) {
if (this.isOnGround) {
this.frameTime += msDuration;
if (!this.image || this.frameTime > this.frameDuration) {
if (this.currentFrame < 0) {
this.kill();
return;
}
this.image = gamejs.image.load('images/explosion_' + this.currentFrame +'.png');
this.frameTime = 0;
if (this.currentFrame == 11) {
var craterRect = new gamejs.Rect(this.rect);
craterRect.width = 15
craterRect.height = 15;
craterRect.center = this.rect.center;
craters.push({
image: gamejs.image.load('images/crater_' + parseInt(Math.random() * 6, 10) + '.png'),
rect: craterRect,
});
this.frameDirection = -1;
}
this.currentFrame += this.frameDirection;
}
} else {
this.flightDuration += msDuration;
if (this.flightDuration > 3500) {
(new gamejs.mixer.Sound('sounds/explosion.ogg')).play();
this.isOnGround = true;
};
}
return;
};
/**
* Main Game 'loop'
*
*/
var jitterPause = 300;
var jitterCumulated = null;
jitterPos = displayRect;
var spawnPause = 3500;
var spawnCumulated = null;
var shootPause = 1500;
var shootCumulated = shootPause * 2;
function tick(msDuration) {
// event handling
gamejs.event.get().forEach(function(event) {
if (event.type === gamejs.event.MOUSE_UP) {
if (event.button === 0) {
if (shootCumulated > shootPause) {
bombs.add(new Bomb(event.pos));
shootCumulated = 0;
}
}
};
});
shootCumulated += msDuration;
// update models
combatants.update(msDuration);
bombs.update(msDuration);
// bomb combat collision detection
bombs.sprites().filter(function(bomb) {
if (bomb.currentFrame >= 8) {
gamejs.sprite.spriteCollide(bomb, combatants, true);
}
}, this);
// combatent spawn
if (spawnCumulated === null || spawnCumulated > spawnPause) {
if (Math.random() > 0.5) {
combatants.add(getCombatant('tank'));
combatants.add(getCombatant('tank'));
} else {
combatants.add(getCombatant('infantry'));
combatants.add(getCombatant('infantry'));
}
spawnCumulated = 0;
}
spawnCumulated += msDuration;
// draw
screen.blit(bg, displayRect);
craters.forEach(function(crater) {
screen.blit(crater.image, crater.rect);
});
combatants.draw(screen);
bombs.draw(screen);
// screen jitter
if (jitterCumulated === null | jitterCumulated >= jitterPause) {
jitterPos = displayRect.move(-5 + (Math.random() * 5), -5 + (Math.random() * 5))
jitterPos.width = displayRect.width * 1.3;
jitterPos.height = displayRect.height * 1.3;
jitterCumulated = 0;
}
jitterCumulated += msDuration;
screen.blit(clouds, jitterPos);
screen.blit(heliGui, displayRect);
};
gamejs.time.fpsCallback(tick, this, 30);
return;
}
var PRELOAD_LIST = [
'images/ground.png',
'images/clouds.png',
'images/heligui.png',
'images/tank.png',
'images/infantry.png',
'sounds/kick.ogg',
'sounds/explosion.ogg',
'sounds/lowpulse.ogg',
];
for (var i=0;i<12;i++) {
PRELOAD_LIST.push('images/explosion_' + i + '.png');
};
for (var i=0;i<6;i++) {
PRELOAD_LIST.push('images/crater_' + i + '.png');
};
gamejs.preload(PRELOAD_LIST);
gamejs.ready(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment