xxxxxxxxxx
117
function windowResized(){
resizeCanvas(windowWidth, windowHeight);
}
let audioInit = false
function mouseClicked() {
if(!audioInit){
userStartAudio();
getAudioContext().resume();
audioInit = true
}
}
let ship
function setup() {
createCanvas(windowWidth, windowHeight);
// INITIAL TESTING CODE
//let ship = new Sprite(50,50,30,"triangle")
}
let px = 200
let py = 400
let checkLines = [
[200,100],[300,200],
[300,200],[200,300],
[200,300],[100,200],
[100,200],[200,100]
]
// TODO: make objects that are connected to
function getWorldCollisions(pos1,pos2){
let results = []
for(let i = 0; i < checkLines.length/2; i++){
result = intersectPoints(pos1,pos2,checkLines[i*2],checkLines[i*2+1])
if(result){
results.push(result)
}
}
let sorted = false
if(results.length < 2) return results
let i = 0
// preform bubble sort
while(!sorted){
if(getMagnitude(subtractVectors(results[i],pos1)) > getMagnitude(subtractVectors(results[i+1],pos1)) && i < results.length - 1){
let tempVal = results[i+1]
results[i+1] = results[i]
results[i] = tempVal
i = 0
}
i++
if(i >= results.length - 1) sorted = true
}
return results
}
function drawCollisionLines(){
for(let i = 0; i < checkLines.length/2; i++){
line(checkLines[i*2][0],checkLines[i*2][1], checkLines[i*2+1][0],checkLines[i*2+1][1])
}
}
function draw() {
background(bgCol);
dt = round(deltaTime/1000,3)
// INITIAL TESTING CODE
if(keyIsDown(87)){ // w
py -= 100 * dt
}
if(keyIsDown(83)){ // s
py += 100 * dt
}
if(keyIsDown(65)){ // a
px -= 100 * dt
}
if(keyIsDown(68)){ // d
px += 100 * dt
}
stroke(255)
drawCollisionLines()
let result = getWorldCollisions([px,py],[mouseX,mouseY])
if(result.length > 0){
line(px,py,result[0][0],result[0][1])
}else{
line(px,py,mouseX, mouseY)
}
}