Commit 8de28214 authored by wanli's avatar wanli

🌈 style: 更新代码注释

parent e294e912
node_modules node_modules
dist
build
project
\ No newline at end of file
import { createApp } from './src/runtime-canvas' import { createApp } from './src/runtime-canvas'
import App from './src/App' import App from './src/App'
import {getCanvasRootContainer} from './src/Game' import {getCanvasRootContainer} from './src/Game'
// import * as PIXI from 'pixi.js'
// console.log(PIXI)
// 创建一个根组件
// const app = createApp(App);
// console.log(app)
// const game = new PIXI.Application({
// width: 750,
// height: 1080
// })
// document.body.appendChild(game.view)
// mount
// createApp(App).mount(game.stage)
createApp(App).mount(getCanvasRootContainer()) createApp(App).mount(getCanvasRootContainer())
\ No newline at end of file
import {defineComponent,h,computed,ref} from '@vue/runtime-core' import { defineComponent, h, computed, ref } from "@vue/runtime-core";
import startPage from './page/startPage' import startPage from "./page/startPage";
import gamePage from './page/gamePage' import gamePage from "./page/gamePage";
import endPage from './page/endPage' import endPage from "./page/endPage";
// template -> render() // template -> render()
export default defineComponent({ export default defineComponent({
...@@ -9,7 +9,7 @@ export default defineComponent({ ...@@ -9,7 +9,7 @@ export default defineComponent({
// 创建一个响应式数据对象 // 创建一个响应式数据对象
// 值类型 string number // 值类型 string number
// const currentPageName = ref('startPage') // const currentPageName = ref('startPage')
const currentPageName = ref('gamePage') const currentPageName = ref("gamePage");
// const currentPageName = ref('endPage') // const currentPageName = ref('endPage')
// console.log(currentPageName) // console.log(currentPageName)
// 计算属性 // 计算属性
...@@ -25,20 +25,20 @@ export default defineComponent({ ...@@ -25,20 +25,20 @@ export default defineComponent({
return { return {
currentPageName, currentPageName,
currentPage currentPage,
} };
}, },
render(ctx) { render(ctx) {
// console.log('---ctx---') // console.log('---ctx---')
// console.log(ctx) // console.log(ctx)
return h("Container", [ return h("Container", [
h(ctx.currentPage,{ h(ctx.currentPage, {
onChangePage(page) { onChangePage(page) {
// console.log(page) // console.log(page)
ctx.currentPageName = page ctx.currentPageName = page;
} },
}) }),
]); ]);
// return h("Container", [h(gamePage)]); // return h("Container", [h(gamePage)]);
} },
}); });
import * as PIXI from 'pixi.js' import * as PIXI from "pixi.js";
const game = new PIXI.Application({ const game = new PIXI.Application({
width: 750, width: 750,
height: 1080 height: 1080,
}) });
document.body.appendChild(game.view) document.body.appendChild(game.view);
export function getCanvasRootContainer() { export function getCanvasRootContainer() {
return game.stage return game.stage;
} }
export function getGame() { export function getGame() {
......
// 子弹组件 // 子弹组件
import {defineComponent, h, watch} from '@vue/runtime-core' import { defineComponent, h, watch } from "@vue/runtime-core";
import bullteImg from "../../assets/bunny-self.png"; import bullteImg from "../../assets/bunny-self.png";
export default defineComponent({ export default defineComponent({
props: ['x', 'y'], props: ["x", "y"],
setup(props) {}, setup(props) {},
render(ctx) { render(ctx) {
return h('Container',{x:ctx.x,y:ctx.y},[ return h("Container", { x: ctx.x, y: ctx.y }, [
h('Sprite',{texture: bullteImg}) h("Sprite", { texture: bullteImg }),
]); ]);
} },
}) });
\ No newline at end of file
// 敌军 // 敌军
import {defineComponent,h} from '@vue/runtime-core' import { defineComponent, h } from "@vue/runtime-core";
import enemyImg from "../../assets/enemy.png"; import enemyImg from "../../assets/enemy.png";
export default defineComponent({ export default defineComponent({
props: ['x', 'y'], props: ["x", "y"],
render(ctx) { render(ctx) {
return h('Container',{x:ctx.x,y:ctx.y},[ return h("Container", { x: ctx.x, y: ctx.y }, [
h('Sprite',{texture: enemyImg}) h("Sprite", { texture: enemyImg }),
]); ]);
} },
}) });
\ No newline at end of file
import {defineComponent,h, ref} from '@vue/runtime-core' import { defineComponent, h, ref } from "@vue/runtime-core";
import mapImg from '../../assets/map.jpg' import mapImg from "../../assets/map.jpg";
import {getGame} from '../Game' import { getGame } from "../Game";
export default defineComponent({ export default defineComponent({
setup() { setup() {
const mapHeight = 1080 const mapHeight = 1080;
const mapY1 = ref(0) const mapY1 = ref(0);
const mapY2 = ref(-mapHeight) const mapY2 = ref(-mapHeight);
// pixi循环 // pixi循环
const speed = 5 const speed = 5;
getGame().ticker.add(() => { getGame().ticker.add(() => {
// console.log('---111---') // console.log('---111---')
mapY1.value += speed mapY1.value += speed;
mapY2.value += speed mapY2.value += speed;
if (mapY1.value >= mapHeight) { if (mapY1.value >= mapHeight) {
mapY1.value = -mapHeight mapY1.value = -mapHeight;
} }
if (mapY2.value >= mapHeight) { if (mapY2.value >= mapHeight) {
mapY2.value = -mapHeight mapY2.value = -mapHeight;
} }
}) });
return { return {
mapY1, mapY1,
mapY2 mapY2,
} };
}, },
render(ctx) { render(ctx) {
return h('Container', [ return h("Container", [
h('Sprite', { texture: mapImg, y: ctx.mapY1} ), h("Sprite", { texture: mapImg, y: ctx.mapY1 }),
h('Sprite', { texture: mapImg, y: ctx.mapY2 }) h("Sprite", { texture: mapImg, y: ctx.mapY2 }),
]) ]);
} },
}) });
\ No newline at end of file
import {defineComponent, h, reactive, watch, toRefs, import {
onMounted,onUnmounted} from '@vue/runtime-core' defineComponent,
import planeImg from '../../assets/plane.png' h,
reactive,
watch,
toRefs,
onMounted,
onUnmounted,
} from "@vue/runtime-core";
import planeImg from "../../assets/plane.png";
export default defineComponent({ export default defineComponent({
props: ['x','y'], props: ["x", "y"],
setup(props,ctx) { setup(props, ctx) {
console.log('---props---') console.log("---props---");
console.log(props) console.log(props);
const handleAttack = (e) => { const handleAttack = (e) => {
if (e.code==='Space') { if (e.code === "Space") {
// console.log('attack') // console.log('attack')
// console.log(e.code) // console.log(e.code)
ctx.emit('attack', { ctx.emit("attack", {
x:props.x, x: props.x,
y:props.y y: props.y,
}) });
}
} }
};
onMounted(() => { onMounted(() => {
window.addEventListener('keydown',handleAttack) window.addEventListener("keydown", handleAttack);
}) });
onUnmounted(() => { onUnmounted(() => {
window.removeEventListener('keydown',handleAttack) window.removeEventListener("keydown", handleAttack);
}) });
const point = reactive({ const point = reactive({
x: props.x, x: props.x,
y: props.y y: props.y,
}) });
watch(props, (value) => { watch(props, (value) => {
point.x = value.x; point.x = value.x;
point.y = value.y; point.y = value.y;
}) });
console.log('---props---') console.log("---props---");
console.log(props.x) console.log(props.x);
// toRefs // toRefs
const {x,y} = toRefs(props) const { x, y } = toRefs(props);
console.log(x,y) console.log(x, y);
return { return {
x,y x,
} y,
};
}, },
render(ctx) { render(ctx) {
return h('Container', { x:ctx.x, y:ctx.y }, [ return h("Container", { x: ctx.x, y: ctx.y }, [
h('Sprite', { texture: planeImg} ) h("Sprite", { texture: planeImg }),
]) ]);
} },
}) });
\ No newline at end of file
// 游戏结束页面 // 游戏结束页面
import {defineComponent,h} from '@vue/runtime-core' import { defineComponent, h } from "@vue/runtime-core";
import endPageImage from '../../assets/end_page.jpg' import endPageImage from "../../assets/end_page.jpg";
import restartBtn from '../../assets/restartBtn.png' import restartBtn from "../../assets/restartBtn.png";
// template->render // template->render
export default defineComponent({ export default defineComponent({
render(ctx) { render(ctx) {
// 显示一张图 // 显示一张图
// <div><img src="" alt=""/></div> // <div><img src="" alt=""/></div>
return h("Container",[ return h("Container", [
h("Sprite",{texture: endPageImage}), h("Sprite", { texture: endPageImage }),
h("Sprite", { h("Sprite", {
texture: restartBtn, x: 210, y: 500, texture: restartBtn,
x: 210,
y: 500,
interactive: true, interactive: true,
onClick() { onClick() {
// console.log('---ctx---') // console.log('---ctx---')
// console.log(ctx) // console.log(ctx)
ctx.$emit("changePage", "gamePage") ctx.$emit("changePage", "gamePage");
} },
}) }),
]) ]);
} },
}) });
\ No newline at end of file
import { defineComponent, h, reactive, toRefs, onMounted, onUnmounted } from '@vue/runtime-core' import {
defineComponent,
import Map from '../component/Map' h,
import Plane from '../component/Plane' reactive,
import Bullet from '../component/Bullet' toRefs,
import EnemyPlane from '../component/EnemyPlane' onMounted,
onUnmounted,
import { getGame } from '../Game' } from "@vue/runtime-core";
import {hitTestTrctangle} from '../utils/index'
import Map from "../component/Map";
import Plane from "../component/Plane";
import Bullet from "../component/Bullet";
import EnemyPlane from "../component/EnemyPlane";
import { getGame } from "../Game";
import { hitTestTrctangle } from "../utils/index";
export default defineComponent({ export default defineComponent({
setup(props,ctx) { setup(props, ctx) {
const planeInfo = useCreatePlaneInfo();
const planeInfo = useCreatePlaneInfo()
// 子弹数据 // 子弹数据
const bullets = reactive([]) const bullets = reactive([]);
// 敌军数据 // 敌军数据
const enemyPlanes = reactive([{ const enemyPlanes = reactive([
x:10,y:10, {
width:308,height:207 x: 10,
}]) y: 10,
width: 308,
height: 207,
},
]);
const handleAttack = (info) => { const handleAttack = (info) => {
const createBulletInfo = () => { const createBulletInfo = () => {
return { return {
x:info.x+100, x: info.x + 100,
y:info.y y: info.y,
} };
} };
bullets.push(createBulletInfo()) bullets.push(createBulletInfo());
return return;
} };
getGame().ticker.add(()=>{ getGame().ticker.add(() => {
// 让子弹动起来 // 让子弹动起来
// 改变子弹的y坐标 // 改变子弹的y坐标
// const speed = 10; // const speed = 10;
// bullets.forEach(bulletInfo => { // bullets.forEach(bulletInfo => {
// bulletInfo.y -= speed; // bulletInfo.y -= speed;
// }) // })
moveBullets(bullets) moveBullets(bullets);
// 碰撞💥检测 // 碰撞💥检测
enemyPlanes.forEach(enemyPlaneInfo => { enemyPlanes.forEach((enemyPlaneInfo) => {
if(hitTestTrctangle(enemyPlaneInfo,planeInfo)) if (hitTestTrctangle(enemyPlaneInfo, planeInfo)) {
{ ctx.emit("changePage", "endPage");
ctx.emit('changePage','endPage') console.log("hit----");
console.log('hit----')
} }
}) });
}) });
// 创建子弹 // 创建子弹
return { return {
planeInfo, planeInfo,
bullets, bullets,
handleAttack, handleAttack,
enemyPlanes enemyPlanes,
} };
}, },
render(ctx) { render(ctx) {
// 渲染子弹 // 渲染子弹
const renderBullets = () => { const renderBullets = () => {
return ctx.bullets.map((info) => { return ctx.bullets.map((info) => {
return h(Bullet, { x: info.x, y: info.y }) return h(Bullet, { x: info.x, y: info.y });
}) });
} };
// 渲染敌人飞机 // 渲染敌人飞机
const renderEnemyPlanes = () => { const renderEnemyPlanes = () => {
return ctx.enemyPlanes.map((info) => { return ctx.enemyPlanes.map((info) => {
return h(EnemyPlane, { x: info.x, y: info.y }) return h(EnemyPlane, { x: info.x, y: info.y });
}) });
} };
return h( return h("Container", [
"Container", [
h(Map), h(Map),
h(Plane,{ x:ctx.planeInfo.x, y:ctx.planeInfo.y, onAttack: ctx.handleAttack }), h(Plane, {
x: ctx.planeInfo.x,
y: ctx.planeInfo.y,
onAttack: ctx.handleAttack,
}),
...renderBullets(), ...renderBullets(),
...renderEnemyPlanes() ...renderEnemyPlanes(),
] ]);
) },
} });
})
const useCreatePlaneInfo = () => { const useCreatePlaneInfo = () => {
const planeInfo = reactive({ const planeInfo = reactive({
x:240,y:680, x: 240,
width:258,height:364 y: 680,
}) width: 258,
height: 364,
});
// console.log(planeInfo) // console.log(planeInfo)
// 让飞机动起来 // 让飞机动起来
const {x, y} = useMovePlane(planeInfo.x,planeInfo.y) const { x, y } = useMovePlane(planeInfo.x, planeInfo.y);
planeInfo.x = x; planeInfo.x = x;
planeInfo.y = y; planeInfo.y = y;
return planeInfo; return planeInfo;
} };
const useMovePlane = (initX, initY) => { const useMovePlane = (initX, initY) => {
const speed = 20; const speed = 20;
const point = reactive({ const point = reactive({
x:initX, x: initX,
y:initY y: initY,
}); });
const handleKeyDown = (e) => { const handleKeyDown = (e) => {
...@@ -121,19 +132,19 @@ const useMovePlane = (initX, initY) => { ...@@ -121,19 +132,19 @@ const useMovePlane = (initX, initY) => {
point.x += speed; point.x += speed;
break; break;
} }
} };
onMounted(()=>{ onMounted(() => {
window.addEventListener('keydown',handleKeyDown) window.addEventListener("keydown", handleKeyDown);
}) });
onUnmounted(()=>{ onUnmounted(() => {
window.removeEventListener('keydown',handleKeyDown) window.removeEventListener("keydown", handleKeyDown);
}) });
return toRefs(point); return toRefs(point);
} };
const moveBullets = (bullets) => { const moveBullets = (bullets) => {
const speed = 10; const speed = 10;
bullets.forEach(bulletInfo => { bullets.forEach((bulletInfo) => {
bulletInfo.y -= speed; bulletInfo.y -= speed;
}) });
} };
\ No newline at end of file
// 开始页面 // 开始页面
import {defineComponent,h} from '@vue/runtime-core' import { defineComponent, h } from "@vue/runtime-core";
import startPageImage from '../../assets/start_page.jpg' import startPageImage from "../../assets/start_page.jpg";
import startBtn from '../../assets/startBtn.png' import startBtn from "../../assets/startBtn.png";
// template->render // template->render
export default defineComponent({ export default defineComponent({
render(ctx) { render(ctx) {
// 显示一张图 // 显示一张图
// <div><img src="" alt=""/></div> // <div><img src="" alt=""/></div>
return h("Container",[ return h("Container", [
h("Sprite",{texture: startPageImage}), h("Sprite", { texture: startPageImage }),
h("Sprite", { h("Sprite", {
texture: startBtn, x: 210, y: 500, texture: startBtn,
x: 210,
y: 500,
interactive: true, interactive: true,
onClick() { onClick() {
// console.log('---ctx---') // console.log('---ctx---')
// console.log(ctx) // console.log(ctx)
ctx.$emit("changePage", "gamePage") ctx.$emit("changePage", "gamePage");
} },
}) }),
]) ]);
} },
}) });
\ No newline at end of file
import { createRenderer } from "@vue/runtime-core";
import {createRenderer} from '@vue/runtime-core' import { Graphics, Text, Container, Sprite, Texture } from "pixi.js";
import {Graphics,Text, Container, Sprite, Texture} from 'pixi.js'
// 创建渲染器 // 创建渲染器
const renderer = createRenderer({ const renderer = createRenderer({
...@@ -18,49 +17,63 @@ const renderer = createRenderer({ ...@@ -18,49 +17,63 @@ const renderer = createRenderer({
} }
return element; return element;
}, },
insert(el,parent) { insert(el, parent) {
parent.addChild(el) console.log("==========> insert start <==========");
parent.addChild(el);
console.log(el);
console.log(parent);
console.log("==========> insert end <==========");
}, },
patchProp(el,key,prevValue,nextValue){ patchProp(el, key, prevValue, nextValue) {
// console.log(nextValue) console.log("==========> patch prop start <==========");
// PIXI console.log(el)
console.log(key)
console.log(prevValue)
console.log(nextValue)
switch (key) { switch (key) {
case "texture": case "texture":
el.texture = Texture.from(nextValue) el.texture = Texture.from(nextValue);
break; break;
case "onClick": case "onClick":
// 给pixi元素注册点击事件 // 给pixi元素注册点击事件
el.on("pointertap", nextValue); el.on("pointertap", nextValue);
break; break;
default: default:
el[key] = nextValue el[key] = nextValue;
} }
console.log("==========> patch prop end <==========");
}, },
setElementText(node,text) { setElementText(node, text) {
const canvasText = new Text(text) const canvasText = new Text(text);
node.addChild(canvasText) node.addChild(canvasText);
}, },
createText(text){ createText(text) {
// console.log(text) // console.log(text)
return new Text(text) return new Text(text);
}, },
// 处理注释 // 处理注释
createComment() {}, createComment() {},
// 获取父节点 // 获取父节点
parentNode() {}, parentNode(node) {
console.log("==========> parent node start <==========");
console.log(node);
console.log("==========> parent node end <==========");
},
// 获取兄弟节点 // 获取兄弟节点
nextSibling() {}, nextSibling() {},
// 删除节点时调用 // 删除节点时调用
remove(el) { remove(el) {
console.log("==========> remove start <==========");
const parent = el.parent; const parent = el.parent;
if (parent) { if (parent) {
parent.removeChild(el); parent.removeChild(el);
} }
} console.log("==========> remove end <==========");
}) },
});
// console.log(renderer) // console.log(renderer)
export function createApp(rootComponent) { export function createApp(rootComponent) {
// 调用 renderer // 调用 renderer
return renderer.createApp(rootComponent) return renderer.createApp(rootComponent);
} }
// 碰撞💥 // 碰撞💥
export function hitTestTrctangle(objA,objB) { export function hitTestTrctangle(objA, objB) {
return ( return (
objA.x + objA.width >= objB.x && objA.x + objA.width >= objB.x &&
objB.x + objB.width >= objA.x && objB.x + objB.width >= objA.x &&
objA.y + objA.height >= objB.y && objA.y + objA.height >= objB.y &&
objB.y + objB.height >= objA.y objB.y + objB.height >= objA.y
) );
} }
const path = require("path") const path = require("path")
module.exports = { module.exports = {
mode: "production",
entry: path.resolve(__dirname, "./main.js"), entry: path.resolve(__dirname, "./main.js"),
output: { output: {
filename: 'build.js', filename: 'build.js',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment