Skip to content

Commit 4dce7ac

Browse files
committed
手里
1 parent a092e6c commit 4dce7ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+355
-212
lines changed

TODO.md

Lines changed: 1 addition & 0 deletions

docs/examples/assets/tiles.png

1.24 KB

docs/examples/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
10+
<body>
11+
<style>
12+
body {
13+
margin: 0% !important;
14+
overflow: hidden;
15+
}
16+
</style>
17+
<canvas id="game"></canvas>
18+
<script type="module" src="./js/index.ts"></script>
19+
</body>
20+
21+
</html>

docs/examples/js/index.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { Camera, Color, Game, GameObject, ScaleRadio, Scene, Sprite, Vec2 } from "../../../src/index"
2+
import { Tilemap, TileSet } from "../../../src/tilemap"
3+
4+
const game = new Game({
5+
canvas: document.getElementById("game") as HTMLCanvasElement,
6+
backgroundColor: Color.Blue(),
7+
scaleEnable: true,
8+
scaleRadio: ScaleRadio.EXPAND,
9+
width: 500,
10+
height: 500,
11+
antialias: true
12+
})
13+
14+
const updateDisplaySize = () => {
15+
game.render.updateDisplaySize(
16+
document.documentElement.clientWidth,
17+
document.documentElement.clientHeight
18+
)
19+
}
20+
21+
window.addEventListener('resize', updateDisplaySize)
22+
updateDisplaySize()
23+
24+
//@ts-ignore
25+
window.game = game
26+
27+
const texture = (await game.asset.loadImage("tiles", "./assets/tiles.png"))!
28+
const tilemapSheet = texture.createSpritesheet(16, 16, 3, 1, 0, 1)
29+
console.log(tilemapSheet)
30+
const tileSet = new TileSet(16, 16)
31+
32+
tilemapSheet.forEach((texture, index) => {
33+
tileSet.setTile(index, texture)
34+
})
35+
36+
class MainScene extends Scene {
37+
player: GameObject
38+
tilemap: GameObject
39+
40+
public create(): void {
41+
this.tilemap = GameObject.create(game, {
42+
position: new Vec2(0, 0),
43+
components: [
44+
new Tilemap({
45+
tileSet,
46+
})
47+
]
48+
})
49+
50+
this.tilemap.getComponent(Tilemap)?.setData([
51+
[1, 0, 2, 1, 2, 1, 0],
52+
[0, 1, 0, 1, 2, 1, 0],
53+
[2, 0, 1, 1, 2, 1, 0],
54+
])
55+
56+
57+
this.player = GameObject.create(game, {
58+
position: new Vec2(0, 0),
59+
components: [
60+
new Sprite({
61+
animations: {
62+
"idle": {
63+
frames: texture.createSpritesheet(16, 16, 2, 1),
64+
fps: 4,
65+
loop: true
66+
},
67+
"walk": {
68+
frames: texture.createSpritesheet(16, 16, 4, 1, 2, 0),
69+
fps: 8,
70+
loop: true
71+
}
72+
},
73+
}),
74+
],
75+
})
76+
this.player.getComponent(Sprite)?.play("idle")
77+
78+
this.addChild(this.tilemap)
79+
this.addChild(this.player)
80+
81+
const camera = GameObject.create(game, {
82+
scale: 0.6,
83+
components: [
84+
new Camera({
85+
enable: true,
86+
}),
87+
]
88+
})
89+
this.player.addChild(camera)
90+
91+
this.player.camera = camera
92+
this.tilemap.camera = camera
93+
}
94+
onUpdate(deltaTime: number): void {
95+
const velocity = this.game.input.getVector("KeyA", "KeyD", "KeyW", "KeyS")
96+
const sprite = this.player.getComponent(Sprite)!
97+
98+
this.player.position.addSelf(velocity.multiply(deltaTime * 50))
99+
if (velocity.length() > 0) {
100+
sprite.play("walk")
101+
if (Math.abs(velocity.x) > 0) {
102+
sprite.flipX = velocity.x < 0
103+
}
104+
} else {
105+
sprite.play("idle")
106+
}
107+
}
108+
}
109+
110+
game.switchScene(new MainScene(game))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)