forked from KilledByAPixel/LittleJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
127 lines (107 loc) Β· 4.15 KB
/
game.js
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
/*
LittleJS Hello World Starter Game
*/
'use strict';
// popup errors if there are any (help diagnose issues on mobile devices)
if (debug)
onerror = (...parameters)=> alert(parameters);
// game variables
let particleEmiter, clickCount = 0;
// sound effects
const sound_click = new Sound([.5,.5]);
// medals
const medal_example = new Medal(0, 'Example Medal', 'Medal description goes here.');
medalsInit('Hello World');
///////////////////////////////////////////////////////////////////////////////
function gameInit()
{
// create tile collision and visible tile layer
initTileCollision(vec2(32,16));
const tileLayer = new TileLayer(vec2(), tileCollisionSize);
const pos = vec2();
// get level data from the tiles image
const imageLevelDataRow = 1;
mainContext.drawImage(tileImage,0,0);
for (pos.x = tileCollisionSize.x; pos.x--;)
for (pos.y = tileCollisionSize.y; pos.y--;)
{
const data = mainContext.getImageData(pos.x, 16*(imageLevelDataRow+1)-pos.y-1, 1, 1).data;
if (data[0])
{
setTileCollisionData(pos, 1);
const tileIndex = 1;
const direction = randInt(4)
const mirror = randInt(2);
const color = randColor();
const data = new TileLayerData(tileIndex, direction, mirror, color);
tileLayer.setData(pos, data);
}
}
tileLayer.redraw();
// move camera to center of collision
cameraPos = tileCollisionSize.scale(.5);
cameraScale = 32;
// enable gravity
gravity = -.01;
// create particle emitter
const center = tileCollisionSize.scale(.5).add(vec2(0,9));
particleEmiter = new ParticleEmitter(
center, 0, 1, 0, 500, PI, // pos, angle, emitSize, emitTime, emitRate, emiteCone
0, vec2(16), // tileIndex, tileSize
new Color(1,1,1), new Color(0,0,0), // colorStartA, colorStartB
new Color(1,1,1,0), new Color(0,0,0,0), // colorEndA, colorEndB
2, .2, .2, .1, .05, // particleTime, sizeStart, sizeEnd, particleSpeed, particleAngleSpeed
.99, 1, 1, PI, .05, // damping, angleDamping, gravityScale, particleCone, fadeRate,
.5, 1, 1 // randomness, collide, additive, randomColorLinear, renderOrder
);
particleEmiter.elasticity = .3;
particleEmiter.trailScale = 2;
}
///////////////////////////////////////////////////////////////////////////////
function gameUpdate()
{
if (mouseWasPressed(0))
{
// play sound when mouse is pressed
sound_click.play(mousePos);
// change particle color
particleEmiter.colorStartA = new Color;
particleEmiter.colorStartB = randColor();
particleEmiter.colorEndA = particleEmiter.colorStartA.scale(1,0);
particleEmiter.colorEndB = particleEmiter.colorStartB.scale(1,0);
// unlock medals
medal_example.unlock();
}
// move particles to mouse location if on screen
if (mousePosScreen.x || mousePosScreen.y)
particleEmiter.pos = mousePos;
}
///////////////////////////////////////////////////////////////////////////////
function gameUpdatePost()
{
}
///////////////////////////////////////////////////////////////////////////////
function gameRender()
{
// draw a grey square in the background without using webgl
drawRect(cameraPos, tileCollisionSize.add(vec2(5)), new Color(.2,.2,.2), 0, 0);
}
///////////////////////////////////////////////////////////////////////////////
function gameRenderPost()
{
// draw to overlay canvas for hud rendering
const drawText = (text, x, y, size=70) =>
{
overlayContext.textAlign = 'center';
overlayContext.textBaseline = 'top';
overlayContext.font = size + 'px arial';
overlayContext.fillStyle = '#fff';
overlayContext.lineWidth = 3;
overlayContext.strokeText(text, x, y);
overlayContext.fillText(text, x, y);
}
drawText('Hello World', overlayCanvas.width/2, 40);
}
///////////////////////////////////////////////////////////////////////////////
// Startup LittleJS Engine
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, 'tiles.png');