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

131 lines
4.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: Game loop</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Game control to show frame rate with ol-games" />
<meta name="keywords" content="ol, game, gameloop, frame, framerate" />
<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 {
top:0.5em;
left: 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: Game loop</h1>
</a>
<div class="info">
<p>
<i>ol.control.FrameRat</i> add a control to show graphically the framerate.
</p>
</div>
<!-- Map div -->
<div id="map"></div>
<div class="options" >
<h2>Options:</h2>
Speed: <input type="range" min="0.1" max="0.5" step="0.01" value="0.2" onchange="speed = parseFloat(this.value)" />
<br/>
Click on the map to change flying direction.
</div>
<script type="text/javascript">
// Layers
var layer = new ol.layer.Tile({ preload:Infinity, source: new ol.source.StadiaMaps({ layer: 'stamen_watercolor' }) });
// Overlay to add features on it
var overlay = new ol.layer.Vector({ source: new ol.source.Vector() });
// Angle, position and speed of the plane
var a = 0;
var center = [260064, 6250762];
var speed = 0.2;
// Add a plane to the overlay
var plane = new ol.Feature(new ol.geom.Point(center));
plane.setStyle ( new ol.style.Style({ image: new ol.style.Icon({ src: "img/Biploar_type4_1.png", scale:0.45 }), zIndex:1 }) );
var shadow = new ol.Feature(new ol.geom.Point(center));
shadow.setStyle ( new ol.style.Style({ image: new ol.style.Icon({ src: "img/Biploar_shadow.png", scale:0.3, opacity:0.4 }), zIndex:0 }) );
overlay.getSource().addFeatures([plane, shadow]);
// Calculate a new direction
function getDirection(pt) {
if (pt) a = Math.atan2 ( pt[0]-center[0], pt[1]-center[1] ) - Math.PI/2;
else a = Math.random()*2*Math.PI;
plane.getStyle().getImage().setRotation(a + Math.PI/2);
shadow.getStyle().getImage().setRotation(a + Math.PI/2);
}
// New Game
var game = new ol.Game({
target: 'map',
zoom: 16,
center: center,
layers: [layer, overlay],
});
getDirection();
// Add frame rate control
game.addControl(new ol.control.FrameRate());
// Click on the map => calculate a new direction
game.getMap().on("click", function(e){
getDirection(e.coordinate);
})
// Game loop
game.on ("render", function(e) {
center[0] += speed * e.dt * Math.cos(a);
center[1] -= speed * e.dt * Math.sin(a);
game.getView().setCenter(center);
plane.getGeometry().setCoordinates(center);
shadow.getGeometry().setCoordinates([center[0]+60, center[1]-80]);
});
// start game!
game.start();
</script>
</body>
</html>