-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverworld.html
More file actions
215 lines (174 loc) · 5.29 KB
/
overworld.html
File metadata and controls
215 lines (174 loc) · 5.29 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Overworld Game Sample</title>
<meta name="description" content="Wander around a retro Zelda style map.">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="example.css">
<script src="../dist/b8.min.js"></script>
<script>
let mapArray = null;
let map = null;
const NPC_IDS = [ "0", "1", "2", "3" ];
const MAPS = {
"OVERWORLD": [
" ",
" ~~~~~ ^^^ ~~ ^^ ",
" ~~~|, ^^ ~~ ^^|| ",
" ~|| ^^ ~,^^|| ",
" || ^^, ~~ || ",
" |, ||, ^^ ~~ , ",
" ||||^^ ~~||^^ ",
" ^ ,||||^| ~~||^ ",
" ^^^| , , == ^ ",
" ^|| , |^^,~~,,^ ",
" ^^^^| ^^^~~ ,^ ",
" ^^^^ ^^~~ ~ ",
" ^ , ~~ ~~ ",
" , ~~ |~~ ",
" , , ~~ ,|~~ ",
" | ~~ ,, ",
" | ,|||T|^~~~ ,, ^^ ",
" ||| ^^^~~~ ^^^^ ",
" ^^^|| ~~~ ^^^^^ ",
" ",
],
"TOWN": [
" ",
" OO ",
" #########..####### ",
" # , .. # ] # ",
" # ###### . # ] # ",
" # # ] # . + ]2# ",
" # # ] # . ###### ",
" # #0] + ., # ] # ",
" # # ] # .. + ]1# ",
" # ###### .. # ] # ",
" #.. .. ...###### ",
" #.,. .. . .. , # ",
" #### . 2 # ",
" # + ...... # ",
" #0 # ,..~~..., ..# ",
" #### 1 ~~.. # ",
" # 0 .. , 1 |# ",
" #|| 2 ||| , |||# ",
" #||| ,, ||,|||||# ",
" ################## ",
],
};
const TILES = {
"~": { fg: 3, bg: 5, t: 188, coll: true }, // water.
"|": { fg: 12, t: 185, coll: true }, // tree.
"^": { fg: 2, t: 187, coll: true }, // mountain.
"=": { fg: 1, t: 248, coll: false }, // bridge.
",": { fg: 13, t: 289, coll: false }, // grass.
" ": { fg: 1, t: 0, coll: false }, // empty.
"T": { fg: 15, t: 362, coll: false }, // town.
"O": { fg: 0, t: 0, coll: false }, //
"#": { fg: 7, t: 334, coll: true }, // wall.
".": { fg: 1, t: 346, coll: false }, // stone path.
"+": { fg: 6, t: 234, coll: false }, // doorway.
"]": { fg: 10, t: 74, coll: true }, // force field.
// // Villagers.
"0": { fg: 15, t: 0xd0, coll: true, talk: true },
"1": { fg: 15, t: 0xd3, coll: true, talk: true },
"2": { fg: 15, t: 0xd5, coll: true, talk: true },
}
const MAP_WIDTH = MAPS.OVERWORLD[ 0 ].length;
const MAP_HEIGHT = MAPS.OVERWORLD.length;
const GREETINGS = [
"Greetings, traveler!", "Welcome, adventurer!",
"I'm a placeholder\ncharacter in an\nexample game.",
];
let curMap = "OVERWORLD"; // Initial map name
let px = 7, py = 9; // Initial player position.
let facingLeft = 1; // If true, player is looking to the left.
async function main() {
while ( true ) {
drawMap();
drawPlayer();
await movePlayer();
}
}
function drawPlayer() {
b8.locate( px, py );
b8.color( 15, 0 );
let animation = 'move-right';
if ( facingLeft ) {
animation = 'move-left';
}
b8.drawActor( 14, animation );
}
function drawNPCs() {
for ( let y = 0; y < mapArray.length; y++ ) {
for ( let x = 0; x < mapArray[ y ].length; x++ ) {
if ( NPC_IDS.includes( mapArray[ y ][ x ] ) ) {
b8.color( 15, 0 );
b8.locate( x, y + 1 );
b8.drawActor( parseInt( mapArray[ y ][ x ] ), "idle" );
}
}
}
}
function drawMap() {
// Map.
b8.cls();
b8.locate( 0, 1 );
b8.Tilemap.draw( map, 0, 0 );
// Draw NPCs.
drawNPCs();
// UI.
b8.locate( 1, 22 );
b8.color( 8, 0 );
b8.print(
"ROBIN HP 34/34 Gold:72\n" +
"Paladin Lvl 6 XP:18,390" );
}
async function movePlayer() {
const keyName = await b8.Async.key();
if ( keyName.includes( "ArrowLeft" ) ) facingLeft = 1;
if ( keyName.includes( "ArrowRight" ) ) facingLeft = 0;
// Where does the player want to go?
const candX = px + ( keyName.includes( "ArrowRight" ) ? 1 : 0 ) + ( keyName.includes( "ArrowLeft" ) ? -1 : 0 );
const candY = py + ( keyName.includes( "ArrowDown" ) ? 1 : 0 ) + ( keyName.includes( "ArrowUp" ) ? -1 : 0 );
// Don't let player go beyond edges.
if ( candX <= 0 || candX >= MAP_WIDTH - 1 ) return;
if ( candY <= 0 || candY >= MAP_HEIGHT - 1 ) return;
// What tile is the player trying to enter?
const tile = mapArray[ candY - 1 ][ candX ];
if ( TILES[ tile ].talk ) await randomGreeting();
if ( TILES[ tile ].coll ) return; // Can't walk over a solid tile.
px = candX, py = candY;
// Change maps?
if ( tile === "T" ) goToMap( "TOWN", 10, 2 );
if ( tile === "O" ) goToMap( "OVERWORLD", 7, 15 );
}
function loadMap( mapName ) {
mapArray = b8.Tilemap.convertFromText( MAPS[ mapName ].join( "\n" ) );
return b8.Tilemap.createFromArray( mapArray, TILES );
}
function goToMap( mapName, x, y ) {
map = loadMap( mapName );
curMap = mapName;
px = x;
py = y;
}
async function randomGreeting() {
const greeting = GREETINGS[ Math.floor( Math.random() * GREETINGS.length ) ];
await b8.Async.dialog( `VILLAGER:\n${greeting}` );
}
window.addEventListener(
"load",
() => b8.init(
() => {
goToMap( "OVERWORLD", 7, 9 );
main();
}
)
);
</script>
</head>
<body>
</body>
</html>