-
Notifications
You must be signed in to change notification settings - Fork 3
/
ysort.html
166 lines (147 loc) · 3.68 KB
/
ysort.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<title>kaboom game</title>
<link rel="icon" href="https://kaboomjs.com/pub/img/kaboom.png" />
<style>
body, * {
margin: 0;
}
body {
background: black;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
width: 90vw;
margin: 0;
background: black;
border: 4px solid yellow;
}
</style>
<!-- telling browser how to find pako allows compressed maps -->
<script type="importmap">
{
"imports": {
"tiled-kaboom": "./tiled-kaboom.js",
"pako": "https://unpkg.com/[email protected]/dist/pako.esm.mjs",
"kaboom": "https://unpkg.com/[email protected]/dist/kaboom.mjs"
}
}
</script>
<script type="module">
import kaboom from 'kaboom'
import tiledKaboom from 'tiled-kaboom'
import map from './map.json' assert { type: "json" }
const k = kaboom({
plugins: [ tiledKaboom ]
})
// load sprites
loadSprite('player', 'player.png')
loadSprite('badbear', 'badbear.png')
// parse map
const {levels, key, mapObj } = await k.loadTiledMap(map)
// return a nice object for an array of Tiled properties
function getProperties(object) {
return object?.properties?.reduce((a, v) => ({ ...a, [v.name]: v.value }), {}) || {}
}
const info = mapObj.layers.filter(layer => layer.type === 'tilelayer')
const allProps = []
// things that are not over
for (const l in levels) {
allProps.push(getProperties(info[l]))
if (!allProps[l].over){
addLevel(levels[l], { width: 32, height: 32, ...key })
}
}
// get layer called "Objects"
const { objects, offsetx, offsety } = map.layers.find(l => l.name === 'Objects')
const speed = 200
// setup tag-based collision (tags come from type property)
// this only triggers on first touch, so it won't quite work
// collides("player", "enemy", (a, b, side) => {
// if (a.pos.y > b.pos.y){
// console.log('over')
// a.z = 1
// b.z = 0
// }else{
// console.log('under')
// a.z = 0
// b.z = 1
// }
// })
// cal this on every frame of player
const nonPlayers = []
action("player", player => {
const playerRect = {
p1: player.pos,
p2: {
x: player.pos.x + 32,
y: player.pos.y + 32
}
}
for (const object of nonPlayers) {
const objectRect = {
p1: object.pos,
p2: {
x: object.pos.x + 32,
y: object.pos.y + 32
}
}
if (colRectRect(playerRect, objectRect)) {
if (player.pos.y > object.pos.y){
player.z = 1
object.z = 0
}else{
player.z = 0
object.z = 1
}
}
}
})
// add all objects with a "sprite" property
for (const object of objects) {
// get props in nicer object
const props = getProperties(object)
// must have "sprite" prop to use a sprite
if (props.sprite) {
const newthing = [
sprite(props.sprite),
pos(object.x + offsetx, object.y + offsety),
area(),
layer(0),
z(0),
// push some data and a tag from type
{...props, name: object.name},
object.type
]
// if boolean "body" prop, then add a body (physics)
if (props.body) {
newthing.push(body())
}
const mapObject = k.add(newthing)
// if it's player add some keys to make it move
if (object.type === 'player') {
keyPressRep("left", () => {
mapObject.move(vec2(-speed, 0))
})
keyPressRep("right", () => {
mapObject.move(vec2(speed, 0))
})
keyPressRep("up", () => {
mapObject.move(vec2(0, -speed))
})
keyPressRep("down", () => {
mapObject.move(vec2(0, speed))
})
}else{
nonPlayers.push(mapObject)
}
}
}
// things with "over" prop
for (const l in levels) {
if (allProps[l].over){
addLevel(levels[l], { width: 32, height: 32, ...key })
}
}
</script>