2017-06-18 16:33:49 +00:00
|
|
|
<!--Copyright (c) 2017 Copyright Cliffbreak.de All Rights Reserved.-->
|
2017-06-18 14:46:11 +00:00
|
|
|
<head>
|
2017-06-18 16:44:10 +00:00
|
|
|
<title>⛳ Minigolf</title>
|
2017-06-18 16:33:49 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="include/style.css">
|
|
|
|
<script src="include/jquery-3.2.1.min.js"></script>
|
2017-06-18 14:46:11 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
2017-06-18 16:33:49 +00:00
|
|
|
<canvas width="1280" height="720"></canvas>
|
|
|
|
<canvas width="1280" height="720"></canvas>
|
2017-06-18 14:46:11 +00:00
|
|
|
</body>
|
2017-06-18 16:33:49 +00:00
|
|
|
|
|
|
|
<script>
|
|
|
|
var ctx,
|
|
|
|
staticctx,
|
|
|
|
bX,
|
|
|
|
bY,
|
2017-06-18 22:55:08 +00:00
|
|
|
bWidth = 10,
|
2017-06-18 16:33:49 +00:00
|
|
|
pX,
|
2017-06-18 20:17:35 +00:00
|
|
|
pY,
|
2017-06-18 22:55:08 +00:00
|
|
|
bVec,
|
|
|
|
borders = []
|
2017-06-18 16:33:49 +00:00
|
|
|
|
|
|
|
/************************/
|
|
|
|
/*** _GAME VARIABLES_ ***/
|
|
|
|
/***********************/
|
|
|
|
var _COLOR_GRASS = '#388E3C',
|
|
|
|
_COLOR_BORDER = '#FF9800',
|
|
|
|
_COLOR_BALL = '#FFFFFF',
|
|
|
|
_COLOR_HOLE = '#000000',
|
|
|
|
_COLOR_BRACKET = '#000000'
|
|
|
|
|
2017-06-18 19:50:42 +00:00
|
|
|
|
2017-06-18 20:17:35 +00:00
|
|
|
var _MAX_BRACKET = 100,
|
|
|
|
_BALL_SPEED = 50,
|
|
|
|
_BALL_SPEED_THRESHOLD = 10
|
2017-06-18 19:50:42 +00:00
|
|
|
|
2017-06-18 16:33:49 +00:00
|
|
|
$(function(){
|
|
|
|
ctx = $('canvas')[0].getContext('2d')
|
|
|
|
staticctx = $('canvas')[1].getContext('2d')
|
|
|
|
init()
|
|
|
|
drawBorder(23, 10, 500, 5)
|
|
|
|
drawBorder(23, 10, 5, 500)
|
2017-06-18 22:55:08 +00:00
|
|
|
drawBorder(23+200, 10+70, 50, 50)
|
2017-06-18 16:33:49 +00:00
|
|
|
drawBorder(23, 500+10-5, 500, 5)
|
|
|
|
drawBorder(500+23-5, 10, 5, 500)
|
|
|
|
drawHole(432, 366)
|
|
|
|
setInterval(animLoop, 10)
|
|
|
|
|
|
|
|
$(document).mousemove(function(e){
|
|
|
|
pX = e.pageX - $('canvas').offset().left
|
|
|
|
pY = e.pageY - $('canvas').offset().top
|
2017-06-18 19:50:42 +00:00
|
|
|
})
|
2017-06-18 22:55:08 +00:00
|
|
|
|
2017-06-18 19:50:42 +00:00
|
|
|
$(document).mousedown(function(e){
|
2017-06-18 20:17:35 +00:00
|
|
|
var strength = Math.hypot(pX - bX, pY - bY)
|
|
|
|
if(strength > _MAX_BRACKET)
|
|
|
|
strength = _MAX_BRACKET
|
|
|
|
angle = Math.atan2(pY - bY, pX - bX)
|
|
|
|
x = bX+strength*Math.cos(angle)-_MAX_BRACKET
|
|
|
|
y = bY+strength*Math.sin(angle)-_MAX_BRACKET
|
|
|
|
if(Math.abs(x) < _BALL_SPEED_THRESHOLD){
|
|
|
|
if(x < 0)
|
|
|
|
x = -_BALL_SPEED_THRESHOLD
|
|
|
|
if(x > 0)
|
|
|
|
x = +_BALL_SPEED_THRESHOLD
|
|
|
|
}
|
|
|
|
if(Math.abs(y) < _BALL_SPEED_THRESHOLD){
|
|
|
|
if(y < 0)
|
|
|
|
y = -_BALL_SPEED_THRESHOLD
|
|
|
|
if(y > 0)
|
|
|
|
y = +_BALL_SPEED_THRESHOLD
|
|
|
|
}
|
|
|
|
console.log(x + '|' + y);
|
|
|
|
if(bVec == null)
|
|
|
|
bVec = [x/_BALL_SPEED, y/_BALL_SPEED]
|
2017-06-18 19:50:42 +00:00
|
|
|
})
|
2017-06-18 16:33:49 +00:00
|
|
|
})
|
|
|
|
|
2017-06-18 22:55:08 +00:00
|
|
|
// var i = 0
|
2017-06-18 16:33:49 +00:00
|
|
|
function animLoop() {
|
|
|
|
ctx.clearRect(0, 0, $('canvas').width(), $('canvas').height());
|
2017-06-18 22:55:08 +00:00
|
|
|
|
|
|
|
if(bVec != null){
|
|
|
|
collide()
|
|
|
|
drawBall(bX = bX-bVec[0], bY = bY-bVec[1])
|
|
|
|
}
|
2017-06-18 20:17:35 +00:00
|
|
|
else
|
|
|
|
drawBall(bX, bY)
|
2017-06-18 16:33:49 +00:00
|
|
|
drawBracket()
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
bX = bY = 100
|
|
|
|
staticctx.fillStyle = _COLOR_GRASS
|
2017-06-18 22:55:08 +00:00
|
|
|
staticctx.fillRect(23, 10, 500, 500)
|
2017-06-18 16:33:49 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 22:55:08 +00:00
|
|
|
function drawBorder(x, y, width, height) {
|
2017-06-18 16:33:49 +00:00
|
|
|
staticctx.fillStyle = _COLOR_BORDER
|
2017-06-18 22:55:08 +00:00
|
|
|
staticctx.fillRect(x, y, width, height)
|
|
|
|
borders.push(new Border(x, y, width, height))
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawDebugBorder(border) {
|
|
|
|
staticctx.fillStyle = 'red'
|
|
|
|
staticctx.fillRect(border.x, border.y, border.width, border.height)
|
2017-06-18 16:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function drawHole(x, y) {
|
|
|
|
staticctx.beginPath()
|
|
|
|
staticctx.arc(x, y, 20, 0, Math.PI*2)
|
|
|
|
staticctx.fillStyle = _COLOR_HOLE
|
|
|
|
staticctx.fill()
|
|
|
|
staticctx.closePath()
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawBall(x, y) {
|
|
|
|
ctx.beginPath()
|
2017-06-18 22:55:08 +00:00
|
|
|
ctx.arc(x, y, bWidth, 0, Math.PI*2)
|
2017-06-18 16:33:49 +00:00
|
|
|
ctx.fillStyle = _COLOR_BALL
|
|
|
|
ctx.fill()
|
|
|
|
ctx.closePath()
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawBracket() {
|
2017-06-18 20:20:12 +00:00
|
|
|
if(bVec != null) return;
|
2017-06-18 19:50:42 +00:00
|
|
|
var x = pX,
|
|
|
|
y = pY
|
|
|
|
if(Math.hypot(pX - bX, pY - bY) > _MAX_BRACKET){
|
|
|
|
angle = Math.atan2(pY - bY, pX - bX)
|
|
|
|
x = bX+_MAX_BRACKET*Math.cos(angle)
|
|
|
|
y = bY+_MAX_BRACKET*Math.sin(angle)
|
|
|
|
}
|
|
|
|
|
2017-06-18 16:33:49 +00:00
|
|
|
ctx.beginPath()
|
|
|
|
ctx.moveTo(bX, bY)
|
2017-06-18 19:50:42 +00:00
|
|
|
ctx.lineTo(x, y)
|
2017-06-18 16:33:49 +00:00
|
|
|
ctx.strokeStyle = _COLOR_BRACKET
|
|
|
|
ctx.stroke()
|
|
|
|
ctx.closePath()
|
|
|
|
}
|
2017-06-18 22:55:08 +00:00
|
|
|
|
|
|
|
function collide() {
|
|
|
|
borders.forEach(function(el){
|
|
|
|
// if(bX >= el.x && bX <= el.x+el.width && bY >= el.y && bY <= el.y+el.height){
|
|
|
|
// drawDebugBorder(el)
|
|
|
|
// if(Math.round(bX) == el.x || Math.round(bX) == el.x+el.width){
|
|
|
|
// bVec = [-bVec[0], bVec[1]]
|
|
|
|
// }
|
|
|
|
// else if(Math.round(bY) == el.y || Math.round(bY) == el.y+el.height)
|
|
|
|
// bVec = [bVec[0], -bVec[1]]
|
|
|
|
// else {
|
|
|
|
// console.error('Error during collide()')
|
|
|
|
// console.log(el)
|
|
|
|
// console.log(Math.round(bY));
|
|
|
|
// console.log(Math.round(bX));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
var dx = (el.x+el.width/2)-(bX+bWidth/2)
|
|
|
|
var dy = (el.y+el.height/2)-(bY+bWidth/2)
|
|
|
|
var width = (el.width+bWidth)/2
|
|
|
|
var height = (el.height+bWidth)/2
|
|
|
|
var crossWidth = width*dy;
|
|
|
|
var crossHeight = height*dx;
|
|
|
|
var collision = 'none';
|
|
|
|
if(Math.abs(dx)<=width && Math.abs(dy)<=height){
|
|
|
|
if(crossWidth<crossHeight){
|
|
|
|
collision=(crossWidth<(-crossHeight))?'bottom':'left'
|
|
|
|
} else {
|
|
|
|
collision=(crossWidth<(-crossHeight))?'right':'top'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(collision != 'none') console.log(collision);
|
|
|
|
switch (collision) {
|
|
|
|
case 'bottom':
|
|
|
|
case 'top': bVec = [bVec[0], -bVec[1]]; break
|
|
|
|
case 'left':
|
|
|
|
case 'right': bVec = [-bVec[0], bVec[1]]; break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/** CLASSES **/
|
|
|
|
function Border(x, y, width, height) {
|
|
|
|
this.x = x
|
|
|
|
this.y = y
|
|
|
|
this.width = width
|
|
|
|
this.height = height
|
|
|
|
}
|
2017-06-18 16:33:49 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script src="http://localhost:35729/livereload.js"></script>
|