Commit c2ddfe5f by Tomás Teijeiro Campo

Add 'js/' from commit '1af73132'

git-subtree-dir: js
git-subtree-mainline: bb8d5e94
git-subtree-split: 1af73132
parents bb8d5e94 1af73132
# Created by https://www.gitignore.io/api/intellij,linux
### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
.idea/
## File-based project format:
*.iws
## Plugin-specific files:
# IntelliJ
/out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
# *.iml
# modules.xml
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
\ No newline at end of file
var invadersApp = {};
invadersApp.Boot = function (game) {
};
invadersApp.Boot.prototype = {
init: function () {
// Unless you specifically know your game needs to support multi-touch I would recommend setting this to 1
this.input.maxPointers = 1;
// Phaser will automatically pause if the browser tab the game is in loses focus. You can disable that here:
this.stage.disableVisibilityChange = true;
if (this.game.device.desktop)
{
// If you have any desktop specific settings, they can go in here
this.scale.pageAlignHorizontally = true;
//this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
var scale = (window.innerWidth/960) * 0.8;
this.scale.setUserScale(scale, scale);
this.scale.scaleMode = Phaser.ScaleManager.USER_SCALE;
}
else
{
// Same goes for mobile settings.
// In this case we're saying "scale the game, no lower than 480x260 and no higher than 1024x768"
this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.scale.setMinMax(480, 260, 1024, 768);
this.scale.forceLandscape = true;
this.scale.pageAlignHorizontally = true;
}
// Pixelized!
this.game.antialias = false;
this.game.stage.smoothed = false;
},
preload: function () {
this.load.image('preloaderBackground', 'assets/background.png');
this.load.image('retroFont', 'assets/fonts/font3.png');
this.load.image('preloaderBar', 'assets/preload.png');
},
create: function () {
// By this point the preloader assets have loaded to the cache, we've set the game settings
// So now let's start the real preloader going
this.state.start('Preloader');
}
};
This diff is collapsed. Click to expand it.
invadersApp.MainMenu = function (game) {
this.mainMusic = null;
this.playButton = null;
};
invadersApp.MainMenu.prototype = {
create: function () {
this.mainMusic = this.add.audio('mainMusic');
var textTop = invadersApp.utils.addText(this, this.game.width / 2, 50, 'CITIUS PROUDLY PRESENTS:' , 2);
textTop.alpha = 0;
var titleYPos = this.game.height / 3;
var title = this.add.sprite(this.game.width / 2, -100, 'title');
title.anchor.setTo(0.5, 0.5);
var logo = this.add.sprite(this.game.width / 2, this.game.height - 60, 'logo');
logo.anchor.setTo(0.5, 0.5);
logo.scale.setTo(0.6, 0.6);
logo.visible = false;
var textIES = invadersApp.utils.addText(this, this.game.width / 2, titleYPos + 50, 'A GAME FOR IES ROSALIA DE CASTRO', 1);
textIES.img.visible = false;
var textPressStart = invadersApp.utils.addText(this, this.game.width / 2, titleYPos + 200, 'PRESS ENTER', 2);
textPressStart.img.visible = false;
var textCopyright = invadersApp.utils.addText(this, this.game.width / 2, logo.y + 40, 'CENTRO SINGULAR DE INVESTIGACION EN TECNOLOXIAS DA INFORMACION', 1);
textCopyright.img.visible = false;
var tweenPresents = this.game.add.tween(textTop).to( { alpha: 1 }, 800, Phaser.Easing.Linear.None, false, 200);
var tweenTitle = this.game.add.tween(title).to( { y: this.game.height / 3 }, 1200, Phaser.Easing.Bounce.Out, false);
tweenPresents.chain(tweenTitle);
tweenPresents.onComplete.add(function () {
// Play mainMusic
this.mainMusic.play('', 0, 1, true, true);
}, this);
tweenTitle.onComplete.add(function () {
// Show bottom info
logo.visible = true;
textCopyright.img.visible = true;
textPressStart.img.visible = true;
textIES.img.visible = true;
// Start blinking event for 'PRESS START'
this.game.time.events.loop(Phaser.Timer.HALF, function () {
textPressStart.img.visible = !textPressStart.img.visible;
}, this);
}, this);
// Start animated chain
tweenPresents.start();
this.game.input.keyboard.addKey(Phaser.Keyboard.ENTER).onDown.add(function () {
this.startGame();
}, this);
},
update: function () {
},
startGame: function (pointer) {
this.mainMusic.stop();
this.state.start('Game');
}
};
invadersApp.Preloader = function (game) {
this.background = null;
this.preloadBar = null;
this.ready = false;
};
invadersApp.Preloader.prototype = {
preload: function () {
var loadingText = invadersApp.utils.addText(this, this.game.width / 2, this.game.height / 2 - 60, 'Loading...', 3);
this.preloadBar = this.add.sprite(this.game.width / 2, loadingText.img.y + 40, 'preloaderBar');
this.preloadBar.anchor.setTo(0.5, 0.5);
// This sets the preloadBar sprite as a loader sprite.
// What that does is automatically crop the sprite from 0 to full-width
// as the files below are loaded in.
this.load.setPreloadSprite(this.preloadBar);
// Here we load the rest of the assets our game needs.
// As this is just a Project Template I've not provided these assets, swap them for your own.
this.load.image('titlepage', 'assets/title.png');
this.load.audio('titleMusic', ['assets/audio/bodenstaendig_2000_in_rock_4bit.ogg']);
this.load.audio('gameOverMusic', ['assets/audio/invaders_gameover.ogg']);
this.load.audio('mainMusic', ['assets/audio/invaders_mainloop.ogg']);
this.load.audio('sfx', 'assets/audio/fx_mixdown.ogg');
// + lots of other required assets here
this.load.image('nao', 'assets/nao.png');
this.load.image('invader', 'assets/invader.png');
this.load.image('bullet', 'assets/player_bullet.png');
this.load.image('title', 'assets/title-3.png');
this.load.image('logo', 'assets/citius-logo-8bit.png');
// Read game settings
this.load.json('settings', 'settings.json');
},
create: function () {
// Once the load has finished we disable the crop because we're going to sit in the update loop for a short while as the mainMusic decodes
this.preloadBar.cropEnabled = false;
},
update: function () {
if (this.cache.isSoundDecoded('titleMusic') && this.ready == false)
{
this.ready = true;
this.state.start('MainMenu');
}
}
};
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>CiTIUS Invaders</title>
<!--<script src="http://zeptojs.com/zepto.min.js"></script>-->
<script src="phaser.min.js"></script>
<script src="chance.min.js"></script>
<script src="Boot.js"></script>
<script src="Preloader.js"></script>
<script src="js/Utils.js"></script>
<script src="js/Player.js"></script>
<script src="js/Invader.js"></script>
<script src="js/Evolution.js"></script>
<script src="MainMenu.js"></script>
<script src="Game.js"></script>
</head>
<body>
<div id="gameContainer"></div>
<script type="text/javascript">
window.onload = function() {
var game = new Phaser.Game(960, 540, Phaser.AUTO, 'gameContainer');
game.state.add('Boot', invadersApp.Boot);
game.state.add('Preloader', invadersApp.Preloader);
game.state.add('MainMenu', invadersApp.MainMenu);
game.state.add('Game', invadersApp.Game);
game.state.start('Boot');
};
</script>
</body>
</html>
\ No newline at end of file
/**
* Created by Pablo Rodríguez Mier
*/
var invadersApp = invadersApp || {};
invadersApp.evolution = {
evolve: function(pool, genes, mutation, effect){
// Load from settings
if (mutation === undefined) { mutation = 0.1 }
if (effect === undefined) { effect = 0.5 }
var offspring = [];
pool.forEach(function (pair) {
var p1 = pair[0];
var p2 = pair[1];
var children = [];
for(var gen in genes){
if (genes.hasOwnProperty(gen)) {
var gen1 = p1.genes[gen];
var gen2 = p2.genes[gen];
var min = (gen1 < gen2) ? gen1 : gen2;
var max = (gen1 > gen2) ? gen1 : gen2;
children[gen] = Math.random() * (max - min) + min;
if (Math.random() < mutation) {
// Mutate this gene
var value = chance.pickone([-1.0, 1.0]) * Math.random() * (genes[gen].max - genes[gen].min) * effect;
if ((children[gen] == genes[gen].min && value < 0) || (children[gen] == genes[gen].max && value > 0)) value = -value;
children[gen] = children[gen] + value;
if (children[gen] < genes[gen].min) children[gen] = genes[gen].min;
if (children[gen] > genes[gen].max) children[gen] = genes[gen].max;
}
}
}
offspring.push([p1, p2, children]);
});
return offspring;
},
pool: function(population, size){
var pool = [];
while (pool.length < size){
var p1 = this.binaryTournament(population);
// Copy and remove the selected invader
var pop2 = population.slice(0);
pop2.splice(pop2.indexOf(p1),1);
var p2 = this.binaryTournament(pop2);
pool.push([p1, p2]);
}
return pool;
},
binaryTournament: function(population){
var invader1 = this.pick(population);
var invader2 = this.pick(population);
if (invader1.fitness > invader2.fitness) {
return invader1;
} else {
return invader2;
}
},
pick: function(array){
return array[Math.floor(Math.random()*array.length)];
}
};
\ No newline at end of file
var invadersApp = invadersApp || {};
// Port of python random.gammavariate
function gammavariate(alpha, beta) {
SG_MAGICCONST = 2.504077396776274;
ainv = Math.sqrt(2.0 * alpha - 1.0);
bbb = alpha - 1.3862943611198906;
ccc = alpha + ainv;
while (1) {
u1 = Math.random();
if (u1 <= 1e-7 || u1 >= .9999999) {
continue;
}
u2 = 1.0 - Math.random();
v = Math.log(u1 / (1.0 - u1)) / ainv;
x = alpha * Math.exp(v);
z = u1 * u1 * u2;
r = bbb + ccc * v - x;
if (r + SG_MAGICCONST - 4.5 * z >= 0.0 || r >= Math.log(z)) {
return x * beta;
}
}
}
function bound_value(v, min_v, max_v) {
return Math.min(Math.max(min_v, v), max_v)
}
// Extended sprite object for Invaders
invadersApp.Invader = function (ctx, genes, x, y) {
this.game = ctx.game;
x = x || this.game.world.randomX;
y = y || this.game.world.randomY % (this.game.world.height - WALL_MARGIN * 1.5);
Phaser.Sprite.call(this, this.game, x, y, 'invader');
this.game.physics.enable(this, Phaser.Physics.ARCADE);
// Initialize genes by getting the default values from settings.json
this.genes = genes || function () {
var settings = ctx.settings;
var genes = {};
//TODO guarrada para comprobar a influencia da primeira xeración
genes['scale'] = bound_value(gammavariate(4, 0.5) + 1, 1, 7);
genes['alpha'] = chance.integer({min: 20, max: 255});
genes['xvelocity'] = 120 * bound_value(gammavariate(2, 0.4), 0.01, 5);
genes['yvelocity'] = 120 * bound_value(gammavariate(2, 0.3), 0.01, 5);
genes['x_prob_change_dir'] = chance.floating({min: 0.01, max: 0.05});
genes['y_prob_change_dir'] = chance.floating({min: 0.01, max: 0.05});
return genes;
}();
this.anchor.setTo(0.5, 0.5);
var alpha = Math.round(this.genes['alpha']);
this.tint = Phaser.Color.getColor(alpha, alpha, alpha);
this.body.velocity.x = this.genes['xvelocity'];
this.body.velocity.y = this.genes['yvelocity'];
this.scaleValue = this.genes['scale'];
this.scale.setTo(this.scaleValue, this.scaleValue);
this.body.collideWorldBounds = true;
this.body.bounce.set(1);
// Used to control the probability of x-y change in direction
// this.lastTimeChanged = 0;
// Event loop to check the probability of changing directions (each 10ms)
// TODO: Move this timer to Game.js?
this.changeTimer = this.game.time.events.loop(10, function () {
if (this.alive && this.game != null) {
if (this.game.rnd.frac() < this.genes['x_prob_change_dir']) {
this.body.velocity.x = -this.body.velocity.x;
}
if (this.game.rnd.frac() < this.genes['y_prob_change_dir']) {
this.body.velocity.y = -this.body.velocity.y;
}
}
}, this);
this.fitness = 0;
// Create a shield
this.shieldGraphics = this.game.make.graphics(0, 0);
// Add the invader to the game (move this outside this class?)
this.game.add.existing(this);
};
invadersApp.Invader.prototype = Object.create(Phaser.Sprite.prototype);
invadersApp.Invader.prototype.constructor = invadersApp.Invader;
invadersApp.Invader.prototype.drawShield = function (color) {
this.shieldGraphics.clear();
this.shieldGraphics.lineStyle(1, color, 1);
this.shieldGraphics.drawCircle(-0.5, -0.5, 15 * this.scaleValue);
this.addChildAt(this.shieldGraphics, 0);
this.shieldGraphics.visible = true;
this.shieldGraphics.scale.setTo(1.5 / this.scaleValue, 1.5 / this.scaleValue);
};
invadersApp.Invader.prototype.hideShield = function () {
this.shieldGraphics.clear();
this.shieldGraphics.visible = false;
};
invadersApp.Invader.prototype.increaseFitness = function () {
this.fitness++;
};
var invadersApp = invadersApp || {};
invadersApp.Player = function (ctx) {
this.shootDelay = 30;
this.ctx = ctx;
this.game = ctx.game;
Phaser.Sprite.call(this, this.game, this.game.width / 2, this.game.height - 30, 'nao');
this.game.physics.enable(this, Phaser.Physics.ARCADE);
this.anchor.setTo(0.5, 0.5);
this.game.physics.enable(this, Phaser.Physics.ARCADE);
this.body.collideWorldBounds = true;
// Create a pool of bullets
this.bullets = this.game.add.group();
this.bullets.enableBody = true;
this.bullets.physicsBodyType = Phaser.Physics.ARCADE;
// Max = 8 bullets
this.bullets.createMultiple(8, 'bullet');
this.bullets.setAll('anchor.x', 0.5);
this.bullets.setAll('anchor.y', 1);
this.bullets.setAll('outOfBoundsKill', true);
this.bullets.setAll('checkWorldBounds', true);
this.readyToFire = true;
this.lastShootAt = 0;
};
invadersApp.Player.prototype = Object.create(Phaser.Sprite.prototype);
invadersApp.Player.prototype.constructor = invadersApp.Player;
invadersApp.Player.prototype.update = function () {
if (this.game.physics.arcade.isPaused) return;
this.body.velocity.setTo(0, 0);
if (this.ctx.cursors.left.isDown) {
if (this.scale.x > 0){
this.scale.x *= -1;
}
this.body.velocity.x = -500;
}
else if (this.ctx.cursors.right.isDown) {
if (this.scale.x < 0){
this.scale.x *= -1;
}
this.body.velocity.x = 500;
}
if (this.ctx.fireButton.isDown && this.readyToFire) {
this.readyToFire = false;
if (this.ctx.gameState == invadersApp.GameState.RUNNING &&
this.game.time.now > this.lastShootAt + this.shootDelay) {
// Check the number of bullets alive
var aliveBullets = this.bullets.countLiving();
// Get the number of alive invaders
var aliveInvaders = this.ctx.living;
if (aliveInvaders > MIN_INVADERS && aliveBullets <= aliveInvaders/10) {
this.lastShootAt = this.game.time.now;
var bullet = this.bullets.getFirstExists(false);
if (bullet) {
// And fire it
var xpos;
if (this.scale.x < 0) {
xpos = this.x - 21;
} else {
xpos = this.x + 21;
}
bullet.reset(xpos, this.y - 20);
//bullet.body.velocity.y = -(1000 + aliveInvaders * 15);
bullet.body.velocity.y = -2000;
this.ctx.fx.play('shot');
}
}
}
}
if (this.ctx.fireButton.isUp){
this.readyToFire = true;
}
};
var invadersApp = invadersApp || {};
invadersApp.utils = {};
invadersApp.utils.TEXT_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!:.©_-/()";
invadersApp.utils.addText = function (game, x, y, text, size) {
var scale = size || 1;
var font = game.add.retroFont('retroFont', 8, 8, invadersApp.utils.TEXT_SET, 0);
font.text = text;
var img = game.add.image(x, y, font);
img.scale.setTo(scale, scale);
img.anchor.setTo(0.5, 0.5);
return {'font': font, 'img': img};
};
invadersApp.utils.pad = function (n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
};
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"version": "0.1",
"genes": {
"scale": {
"min": 1,
"max": 7,
"mean": 4
},
"alpha": {
"min": 5,
"max": 255,
"mean": 200
},
"xvelocity": {
"min": 1,
"max": 500,
"mean": 200
},
"yvelocity": {
"min": 1,
"max": 500,
"mean": 200
},
"x_prob_change_dir": {
"min": 0.01,
"max": 0.07,
"mean": 0.03
},
"y_prob_change_dir": {
"min": 0.01,
"max": 0.07,
"mean": 0.03
}
},
"mutation_probability": 0.1,
"mutation_effect": 0.5
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment