Sic/frame3.as

From Organic Design wiki
Legacy.svg Legacy: This article describes a concept that has been superseded in the course of ongoing development on the Organic Design wiki. Please do not develop this any further or base work on this concept, this is only useful for a historic record of work done. You may find a link to the currently used concept or function in this article, if not you can contact the author to find out what has taken the place of this legacy item.
//
// Liscenced under LGPL: www.gnu.org/copyleft/lesser.html
//
// Nad - 2003 - SIC-Games
//
// Frame3: SICK INITIALISATION


// GAME SPACE
// GAME OBJECTS
// DEMO SPACE
// GUN & TARGET
// FADER
// ENVIRONMENT
// SOUND
// RANDOM

// _____________________________________________________________________________________________________________________ //
// GAME SPACE
createEmptyMovieClip('game', 1);
initialiseSpace(game);
game._x = 512;
game._y = 384;

// _____________________________________________________________________________________________________________________ //
// GAME OBJECTS
objects = {};
objects.n = 0;
objects.max = 6;
objects.kills = 0;
// The kill event comes 500ms after a peer hits it
objects.kill = function(parameters) {
	var obj = _root.game[parameters[3]];
	// Change bg every n kills
	if (++_root.objects.kills%3==0) _root.environment.change();
	// Explosion
	//_root.game.create([0,0,'','starBurst',10,0.25,obj.x,obj.y,obj.z]);
	// Kill object
	obj.die = true;
	_root.objects.n--;
	};

// _____________________________________________________________________________________________________________________ //
// DEMO SPACE
attachMovie('bg', 'demo', 3);
initialiseSpace(demo);
demo.personas = ['tiger','tamarin','wolf','hawk'];
// load new persona
demo.setPersona = function(p) {
	var demo = _root.demo;
	demo.createEmptyMovieClip('swf', 2);
	demo.swf.loadMovie('./swf/'+p+'.swf');
	if (p == 'tiger') {
		demo.swf._x = 200;
		demo.swf._y = 100;
		}
	else {
		demo.swf._x = 70;
		demo.swf._y = 70;
		}
	demo.persona = p;
	};
demo.active = demo._visible = false;
//demo.setPersona('hawk');

// _____________________________________________________________________________________________________________________ //
// GUN & TARGET
game.target = game.create([0,0,'','torus',4,150,5000]);
gun = {};
gun.onMouseMove = function() {
	if (_root.shell.state == 2) return;
	var x = _root._xmouse-512;
	var y = _root._ymouse-384;
	_root.game.target._x = x;
	_root.game.target._y = y;
	x = x/512;
	if (x > 1) x = 1;
	if (x < -1) x = -1;
	var b=40.5,r = 5;
	if (y > 200) {b = 29.5; r = -5;}
	else if (y > -50) {b = 17.5; r=4;}
	else if (y > -180) {b = 6.5; r=-5;}
	this.frame = int(b+x*r+0.5);
	};
gun.onMouseDown = function() {
	if (_root.shell.state == 2) return;
	if (_root.gun.patch++%7) _root.gun.fire.state = 1;
	else _root.sounds.load1.start();
	};
gun.sr = function(f1,f2,x1,x2,y) {
	for (var f = f1; f <= f2; f++) {
		gun.ox[f] = x1+(f-f1)/(f2-f1)*(x2-x1);
		gun.oy[f] = y;
		}
	};
gun.bullets = gun.shots;
gun.object = game.create([0,0,'','gun']);
gun.fire = game.create([0,0,'','fire']);
gun.lastShot = time;
gun.frame = 1;
gun.ox = [];
gun.oy = [];
gun.sr(1,12,120,-150,-75);
gun.sr(13,22,-170,180,-30);
gun.sr(24,35,100,-150,50);
gun.sr(36,45,-75,100,-80);
gun.ox[7] -= 20;
gun.ox[8] -= 40;
gun.ox[9] -= 60;
gun.ox[10] -= 50;
gun.ox[30] -= 50;
gun.ox[31] -= 75;
gun.ox[37] -= 20;
gun.ox[38] -= 20;
gun.ox[39] -= 20;
gun.ox[42] += 40;
gun.ox[43] += 50;
gun.ox[44] += 40;
Mouse.addListener(gun);

// _____________________________________________________________________________________________________________________ //
// FADER
attachMovie('spacer', 'fader', 4);
fader._x = 512;
fader._y = 384;
fader._width = 1024;
fader._height = 768;

// _____________________________________________________________________________________________________________________ //
// ENVIRONMENT
// bg-changer resets game-space and clears finishedEvents
createEmptyMovieClip('environment', 0);
environment._xscale = environment._yscale = 400;
environment.lastActivity = time;
environment.lastSync = time;
environment.lastChange = time;
environment.timeout = 30000; // milliseconds (300000 = 5min)
environment.time = null;
environment.phase = null;
environment.current = 0;
// Called per-frame
environment.frame = function() {
	var t = _root.time - this.time; // time into this phase
	// Set bg frame
	var f = Math.floor(t/150+_root.network.thisIndex*this.swf._totalFrames/4);
	this.swf.gotoAndStop(1+(f%this.swf._totalFrames));
	if (this.phase == 1) {
		// phase1: normal game-play
		
		// change environment if same for too long
		if (_root.time-this.lastChange > 60000) _root.environment.change([0,time]);

		// screams with gunshots
		if (_root.time-_root.gun.lastShot < 2000) {
			if (Math.random() < 0.02) {
				var snd = _root.sounds['scream'+int(Math.random()*10)];
				var vol = int(-this.z/15);
				if (vol < 20) vol = 20;
				if (vol > 100) vol = 100;
				snd.setVolume(vol);
				snd.start();
				}
			}
			
		// Object creation
		if ((++_root.objects.ctr%250 == 40*(1+_root.network.thisIndex)+1) && (_root.objects.n < _root.objects.max)) {
//		if ((Math.random(100)<10) && (_root.objects.n < _root.objects.max)) {
			var s = swfList[Math.floor(Math.random()*swfList.length)];
			var t = ''+(time+1000);
			createEvent(t+',game.create,obj,'+s, null);
			createEvent(t+',playSound,implode1,50', null);
			objects.n++;
			}

		}
	else if (this.phase == 2) {
		// fade out of current env
		var fader = _root.fader;
		var a = t/20;
		if (a > 100) a = 100;
		fader._alpha = a;
		if (fader._alpha = 100) {
			this.time = _root.time;
			this.phase++;
			}
		}
	else if (this.phase == 3) {
		// clear game space (but gun and fire need to be put back)
		for (var i = 0; i < _root.game.zbLength; i++) {
			var obj = _root.game.zbuff[i];
			if (obj.applySR) {logAdd('die'); obj.die = true;}
			}
		_root.objects.n = 0;
		// later: set up starfield
		this.time = _root.time;
		this.phase++;
		}
	else if (this.phase == 4) {
		// fade-in starfield
		this.time = _root.time;
		this.phase++;
		}
	else if (this.phase == 5) {
		// normal starfield
		this.time = _root.time;
		this.phase++;
		}
	else if (this.phase == 6) {
		// fade-out starfield
		this.time = _root.time;
		this.phase++;
		}
	else if (this.phase == 7) {
		// clear game space (but gun and fire need to be put back)
		// load new bg
		var file = _root.bgList[++this.current%_root.bgList.length];
		this.createEmptyMovieClip('swf', 0);
		this.swf.loadMovie('./swf/'+file);
		this.swf.stop();
		this.time = _root.time;
		this.phase++;
		}
	else if (this.phase == 8) {
		// fade in to new env
		var fader = _root.fader;
		var a = 100-t/20;
		if (a < 0) a = 0;
		fader._alpha = a;
		if (fader._alpha = 0) {
			this.time = _root.time;
			this.phase = 1;
			}
		}
	else if (this.phase == 10) {
		// Demo activated, fade out of current env
		var fader = _root.fader;
		var a = t/20;
		if (a > 100) a = 100;
		fader._alpha = a;
		if (fader._alpha = 100) {
			this.time = _root.time;
			this.phase++;
			}
		}
	else if (this.phase == 11) {
		// Switch on demo and deactivate game
		_root.demo._visible = _root.demo.active = true;
		_root.game._visible = _root.game.active = false;
		this.phase++;
		}
	else if (this.phase == 12) {
		// fade in demo
		var fader = _root.fader;
		var a = 100-t/20;
		if (a < 0) a = 0;
		fader._alpha = a;
		// set demo frame
		var f = 1+Math.floor((t/100)%_root.demo.swf._totalFrames);
		_root.demo.swf.gotoAndStop(f);
		// Sound
		if (f == 1) {
			var snd = sounds['cam'+Math.floor(Math.random()*9)];
			snd.setVolume(30);
			snd.start();
			}
		}
	};
// Change to next enviironment
environment.change = function(parameters) {
	var env = _root.environment;
	// remember bg change-time (for fader phases etc)
	env.time = 1*parameters[1];
	if (env.time == 0) env.time = _root.time;
	env.phase = 2;
	env.lastChange = env.time;
	};
// Record last input for activating demo mode (screensaver) - actual change is in frame4 code
environment.onMouseDown = environment.onMouseMove = function() {
	if (_root.shell.state == 2) return;
	_root.demo._visible = _root.demo.active = false;
	_root.game._visible = _root.game.active = true;
	_root.fader._alpha = 0;
	// Propagate input if we haven't done so for a few seconds (to ensure screensavers are synced)
	if ((_root.time - environment.lastSync) > (environment.timeout-5000)) {
		environment.lastSync = _root.time;
		createEvent(''+_root.time+',environment.inputEvent', null);
		_root.environment.lastActivity = _root.time;
		}
//	_root.environment.lastChange = _root.time;
	};
// respond to incoming inputEvent (to sync screensavers)
environment.inputEvent = function(parameters) {
	if (_root.shell.state == 2) return;
	_root.demo._visible = _root.demo.active = false;
	_root.game._visible = _root.game.active = true;
	_root.fader._alpha = 0;
	_root.environment.phase = 1;
	_root.environment.lastActivity = 1*parameters[1];
//	_root.environment.lastChange = _root.time;
	};
Mouse.addListener(environment);
environment.change([0,time]);

// _____________________________________________________________________________________________________________________ //
// SOUND
sounds = {};
sounds.list = [];
loadSounds('scream', 10);
loadSounds('ric', 12);
loadSounds('load', 1);
loadSounds('gun', 5);
loadSounds('die', 10);
loadSounds('cam', 9);
loadSounds('bone', 10);
loadSounds('implode', 1);

function loadSounds(name, n) {
	for (var i = 1; i <= n; i++) {
		var id = name+i;
		_root.sounds[id] = new Sound();
		var s = _root.sounds[id];
		s.loadSound('./sound/'+id+'.mp3', false);
		s.setVolume(100);
		_root.sounds.list.push(s);
		}
	}

// Play sound function which can be called from an event
function playSound(parameters) {
	var snd = _root.sounds[parameters[3]];
	snd.setVolume(parseInt(parameters[4]));
	snd.start();
	}

// _____________________________________________________________________________________________________________________ //
// RANDOM
// - from shared random table
function rnd(j, k) {
	j = Math.floor(j)%600;
	var n = 0.1*_root.randTable[j];
	n += 0.01*_root.randTable[j+k];
	n += 0.001*_root.randTable[j+k*2];
	n += 0.0001*_root.randTable[j+k*3];
	n += 0.00001*_root.randTable[j+k*4];
	n += 0.000001*_root.randTable[j+k*5];
	return n;
	}
randString = "1729676126689669130222528464311742821838347712063000467932331689707902171036184370272051185812840501418184727075932174000060832267715242764608";
randTable = [];
for (var i = 0; i<1000; i++) randTable.push(parseFloat(randString.substr(i,1)));