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

173 lines
5.1 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 path</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Moving sprites along a path with openlayers" />
<meta name="keywords" content="ol,openlayers, game, animation, sprite, move, path" />
<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;
}
.ol-control.ol-framerate {
left: auto;
right: 0.5em;
}
textarea {
vertical-align: top;
}
</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 path</h1>
</a>
<div class="info">
This example show how a sprite can follow a path.
<br/>
You can use the <i>ol.Sprite.setPath()</i> to set the path to follow. Then just call <i>ol.Sprite.move()</i>
in the game loop to have the sprite moving along the path.
Use <i>ol.Sprite.stop()</i> and <i>ol.Sprite.restart()</i> to control the movement.
<br />
Listen to :
<ul>
<li>
<i>change:direction</i> event to know when the sprite take a new segment
</li>
<li>
<i>destination</i> event to know when the sprite reach the final destination.
</li>
</ul>
</div>
<!-- Map div -->
<div id="map"></div>
<div class="options" >
<h2>Options:</h2>
<ul>
<li>
<button onclick="go()">Follow the path...</button>
</li>
<li>
<button onclick="plane.stop()">Stop!</button>
<button onclick="if (plane.destination) plane.restart()">Restart!</button>
</li>
<li><hr /></li>
<li>
<input id="loop" type="checkbox" checked="checked" /><label for="loop"> loop</label>
</li>
<li>
Speed: <input id="speed" type="number" min="3" max="100" value="10" onchange="plane.setSpeed(Math.max(parseInt(this.value),1));" />
</li>
</ul>
</div>
<script type="text/javascript">
// Layers
var layer = new ol.layer.Tile({ preload:Infinity, source: new ol.source.StadiaMaps({ layer: 'stamen_watercolor' }) });
// The map
var map = new ol.Map ({
target: 'map',
loadTilesWhileAnimating: true,
loadTilesWhileInteracting: true,
view: new ol.View ({
zoom: 11,
center: [260064, 6250762]
}),
controls: [ new ol.control.Attribution() ],
interactions: [],
layers: [layer]
});
var sprites = new ol.layer.Vector({
source: new ol.source.Vector(),
updateWhileAnimating: true,
updateWhileInteracting: true
});
map.addLayer(sprites);
// New Path (cspline)
var path = new ol.geom.LineString([[250000, 6250762], [270000, 6265000], [280000, 6240000], [250000, 6250762]]);
var f = new ol.Feature(path.cspline({ tension: 1, pointsPerSeg: 50 }));
sprites.getSource().addFeature(f);
// Plane
var plane = new ol.Sprite({
position: [250000, 6250762],
src: "data/plane.png",
size: 175,
scale: 0.4,
frameRate: 10,
// Define the states for the plane
states: {
idle: { line:0, length:3 }
}
});
sprites.getSource().addFeature(plane);
// When direction change, set the angle of the sprite
plane.on("change:direction", function(e){
plane.setRotation(e.angle);
});
// Follow the path
var t;
function go() {
t = new Date();
var speed = Math.max(parseInt($("#speed").val()),1);
$("#speed").val(speed);
if (!plane.moving()) plane.setPath(f.getGeometry().getCoordinates(), speed);
}
// Calculate trip duration
plane.on("destination", function(e) {
console.log("Time: "+((new Date() - t)/1000));
if ($("#loop").prop("checked")) go();
});
// New Game
var game = new ol.Game({ map: map, controls:[new ol.control.FrameRate()] });
// Game loop
game.on ("render", function(e) {
plane.move(e);
});
// start game!
game.start();
</script>
</body>
</html>