ol-games/examples/map.sprite.collision.html
2024-02-10 11:55:01 +01:00

234 lines
7.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<!--
Copyright (c) 2016 Jean-Marc VIGLINO,
released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
<title>ol-games: Sprite collision</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Sprite collision for openlayers games" />
<meta name="keywords" content="ol, openlayers, game, sprite, collision" />
<link rel="stylesheet" href="style.css" />
<!-- jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<!-- FontAwesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- OpenLayers -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
<!-- ol-ext -->
<link rel="stylesheet" href="https://rawgit.com/Viglino/ol-ext/master/dist/ol-ext.css" />
<script type="text/javascript" src="https://rawgit.com/Viglino/ol-ext/master/dist/ol-ext.js"></script>
<!-- ol-games -->
<link rel="stylesheet" href="../dist/ol-games.css" />
<script type="text/javascript" src="../dist/ol-games.js"></script>
<style>
#map {
width:800px;
height:600px;
float: left;
}
#pattern canvas,
#clayer canvas {
border: 5px solid #369;
vertical-align: middle;
background-image:
-moz-linear-gradient(45deg, rgba(0,0,0,0.05) 25%, transparent 25%),
-moz-linear-gradient(-45deg, rgba(0,0,0,0.05) 25%, transparent 25%),
-moz-linear-gradient(45deg, transparent 75%, rgba(0,0,0,0.05) 75%),
-moz-linear-gradient(-45deg, transparent 75%, rgba(0,0,0,0.05) 75%);
background-image:
-webkit-gradient(linear, 0 100%, 100% 0, color-stop(.25, rgba(0,0,0,0.05)), color-stop(.25, transparent)),
-webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, rgba(0,0,0,0.05)), color-stop(.25, transparent)),
-webkit-gradient(linear, 0 100%, 100% 0, color-stop(.75, transparent), color-stop(.75, rgba(0,0,0,0.05))),
-webkit-gradient(linear, 0 0, 100% 100%, color-stop(.75, transparent), color-stop(.75, rgba(0,0,0,0.05)));
-moz-background-size: 20px 20px;
background-size: 20px 20px;
-webkit-background-size: 20px 20px; /* override value for shitty webkit */
background-position:0 0, 10px 0, 10px -10px, 0px 10px;
}
.collide {
border:0!important;
margin:5px;
box-shadow: 0 0 25px 2px #A00;
}
.ol-control.ol-framerate {
left: auto;
right: 0.5em;
}
</style>
</head>
<body >
<a href="https://github.com/Viglino/ol-games" class="icss-github-corner"><i></i></a>
<a href="../index.html">
<h1>ol-games: Sprite collision</h1>
</a>
<div class="info">
The the <i>ol.Collision</i> perform pixel collisions beetween sprites or with an <i>ol.Offscreen</i>.
<br/>
The <i>ol.Game</i> object has its own collision mecanism use the <i>ol.Game.collide()</i> to test collisions.
</div>
<!-- Map div -->
<div id="map"></div>
<div class="options" >
<h2>Options:</h2>
<div id="pattern">Sprite collision:</div>
<div id="clayer">Collision Layer:</div>
<ul>
</ul>
</div>
<script type="text/javascript">
// Layers
var layer = new ol.layer.Tile({ preload:Infinity, source: new ol.source.StadiaMaps({ layer: 'stamen_watercolor' }) });
var labels = new ol.layer.Tile({ source: new ol.source.StadiaMaps({ layer: 'stamen_toner_labels' }), title:"labels" });
var offlabels = new ol.layer.Tile({ source: new ol.source.StadiaMaps({ layer: 'stamen_toner_labels' }), title:"labels" });
// Popup overlay
var popup = new ol.Overlay.Popup ({
popupClass: "default", //"tooltips", "warning" "black" "default", "tips", "shadow",
positioning: "bottom-left",
offset: [0,-20],
autoPan: false
});
// The map
var map = new ol.Map ({
target: 'map',
loadTilesWhileAnimating: true,
loadTilesWhileInteracting: true,
pixelRatio: 1,
view: new ol.View ({
zoom: 11,
center: [260064, 6250762]
}),
layers: [layer, labels],
overlays: [popup]
});
// Add a background map
// var backmap = new ol.Backscreen({ map:map, layers: [layer, labels], pixelRatio:1 });
// Add an offscreen map to calculte collision over it
var offmap = new ol.Offscreen({ map:map, layers: [offlabels], pixelRatio:1, resample:1 });
$(offmap.canvas).appendTo("#clayer");
var character = new ol.layer.Vector({
source: new ol.source.Vector(),
updateWhileAnimating: true,
updateWhileInteracting: true
});
map.addLayer(character);
var vector = new ol.layer.Vector({
source: new ol.source.Vector(),
updateWhileAnimating: true,
updateWhileInteracting: true
});
map.addLayer(vector);
var orc = new ol.Sprite({
name:"Orc",
position: map.getView().getCenter(),
src: "data/orc.png",
size: 64,
scale: 1.5,
crossOrigin: "anonymous"
});
character.getSource().addFeature(orc);
orc.setDestination(orc.getCoordinate(), 10);
orc.setState("walk_"+orc.getQuarter());
orc.on("destination", function() {
orc.setState("idle");
});
map.on ("click", function(e) {
orc.setDestination(e.coordinate);
orc.setState("walk_"+orc.getQuarter());
});
var elf = new ol.Sprite({
name:"Robin",
position: map.getView().getCenter(),
src: "data/robin.png",
size: 64,
scale: 1.5,
crossOrigin: "anonymous"
});
character.getSource().addFeature(elf);
function getDestination() {
var e = map.getView().calculateExtent(map.getSize());
return [
e[0]+(e[2]-e[0])*Math.random(),
e[1]+(e[3]-e[1])*Math.random()
]
}
elf.setDestination(getDestination(),5);
elf.setState("walk_"+elf.getQuarter());
elf.on("destination", function () {
elf.setDestination(getDestination());
elf.setState("walk_"+elf.getQuarter());
})
// New Game
var game = new ol.Game({ map: map });
map.addControl(new ol.control.FrameRate());
// Add collision test
/*
var collision = new ol.Collision({map: map, sprites: [elf, orc]});
$("#pattern").append(collision.getImage());
game.addCollision (collision);
orc.on("collide", function(e)
{ console.log("hello!");
});
*/
$("#pattern").append(game.collision.getImage());
var spriteCollision = $("#pattern canvas");
var layerCollision = $("#clayer canvas");
// Game loop
game.on ("render", function(e) {
if (game.collide (orc, elf)) {
popup.show(orc.getCoordinate(), "Hello!");
spriteCollision.addClass("collide");
} else {
popup.hide();
orc.move(e);
spriteCollision.removeClass("collide");
}
elf.move(e);
// Test collision with the labels
//if (orc.moving())
if (offmap.collide(orc)) {
layerCollision.addClass("collide");
} else {
layerCollision.removeClass("collide");
}
});
// start game!
game.start();
</script>
</body>
</html>