1 Star 0 Fork 0

刘畅2016/map_2d

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2dmap_movement.htm 6.34 KB
一键复制 编辑 原始数据 按行查看 历史
liuch 提交于 2021-12-28 02:17 . init
<html>
<head>
<title>JavaScript Wolfenstein 3D Engine - Player movement</title>
<!--
/*
* Example code from the article at: http://dev.opera.com/articles/view/creating-pseudo-3d-games-with-html-5-can-1/
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* The code is available to use under the terms of the MIT License
* [http://www.opensource.org/licenses/mit-license.php]
*/
-->
<style>
div#minimapcontainer {
}
canvas#minimap {
position : absolute;
}
canvas#minimapobjects {
position : absolute;
}
</style>
<script src="excanvas.js" type="text/javascript"></script>
</head>
<body>
<div id="minimapcontainer">
<canvas id="minimap"></canvas>
<canvas id="minimapobjects"></canvas>
</div>
<script type="text/javascript">
var $ = function(id) { return document.getElementById(id); };
var dc = function(tag) { return document.createElement(tag); };
var map = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,3,0,3,0,0,1,1,1,2,1,1,1,1,1,2,1,1,1,2,1,0,0,0,0,0,0,0,0,1],
[1,0,0,3,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];
var player = {
x : 16, // current x, y position
y : 10,
dir : 0, // the direction that the player is turning, either -1 for left or 1 for right.
rot : 0, // the current angle of rotation
speed : 0, // is the playing moving forward (speed = 1) or backwards (speed = -1).
moveSpeed : 0.18, // how far (in map units) does the player move each step/update
rotSpeed : 6 // how much does the player rotate each step/update (in degrees)
}
var mapWidth = 0;
var mapHeight = 0;
var miniMapScale = 8;
function init() {
mapWidth = map[0].length;
mapHeight = map.length;
bindKeys();
drawMiniMap();
gameCycle();
}
// bind keyboard events to game functions (movement, etc)
function bindKeys() {
document.onkeydown = function(e) {
e = e || window.event;
switch (e.keyCode) { // which key was pressed?
case 38: // up, move player forward, ie. increase speed
player.speed = 1;
break;
case 40: // down, move player backward, set negative speed
player.speed = -1;
break;
case 37: // left, rotate player left
player.dir = -1;
break;
case 39: // right, rotate player right
player.dir = 1;
break;
}
}
document.onkeyup = function(e) {
e = e || window.event;
switch (e.keyCode) {
case 38:
case 40:
player.speed = 0; // stop the player movement when up/down key is released
break;
case 37:
case 39:
player.dir = 0;
break;
}
}
}
function gameCycle() {
move();
updateMiniMap();
setTimeout(gameCycle,1000/30);
}
function move() {
var moveStep = player.speed * player.moveSpeed; // player will move this far along the current direction vector
player.rot += player.dir * player.rotSpeed * Math.PI / 180; // add rotation if player is rotating (player.dir != 0)
var newX = player.x + Math.cos(player.rot) * moveStep; // calculate new player position with simple trigonometry
var newY = player.y + Math.sin(player.rot) * moveStep;
player.x = newX; // set new position
player.y = newY;
}
function updateMiniMap() {
var miniMap = $("minimap");
var miniMapObjects = $("minimapobjects");
var objectCtx = miniMapObjects.getContext("2d");
objectCtx.clearRect(0,0,miniMap.width,miniMap.height);
objectCtx.fillRect( // draw a dot at the current player position
player.x * miniMapScale - 2,
player.y * miniMapScale - 2,
4, 4
);
objectCtx.beginPath();
objectCtx.moveTo(player.x * miniMapScale, player.y * miniMapScale);
objectCtx.lineTo(
(player.x + Math.cos(player.rot) * 4) * miniMapScale,
(player.y + Math.sin(player.rot) * 4) * miniMapScale
);
objectCtx.closePath();
objectCtx.stroke();
}
function drawMiniMap() {
// draw the topdown view minimap
var miniMap = $("minimap"); // the actual map
var miniMapCtr = $("minimapcontainer"); // the container div element
var miniMapObjects = $("minimapobjects"); // the canvas used for drawing the objects on the map (player character, etc)
miniMap.width = mapWidth * miniMapScale; // resize the internal canvas dimensions
miniMap.height = mapHeight * miniMapScale; // of both the map canvas and the object canvas
miniMapObjects.width = miniMap.width;
miniMapObjects.height = miniMap.height;
var w = (mapWidth * miniMapScale) + "px" // minimap CSS dimensions
var h = (mapHeight * miniMapScale) + "px"
miniMap.style.width = miniMapObjects.style.width = miniMapCtr.style.width = w;
miniMap.style.height = miniMapObjects.style.height = miniMapCtr.style.height = h;
var ctx = miniMap.getContext("2d");
// loop through all blocks on the map
for (var y=0;y<mapHeight;y++) {
for (var x=0;x<mapWidth;x++) {
var wall = map[y][x];
if (wall > 0) { // if there is a wall block at this (x,y) ...
ctx.fillStyle = "rgb(200,200,200)";
ctx.fillRect( // ... then draw a block on the minimap
x * miniMapScale,
y * miniMapScale,
miniMapScale,miniMapScale
);
}
}
}
updateMiniMap();
}
setTimeout(init, 1);
</script>
</body>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LiuChang2016/map_2d.git
[email protected]:LiuChang2016/map_2d.git
LiuChang2016
map_2d
map_2d
master

搜索帮助