parent
1cb251f5e1
commit
c4b16d76db
Before Width: | Height: | Size: 102 KiB After Width: | Height: | Size: 102 KiB |
@ -0,0 +1,71 @@
|
||||
const AC_GAME_OBJECTS = [];
|
||||
|
||||
// 基类
|
||||
export class AcGameObject
|
||||
{
|
||||
constructor(){
|
||||
//添加对象
|
||||
AC_GAME_OBJECTS.push(this);
|
||||
//时间间隔
|
||||
this.timedelta = 0;
|
||||
//没有执行过start函数
|
||||
this.has_called_satrt = false;
|
||||
}
|
||||
|
||||
start(){//只在开始执行一次
|
||||
|
||||
}
|
||||
|
||||
update(){//除了第一次,每一帧执行一次
|
||||
|
||||
}
|
||||
|
||||
on_destroy(){// 删除前执行一次
|
||||
|
||||
}
|
||||
|
||||
destroy(){//删除时执行
|
||||
this.on_destroy();
|
||||
|
||||
//删除对象
|
||||
for(let i in AC_GAME_OBJECTS){
|
||||
const obj = AC_GAME_OBJECTS[i];
|
||||
if(obj === this)
|
||||
{
|
||||
AC_GAME_OBJECTS.splice(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//上一秒执行的时刻
|
||||
let last_timestamp;
|
||||
|
||||
const step = timestamp => {
|
||||
//针对所有对象
|
||||
for(let obj of AC_GAME_OBJECTS)
|
||||
{
|
||||
if(obj.has_called_satrt===false)//执行start
|
||||
{
|
||||
obj.start();//调用的是子类的start()
|
||||
obj.has_called_satrt=true;
|
||||
}
|
||||
else //执行update
|
||||
{
|
||||
obj.update();//调用的是子类的update()
|
||||
obj.timedelta = timestamp - last_timestamp;//当前-之前
|
||||
}
|
||||
}
|
||||
last_timestamp = timestamp;
|
||||
|
||||
//【迭代】浏览器每一秒会迭代60次
|
||||
requestAnimationFrame(step);
|
||||
|
||||
}
|
||||
|
||||
//浏览器刷新 -> 执行一次
|
||||
requestAnimationFrame(step);
|
@ -0,0 +1,32 @@
|
||||
import { AcGameObject } from "./AcGameObject";
|
||||
|
||||
export class Wall extends AcGameObject{
|
||||
//传入 行 列 地图
|
||||
constructor(r,c,gamemap){
|
||||
super();
|
||||
|
||||
this.r = r;
|
||||
this.c = c;
|
||||
this.gamemap = gamemap;
|
||||
this.color = "#B37266";
|
||||
}
|
||||
|
||||
start(){
|
||||
|
||||
}
|
||||
|
||||
update(){
|
||||
this.render();
|
||||
}
|
||||
|
||||
//渲染
|
||||
render(){
|
||||
//获取地图中 小格长度 画板
|
||||
const L = this.gamemap.L;
|
||||
const ctx = this.gamemap.ctx;
|
||||
|
||||
ctx.fillStyle = this.color;
|
||||
ctx.fillRect(this.c*L,this.r*L, L, L);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<template>
|
||||
<div ref="parent" class="gamemap">
|
||||
<canvas ref="canvas"></canvas>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
//注意 {}
|
||||
import { GameMap } from "@/assets/scripts/GameMap.js";
|
||||
import { ref,onMounted } from "vue";
|
||||
|
||||
export default {
|
||||
setup(){
|
||||
//获取DOM
|
||||
let parent = ref(null);
|
||||
let canvas = ref(null);
|
||||
|
||||
onMounted(()=>{
|
||||
new GameMap(canvas.value.getContext('2d'),parent.value);
|
||||
// console.log(parent.value);//DOM
|
||||
// console.log(canvas.value);//DOM
|
||||
})
|
||||
|
||||
return {
|
||||
parent,
|
||||
canvas
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
div.gamemap {
|
||||
width: 100%;
|
||||
height:100%;
|
||||
|
||||
/* 将canvas画布居中 */
|
||||
display: flex;
|
||||
justify-content:center;
|
||||
align-items: center;
|
||||
}
|
||||
</style>
|
@ -0,0 +1,27 @@
|
||||
<template>
|
||||
<div class="playground">
|
||||
<GameMap></GameMap>
|
||||
</div>
|
||||
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import GameMap from "@/components/GameMap.vue";
|
||||
|
||||
export default {
|
||||
components:{
|
||||
GameMap,
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
div.playground {
|
||||
width: 60vw;
|
||||
height: 70vh;
|
||||
/* background-color: lightblue; */
|
||||
margin: 40px auto;
|
||||
}
|
||||
|
||||
</style>
|
Loading…
Reference in new issue