Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var planets = [];
- var asteroids = [];
- var canvas;
- var ctx;
- function spaceship(){
- this.location = new vector(500, 500);
- this.velocity = new vector(0, 0);
- this.power = 0.1;
- this.draw = function(){
- ctx.rect(this.location.x-10,this.location.y-10,20,20);
- ctx.stroke();
- }
- this.up = function(){
- this.velocity = this.velocity.add(new vector(0, -this.power));
- }
- this.down = function(){
- this.velocity = this.velocity.add(new vector(0, this.power));
- }
- this.left = function(){
- this.velocity = this.velocity.add(new vector(-this.power, 0));
- }
- this.right = function(){
- this.velocity = this.velocity.add(new vector(this.power, 0));
- }
- }
- var ss = new spaceship();
- function vector(x, y) {
- //konstruuje wektor
- this.x = x;
- this.y = y;
- this.add = function(v){
- return new vector(this.x+v.x, this.y+v.y);
- }
- this.sub = function(v){
- return new vector(this.x-v.x, this.y-v.y);
- }
- this.div = function(n){
- return
- }
- this.dist = function(v){
- dx = this.x - v.x;
- dy = this.y - v.y;
- return Math.sqrt(dx*dx+dy*dy);
- }
- }
- function planet(location, size){
- //konstruuje planetę
- this.location = location;
- this.size = size;
- this.mass = size;
- this.dist = function(v){
- return this.location.dist(v);
- }
- this.gravity = function(point){
- var tmp = this.location.sub(point);
- tmp.x = (this.mass/(tmp.x))/10000;
- tmp.y = (this.mass/(tmp.y))/10000;
- return tmp;
- }
- this.draw = function(){
- centerX = this.location.x;
- centerY = this.location.y;
- radius = this.size;
- ctx.beginPath();
- ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
- ctx.fillStyle = 'green';
- ctx.fill();
- ctx.lineWidth = 5;
- ctx.strokeStyle = '#003300';
- ctx.stroke();
- }
- }
- function asteroid(location, velocity){
- //konstruuje planetę
- this.location = location;
- this.velocity = velocity;
- this.draw = function(){
- centerX = this.location.x;
- centerY = this.location.y;
- radius = 5;
- ctx.beginPath();
- ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
- ctx.fillStyle = 'red';
- ctx.fill();
- ctx.lineWidth = 5;
- ctx.strokeStyle = '#003300';
- ctx.stroke();
- }
- }
- function tick(){
- //wykonuje sie co klatkę
- for (var i = asteroids.length - 1; i >= 0; i--) {
- asteroids[i].location = asteroids[i].location.add(asteroids[i].velocity);
- ss.location = ss.location.add(ss.velocity);
- for (var j = planets.length - 1; j >= 0; j--) {
- asteroids[i].velocity = asteroids[i].velocity.add(planets[j].gravity(asteroids[i].location));
- //ss.velocity = ss.velocity.add(planets[j].gravity(ss.location));
- ctx.moveTo(500,500);
- var tmp = planets[j].gravity(ss.location);
- ctx.lineTo(500+tmp.x*10000000,500+tmp.y*10000000);
- ctx.stroke();
- };
- };
- //zmień prędkość statku
- }
- function draw(){
- //zaktualizuj canvas
- ctx.clearRect(0,0, window.innerWidth, window.innerHeight);
- ss.draw();
- for (var i = planets.length - 1; i >= 0; i--) {
- planets[i].draw();
- };
- for (var i = asteroids.length - 1; i >= 0; i--) {
- asteroids[i].draw();
- };
- }
- $(function() {
- //po załadowaniu
- $("body").keydown(function(e){
- //alert(e.keyCode);
- var k = e.keyCode;
- if(k == 38)
- ss.up();
- if(k == 40)
- ss.down();
- if(k == 37)
- ss.left();
- if(k == 39)
- ss.right();
- });
- canvas = document.getElementById("board");
- ctx = canvas.getContext("2d");
- ctx.canvas.width = window.innerWidth-20;
- ctx.canvas.height = window.innerHeight-20;
- //planets.push(new planet(new vector(50, 50), 10));
- planets.push(new planet(new vector(750, 500), 1));
- // planets.push(new planet(new vector(300, 100), 10));
- // planets.push(new planet(new vector(100, 355), 10));
- // planets.push(new planet(new vector(70, 70), 10));
- for(var i=0; i<10; i++){
- var location = new vector(Math.random()*1000, Math.random()*1000);
- var velocity = new vector((Math.random()-0.5)*1, (Math.random()-0.5)*1);
- asteroids.push(new asteroid(location, velocity));
- }
- var timer = setInterval(function () {
- tick();
- draw();
- }, 1000 / 60); // 60FPS
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement