/
asteroids.js
106 lines (83 loc) · 2.91 KB
/
asteroids.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var Asteroids = function(settings) {
// Asteroids general settings
var asteroidsElement = null;
var asteroidsCreated = false;
var asteroidsPosition = {};
var astAngle = 100 * Math.random() + "deg";
var astSpeed = 2;
// SPAWN asteroids
var spawnedAsteroids = [];
this.getElement = function() {
return asteroidsElement;
}
function createAsteroid() {
var space = document.getElementsByClassName('startpagebackground')[0]
asteroidsElement = document.createElement("div");
asteroidsElement.className = "astNew";
// Asteroid random spawned position
asteroidsElement.style.top = parseInt(300 * Math.random(500)) + "px";
asteroidsElement.style.left = parseInt(1000 * Math.random(500)) + "px";
// Asteroid random angle
asteroidsElement.style.transform = 'rotate(' + (parseInt(Math.random() * 100)) + 'deg)';
// Append new asteroid div to Body
space.appendChild(asteroidsElement)
}
// function getRandomInt(min, max) {
// min = Math.ceil(min);
// max = Math.floor(max);
// return Math.floor(Math.random() * (max - min)) + min;
// }
function toRadians(angle) {
return angle * (Math.PI / 180);
}
function astWall() {
var asRect = asteroidsElement.getBoundingClientRect();
var w = parseInt(window.innerWidth);
var h = parseInt(window.innerHeight);
if (asRect.bottom > h) {
asteroidsElement.style.top = (h - asRect.height) + 'px';
}
if (asRect.top < 0) {
asteroidsElement.style.top = '0px';
}
if (asRect.left < 0) {
asteroidsElement.style.left = '0px';
}
if (asRect.right > w) {
asteroidsElement.style.left = (w - asRect.width) + 'px';
}
// BOUNCE CHECK
// if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
// astSpeed = -2;
if (asRect.top > window.innerHeight || asRect.top < 0) {
asteroidsElement.style.top = parseInt(asteroidsElement.style.top) + Math.cos(toRadians(parseInt(astAngle - 180))) * (astSpeed) + "px";
}
// // }
// // if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
// // astSpeed = -2;
if (asRect.left < window.innerWidth || asRect.left) {
asteroidsElement.style.left = parseInt(asteroidsElement.style.left) + Math.sin(toRadians(parseInt(astAngle - 180))) * (astSpeed) + "px";
}
// }
}
function move() {
// Asteroid movement based on random angle
asteroidsElement.style.top = parseInt(asteroidsElement.style.top) + Math.cos(toRadians(parseInt(astAngle))) * astSpeed + "px";
asteroidsElement.style.left = parseInt(asteroidsElement.style.left) + Math.sin(toRadians(parseInt(astAngle))) * astSpeed + "px";
astWall();
}
function create() {
// Create the object asset
}
this.render = function(interactions) {
move();
}
function init() {
createAsteroid();
}
// this.render = function(interactions) {
// move(interactions);
//
// }
init();
}