/** * Spaceman * By Raphael Rouvinov-Kats * * Written for Ludum Dare 29- theme: "Beneath the Surface" * * Your name is Captain Tom * You are an astronaut disembarking through your capsule * And you decide to float in a most peculiar way * Toward the icelandic volcanoe of Snæfellsjökull * Where you will barrel straight into the center of the Earth * You take a deep breath, kill your circuits, and go * * To accomplish your task you'll have to barrel into the mountain: * 1) Without dying. This means you'll have to keep your temperature down. * The mountain's melting icecaps will give you a nice cushion to fall onto, * so don't worry about that. * 2) At a fast speed. Like killer fast. From where you're jumping going fast isn't * a problem. Maintaining your speed is. As you're falling you'll have to pick up * the space debris coming from behind you because it'll increase your momentum. * You'll also want to avoid space debris coming toward you- it'll slow you down. * Gravity wells are another issue. They * */ // lets me have misorder, e.g. player=new Player() before var Player=function() // aka letting me use things before they're defined /*jslint latedef:false*/ frameRate(0); var accel = 9.8; // acceleration of earth: 9.8 m/s/s, adjusted for framerate in code var startHeight = 370e3; // ISS height in meters var mesosphereTop = 80e3; // mesosphere max height var stratosphereTop = 80e3; // stratosphere max height var tropophereTop = 80e3; // troposphere max height // let the game play at frameRate(0) without making it speed up or slow down var lastFrameMillis = millis(); var millisSinceLastFrame = 1; var defaultFont = createFont("sans-serif", 14); var HUDFont = createFont("Impact", 24); var MMFont = createFont("Tahoma", 24); var forEach = function(arr, action) { for(var i = 0; i < arr.length; i++) { action(arr[i]); } }; var Player = function() { var p = new PVector(200, 100); // position on screen var v = new PVector(0, 1); // velocity of player on screen var pos = new PVector(0, startHeight); // position relative to ground var vel = new PVector(0, -30); // velocity var r = 0; // rotation of player var temp = 120; // degrees Celsius of player this.update = function() { p.add(v); // move on screen p.x = max(0, min(400, p.x)); // x bounds checking p.y = max(0, min(400, p.y)); // y bounds checking var velTotal = vel.get(); velTotal.add(v); // total velocity r = 90 + velTotal.heading(); // rotation v.mult(0.8); // decelerate on screen var timeAdjust = millisSinceLastFrame / 1e3; // seconds that have gone by var a = accel * timeAdjust; // adjust accel by framerate vel.add(0, a); // accelerate toward earth var dy = vel.get(); dy.mult(timeAdjust); pos.add(dy); // move toward earth, frate adjusted temp = 0.4636363636 * abs(vel.y) + 120; }; this.draw = function() { pushMatrix(); translate(p.x, p.y); rotate(r); stroke(0, 0, 0); fill(255, 255, 255); rect(-7, -7, 14, 20); // backpack ellipse(0.5, 0, 10, 10); // head rect(-5, 5, 10, 18); // torso rect(-7, 11, 3, 7); // left pack control rect(4, 11, 3, 7); // right pack control rect(-7, 5, 3, 9); // left arm rect(4, 5, 3, 9); // right arm rect(-5, 23, 4, 13); // left leg rect(1, 23, 4, 13); // right leg fill(105, 94, 77); ellipse(0.5, 0, 10, 5); // head stroke(255, 0, 0); line(-5, 28, -1, 28); // left leg red stripe line(1, 28, 6, 28); // right leg red stripe // american flag noStroke(); fill(255, 0, 0); rect(6, 7, 1, 1); rect(6, 8, 1, 1); rect(5, 8, 1, 1); fill(17, 0, 255); rect(5, 7, 1, 1); fill(138, 104, 0); textFont(defaultFont); text("▒", -4, 16); // torso texture popMatrix(); }; /** * @return screen position of player * */ this.getP = function() { }; /** * set screen velocity of player * */ this.setV = function(dx, dy) { v.set(dx, dy); }; /** * @return velocity of player * */ this.getVel = function() { return vel; }; /** * @return position of player * */ this.getPos = function() { return pos; }; this.getTemp = function() { return temp; }; }; var BG = function() { var particles = []; for(var i = 0; i < 100; i++) { particles[i] = [random(0, 400), random(0, 400)]; } this.update = function() { for(var i = 0; i < particles.length; i++) { var timeAdjust = millisSinceLastFrame / 1e3; particles[i][0] += 0; particles[i][1] -= random(0, player.getVel().y*timeAdjust/5); while(particles[i][1] < 0 || particles[i][1] >= 400 || particles[i][0] < 0 || particles[i][1] >= 400) { var playerVel = player.getVel().y; if(playerVel >= 0) { particles[i] = [random(0, 400), random(399, 400)]; } else { particles[i] = [random(0, 400), random(0, 1)]; } } } }; this.drawSpaceStation = function(playerH) { // draw space station if close var dist = playerH - startHeight; if(abs(dist) < 800) { pushMatrix(); translate(200, dist); noStroke(); fill(184, 104, 33); rect(-50, 130, 70, 10); // plank popMatrix(); } }; this.draw = function() { var playerH = floor(startHeight*2 - player.getPos().y); // draw background color colorMode(HSB); fill(140, 255, 255 - playerH/startHeight*255); noStroke(); rect(0, 0, 400, 400); colorMode(RGB); // draw stars stroke(255, 255, 255, playerH/startHeight*255); for(var i = 0; i < particles.length; i++) { point(particles[i][0], particles[i][1]); } // draw space station this.drawSpaceStation(playerH); // draw Earth fill(67, 115, 186); stroke(189, 156, 23, 10); ellipse(200, 800- (1-playerH/startHeight)*400, 850+(1-playerH/startHeight)*3000, 850); }; }; var HUD = function() { this.drawHeight = function() { var pH = floor(startHeight*2 - player.getPos().y); colorMode(HSB); fill(60, 255 - pH/startHeight*255, 255, 200); textFont(HUDFont); text(ceil(pH/1e3) + " km", 20, 50); // height colorMode(RGB); }; this.drawVelocity = function() { var pV = player.getVel().y; colorMode(HSB); fill(160, pV/2000*255, 255, 200); text(floor(pV) + " m/s", 20, 84); // vel colorMode(RGB); }; this.drawTemp = function() { var temp = player.getTemp(); colorMode(HSB); fill(0, temp/1700*255, 255, 200 + temp/1700*55); text(floor(temp) + "°C", 20, 110); // temp colorMode(RGB); }; this.draw = function() { this.drawHeight(); this.drawVelocity(); this.drawTemp(); }; }; var GravityWell = function(pos, vel) { this.pos = pos; // screen position of debris this.vel = vel; // screen velocity of debris this.getVel = function() { return vel; }; this.getPos = function() { return pos; }; this.draw = function() { }; }; var spaceDebris = function(pos, vel) { this.pos = pos; // screen position of debris this.vel = vel; // screen velocity of debris this.getVel = function() { return vel; }; this.getPos = function() { return pos; }; this.draw = function() { }; }; var cloud = function(pos, vel) { this.pos = pos; // screen position of debris this.vel = vel; // screen velocity of debris this.getVel = function() { return vel; }; this.getPos = function() { return pos; }; this.draw = function() { }; }; var Environment = function() { var debris = []; var gwells = []; var clouds = []; this.update = function() { // add debris // add wells // check for debris collisions // check for well collisions // check if player is hitting a }; this.draw = function() { // draw wells forEach(gwells, draw); // draw debris forEach(debris, draw); // draw clouds forEach(clouds, draw); }; }; var MainMenu = function() { var countdown = -999; this.update = function() { if(countdown === -999) { if(mouseIsPressed) { countdown = 255; } } else { if(--countdown < 0) { inMM = false; } } }; this.draw = function() { fill(255, 255, 255); if(countdown !== -999) { fill(255, 255, 255, countdown); } textFont(MMFont, 72); text("Spaceman", 40, 60); textFont(MMFont, 24); text("Click to jump", 140, 360); }; }; var player = new Player(); var bg = new BG(); var hud = new HUD(); var env = new Environment(); var mm = new MainMenu(); var inMM = true; var draw = function() { if(inMM) { mm.update(); bg.draw(); player.draw(); mm.draw(); } else { millisSinceLastFrame = millis() - lastFrameMillis; lastFrameMillis = millis(); bg.update(); env.update(); player.update(); bg.draw(); env.draw(); player.draw(); hud.draw(); } }; var mouseDragged = function() { var dx = mouseX - pmouseX; var dy = mouseY - pmouseY; var dScale = 3; player.setV(dx * dScale, dy * dScale); };