123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- /*
- otter floating animation
- rocks, fish, seaweed, etc
- extra leaves on the kelp bases for decoration/deterioration
- menu
- assets:
- fish
- urchins
- decorations
- menus and gfx
- */
- var images = {};
- var img_to_load = {
- otter_swimming: 'art/otter-swimming.png',
- otter_floating: 'art/otter-floating.png',
- otter_eating: 'art/otter-eating.png',
- kelp_icon: 'art/kelp-icon.png',
- bubble_icon: 'art/bubble-breath.png',
- sea_urchin_icon: 'art/sea-urchin-icon.png',
- heart: 'art/heart.png',
- bubble: 'art/bubble-effect.png',
- holdfast1_1: 'art/holdfast1-1.png',
- holdfast1_2: 'art/holdfast1-2.png',
- holdfast1_3: 'art/holdfast1-3.png',
- holdfast1_4: 'art/holdfast1-4.png',
- holdfast1_5: 'art/holdfast1-5.png',
- holdfast2_1: 'art/holdfast2-1.png',
- holdfast2_2: 'art/holdfast2-2.png',
- holdfast2_3: 'art/holdfast2-3.png',
- holdfast2_4: 'art/holdfast2-4.png',
- holdfast2_5: 'art/holdfast2-5.png',
- holdfast3_1: 'art/holdfast3-1.png',
- holdfast3_2: 'art/holdfast3-2.png',
- holdfast3_3: 'art/holdfast3-3.png',
- holdfast3_4: 'art/holdfast3-4.png',
- holdfast3_5: 'art/holdfast3-5.png',
- }
- var _inputdata = {
- pressed: {},
- down: {},
- };
- $(document).ready(function() {
-
-
- var imgs = Object.keys(img_to_load).length;
- var maxImgs = imgs;
- function loadcb() {
- imgs--;
- $('.loading-progress').css({width: ((maxImgs-imgs)*100)/maxImgs + '%'});
- if(imgs == 0) {
- // $('.loading').hide();
- $('.loading, .loadbar').hide();
- $('.play').show();
- waitingOnUser = true;
- }
- }
-
- for(var k in img_to_load) {
- images[k] = new Image();
- images[k].onload = loadcb;
- images[k].src = img_to_load[k];
- }
-
- // init input stuff
- $(document).keydown(function(e) {
- _inputdata.down[e.which] = 1;
- _inputdata.down[e.which] |= 0;
- e.preventDefault();
- });
- $(document).keyup(function(e) {
- _inputdata.down[e.which] = 0;
- _inputdata.pressed[e.which] = (_inputdata.pressed[e.which]>>>0) + 1;
- e.preventDefault();
- });
-
-
- });
- var extStop = false;
- var extCallback = function() {};
- function newgame(gm) {
-
- extCallback = function() {
- game_lvl = gm;
- extStop = false;
- extCallback = function() {};
- game(game_lvl);
- };
- extStop = true;
- }
-
- function game(hardness) {
-
- hardness = hardness || 5;
-
- var runGame = true;
-
- var w = $(window);
-
- var canvas = $('.scribble-canvas');
-
- var ctx = canvas[0].getContext("2d");
-
-
- ctx.drawBox = function(x,y, style, width) {
- this.beginPath();
- this.rect(x - 3, y - 3, 6, 6);
- if(!(width > 1)) width = 1;
- this.lineWidth = width;
- this.strokeStyle = style;
- this.stroke();
- }
-
-
-
-
- function checkinputs() {
- var curinput = {
- down: _inputdata.down,
- pressed: _inputdata.pressed,
- };
-
- _inputdata.pressed = {};
- // pressed: {},
- // down: {},
- // };
- return curinput;
- }
-
-
-
- // w.resize(function(){
- // canvas.attr({width: w.width(), height: w.height()});
- // });
- // w.trigger('resize');
- //
- window.requestAnimFrame = (function(){
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function(callback){
- window.setTimeout(callback, 1000 / 30);
- };
- })();
-
- function frame(info){
-
- checkVictory();
- checkDefeat();
- checkInterrupt();
- // info = info || {};
-
- //info.ii
- var lol = checkinputs();
- // info.ii = lol;
- // console.log(lol);
- // // console.log(info.ii);
- draw(info, lol);
-
- if(runGame) window.requestAnimFrame(frame);
- }
-
-
- var gameinfo = {
- numUrchins: 1 + hardness,
- numKelps: (3 + hardness * 1.5)|0,
- };
-
- var hud = new HUD();
-
- var collisions = new Collisions();
-
- var splashText = new SplashText();
- var waves = new Waves();
- var mud = new Mud();
- var otters = [];
- var urchins = [];
- var kelps = [];
- var seaweeds = [];
-
- seaweeds.push( new Seaweed({}) );
- otters.push( new Otter({}) );
-
-
- for(var i = 0; i < gameinfo.numUrchins; i++) {
- urchins.push( new Urchin({position: pt(Math.random() * 900 + 50, 200)}) );
- }
- for(var i = 0; i < gameinfo.numKelps; i++) {
- kelps.push( new Kelp({position: pt(Math.random() * 900 + 50, 428)}) );
- }
-
- // boxen.push(new RoundedBox({}));
-
- collisions.subtractStaticRect(rect(0,450,0,1000));
-
- collisions.addActor(urchins);
- collisions.addActor(seaweeds);
- collisions.addActor(otters);
- collisions.addActor(kelps);
-
- hud.addWidget(new HUDHearts(otters[0]), 990 - (6*25), 20);
- hud.addWidget(new HUDBubbles(otters[0]), 10, 20);
- hud.addWidget(new HUDList({
- list: kelps,
- icon: images.kelp_icon,
- textOffset: pt(2, 35),
- imgScale: .7,
- }), 750, 10);
- hud.addWidget(new HUDList({
- list: urchins,
- icon: images.sea_urchin_icon,
- textOffset: pt(5, 30),
- imgScale: 1,
- }), 650, 15);
-
- var d = new Date();
- var lastframe = d.getTime();
-
-
- frame();
-
-
- function draw(info, ii) {
- var d = new Date();
- var now = d.getTime();
-
- // time elapsed (since last frame)
- var te = (now - lastframe) / 1000.0;
- lastframe = now;
-
- var w = ctx.canvas.width;
- var h = ctx.canvas.height;
- ctx.clearRect(0, 0, w, h);
-
- // console.log(ii.pressed);
-
- otters[0].uiDirection.x = 0;
- otters[0].uiDirection.y = 0;
- otters[0].uiAcc = false;
- if(ii.down[37]) {
- //console.log(ii);
- otters[0].uiDirection.x = -1;
- otters[0].uiAcc = true;
- }
- if(ii.down[39]) {
- //console.log(ii);
- otters[0].uiDirection.x = 1;
- otters[0].uiAcc = true;
- }
- if(ii.down[38]) {
- //console.log(ii);
- otters[0].uiDirection.y = -1;
- otters[0].uiAcc = true;
- }
- if(ii.down[40]) {
- //console.log(ii);
- otters[0].uiDirection.y = 1;
- otters[0].uiAcc = true;
- }
- if(ii.pressed[32]) {
- //console.log(ii);
- otters[0].takeBite();
- // otters[0].dropItem();
- }
- if(ii.down[32]) {
- //console.log(ii);
- otters[0].mouthOpen = true;
- // otters[0].dropItem();
- }
- else {
- otters[0].mouthOpen = false;
- }
-
-
- splashText.frameMove(te);
- waves.frameMove(te);
- mud.frameMove(te);
-
- // other shit here
- kelps.map(function(q) { q.frameMove(te) });
- urchins.map(function(q) { q.frameMove(te) });
- otters.map(function(q) { q.frameMove(te) });
- seaweeds.map(function(q) { q.frameMove(te) });
-
- collisions.checkActorCollisions();
- collisions.checkActorBounds();
- collisions.frameMove(te);
-
- hud.frameMove(te);
-
- splashText.render(ctx);
- waves.render(ctx);
- mud.render(ctx);
-
- // other shit here
- seaweeds.map(function(q) { q.render(ctx) });
- kelps.map(function(q) { q.render(ctx) });
- urchins.map(function(q) { q.render(ctx) });
- otters.map(function(q) { q.render(ctx) });
-
- hud.render(ctx);
- // console.log('lol');
- }
-
- function distance(from, to) {
- var x = to.x - from.x;
- var y = to.y - from.y;
- return Math.sqrt(x*x + y*y);
- }
-
-
-
- function checkVictory() {
- var urchinsAlive = 0;
- for(var i = 0; i < urchins.length; i++) {
- if(urchins[i].hp > 0) urchinsAlive++;
- }
- if(urchinsAlive == 0) win();
- }
-
-
- function checkDefeat() {
- var kelpsAlive = 0;
- for(var i = 0; i < kelps.length; i++) {
- if(kelps[i].hp > 0) kelpsAlive++;
- }
-
- var otterDead = false;
- if(otters[0].hp == 0) otterDead = true;
-
- if(otterDead || kelpsAlive == 0) lose();
- }
- function checkInterrupt() {
- if(extStop) {
- runGame = false;
- setTimeout(extCallback, 100);
- }
- }
-
-
- function win() {
- runGame = false;
- game_lvl++;
- waitingOnUser = true;
- $('.victory').show();
- }
-
- function lose() {
- runGame = false;
- waitingOnUser = true;
- $('.defeat').show();
- }
-
-
-
- };
|