mirror of
https://github.com/Viglino/ol-games.git
synced 2026-01-25 15:35:19 +00:00
147 lines
4.3 KiB
HTML
147 lines
4.3 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 destination</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<meta name="description" content="Moving sprites to a destination with openlayers" />
|
|
<meta name="keywords" content="ol, openlayers, game, animation, sprite, move, destination" />
|
|
|
|
<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 destination</h1>
|
|
</a>
|
|
<div class="info">
|
|
This example show how to give a destination to a sprite.
|
|
<br/>
|
|
You just have to use the <i>ol.Sprite.setDestination()</i> to set the destination you want to go.
|
|
Then just call <i>ol.Sprite.move()</i> in the game loop to have the sprite moving to that point.
|
|
<br/>
|
|
Listen to <i>destination</i> event to know when the sprite reach the final destination.
|
|
</div>
|
|
|
|
<!-- Map div -->
|
|
<div id="map"></div>
|
|
|
|
<div class="options" >
|
|
<h2>Options:</h2>
|
|
<ul>
|
|
<li>
|
|
Name: <textarea type="text" onkeyup="orc.setName(this.value)">Orky</textarea>
|
|
</li>
|
|
</ul>
|
|
Click on the map to move Orky.
|
|
</div>
|
|
|
|
|
|
<script type="text/javascript">
|
|
// Layers
|
|
var layer = new ol.layer.Tile({ preload:Infinity, source: new ol.source.StadiaMaps({ layer: 'stamen_watercolor' }) });
|
|
|
|
// Popup overlay
|
|
var popup = new ol.Overlay.Popup ({
|
|
popupClass: "default",
|
|
positioning: "bottom-left",
|
|
offset: [0,-20],
|
|
autoPan: false
|
|
});
|
|
|
|
// The map
|
|
var map = new ol.Map ({
|
|
target: 'map',
|
|
loadTilesWhileAnimating: true,
|
|
loadTilesWhileInteracting: true,
|
|
view: new ol.View ({
|
|
zoom: 11,
|
|
center: [260064, 6250762]
|
|
}),
|
|
layers: [layer],
|
|
overlays: [popup]
|
|
});
|
|
|
|
var caractere = new ol.layer.Vector({
|
|
source: new ol.source.Vector(),
|
|
updateWhileAnimating: true,
|
|
updateWhileInteracting: true
|
|
});
|
|
map.addLayer(caractere);
|
|
|
|
// Add orc sprite
|
|
var orc = new ol.Sprite({
|
|
name:"Orky",
|
|
position: map.getView().getCenter(),
|
|
src: "data/orc.png",
|
|
scale: 1.5
|
|
});
|
|
caractere.getSource().addFeature(orc);
|
|
|
|
// On click => change destination
|
|
map.on ("click", function(e){
|
|
orc.setDestination(e.coordinate, map.getView().getResolution()/7);
|
|
orc.setState("walk_"+orc.getQuarter());
|
|
popup.hide();
|
|
});
|
|
|
|
// Do something when arrived at destination
|
|
var sentences = ["Hello!","Grrwn...","Do you know what!", "I'm so happy","It's me!","Hugh!"];
|
|
orc.on("destination", function() {
|
|
orc.setState("idle");
|
|
// Show popup then hide
|
|
popup.show(orc.getCoordinate(), sentences[(Math.random()*sentences.length)|0]);
|
|
setTimeout(function(){popup.hide()}, 1000);
|
|
});
|
|
|
|
// New Game
|
|
var game = new ol.Game({ map: map, controls:[new ol.control.FrameRate()] });
|
|
// Game loop
|
|
game.on ("render", function(e) {
|
|
orc.move(e);
|
|
});
|
|
// start game!
|
|
game.start();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html> |