-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
209 lines (185 loc) · 4.07 KB
/
index.js
File metadata and controls
209 lines (185 loc) · 4.07 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
// defined in webpack
/* global ASSET_NAMES */
export const options = {
loadingTextures: [
t.text({
color: "black",
text: "Loading...",
}),
],
assets: ASSET_NAMES,
dimensions: "scale-up",
};
import { makeSprite, t } from '@replay/core';
import * as playset from './playset';
export const gameProps = {
id: 'Game',
size: {
landscape: {
width: 600,
height: 400,
maxWidthMargin: 150,
},
portrait: {
width: 400,
height: 600,
maxHeightMargin: 150,
},
},
defaultFont: {
name: 'Courier',
size: 10,
},
};
export const Game = makeSprite({
init() {
return {
player: {
x: 0,
y: 0,
direction: 'down',
walking: false
}
};
},
loop({ state, device }) {
const { keysDown } = device.inputs;
let { player } = state;
const xDir = keysDown.ArrowLeft ? -1 : keysDown.ArrowRight ? 1 : 0;
const yDir = keysDown.ArrowDown ? -1 : keysDown.ArrowUp ? 1 : 0;
const walkSpeed = 2;
player.x += xDir * walkSpeed;
player.y += yDir * walkSpeed;
player.direction = 'down';
if (yDir === 1) {
player.direction = 'up'
} else if (yDir === -1) {
player.direction = 'down'
} else if (xDir === 1) {
player.direction = 'right'
} else if (xDir === -1) {
player.direction = 'left'
}
player.walking = (xDir !== 0 || yDir !== 0);
return {
player
};
},
render({ state, device }) {
// minimum of options
const blameFlame = playset.Animation({
id: 'blue-flame',
x: -100,
y: 0,
fileName: 'blue-flame.png',
columns: 3,
rows: 1,
fps: 10,
width: 50,
height: 50,
});
// using the x/y `frameArray` syntax in a larger spritesheet
const orangeFlame = playset.Animation({
id: 'orange-flame',
x: 100,
y: 0,
fileName: 'things.png',
columns: 12,
rows: 8,
fps: 10,
width: 50,
height: 50,
frameArray: [{x: 0, y: 4}, {x: 1, y: 4}, {x: 2, y: 4}],
});
const frameArrays = {
up: [39, 40, 41, 40],
down: [3, 4, 5, 4],
left: [15, 16, 17, 16],
right: [27, 28, 29, 28]
}
// example of dynamically changing `frameArray`
// and `playing` to swap between animations
const { player } = state;
const playerSprite = playset.Animation({
id: 'player',
x: player.x,
y: player.y,
fileName: 'characters.png',
columns: 12,
rows: 8,
fps: 8,
width: 50,
height: 50,
frameArray: frameArrays[player.direction],
playing: player.walking
});
// basic pattern
const path = playset.Pattern({
id: 'background-path',
x: 0,
y: 0,
width: 150,
height: 150,
tileWidth: 50,
tileHeight: 50,
fileName: 'path.png'
});
// full-screen pattern
const grass = playset.Pattern({
id: 'background-grass',
x: 0,
y: 0,
width: device.size.deviceWidth,
height: device.size.deviceHeight,
tileWidth: 50,
tileHeight: 50,
fileName: 'grass.png'
});
const button = playset.Button({
id: 'button',
x: -100,
y: 150,
width: 100,
height: 30,
font: { name: 'Papyrus', size: 15 },
text: 'Click me',
color: 'white',
colorPressed: 'gray',
onPress: () => alert('button pressed')
});
const transitionTest = playset.Transition({
id: 'transition-test',
x: 150,
y: -150,
origin: {
scaleX: 0.8,
rotation: -5,
},
target: {
scaleX: 1.4,
rotation: 5,
},
duration: 2000,
playback: 'bounce',
sprites: [
t.rectangle({
width: 120,
height: 15,
}),
t.text({
text: 'Transition example',
color: 'black',
}),
]
});
return [
grass,
path,
playerSprite,
orangeFlame,
blameFlame,
button,
transitionTest,
];
},
});