-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
167 lines (136 loc) · 6.18 KB
/
main.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
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
//////////////////////////////////////////////////////////////////////
// main //
//////////////////////////////////////////////////////////////////////
// Shared variables
let mgr, cnv, dropedFile, game, mood, sceneIndex, gateIndex, toolPanelsContainer, status, autoSaveStatus, template;
//
let gameWidth = 800;
let gameHeight = 600;
let defaultImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAIAAAAVFBUnAAABhWlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9Tix9UBa0g4pChOlkQFXHUKhShQqgVWnUwufQLmjQkKS6OgmvBwY/FqoOLs64OroIg+AHi5uak6CIl/i8ptIj14Lgf7+497t4BQrXINKttHNB020zEomIqvSq2vyKAPnRiAD0ys4w5SYqj5fi6h4+vdxGe1frcn6NbzVgM8InEs8wwbeIN4ulN2+C8TxxieVklPiceM+mCxI9cVzx+45xzWeCZITOZmCcOEYu5JlaamOVNjXiKOKxqOuULKY9VzluctWKZ1e/JXxjM6CvLXKc5jBgWsQQJIhSUUUARNiK06qRYSNB+tIV/yPVL5FLIVQAjxwJK0CC7fvA/+N2tlZ2c8JKCUSDw4jgfI0D7LlCrOM73sePUTgD/M3ClN/ylKjDzSXqloYWPgN5t4OK6oSl7wOUOMPhkyKbsSn6aQjYL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AAAACAVwP8kwAB1rbC6AAAAABJRU5ErkJggg=='
let statusFormat = '<span><b>mode: </b>{mode}</span><span><b>time: </b>{time}</span><span><b>last autosave: </b>{autosave}</span><span><b>scene: </b>{sceneIndex}/{scenesLength}</span><span><b>mood: </b>{mood}</span>'
function preload() {
template = loadStrings('template.html');
}
function setup() {
// Dom
cnv = createCanvas(gameWidth, gameHeight);
cnv.parent('canvasContainer');
cnv.drop(gotFile);
toolPanelsContainer = select('#toolPanelsContainer');
statusBar = select('#statusBar');
// Game
game = getItem('game');
// Scene Manager
mgr = new SceneManager();
mgr.showScene(home);
// Mood
mood = getItem('mood')
if(mood === null){
mood = 60;
}
//
autoSaveStatus=''
}
function draw(){
let mgrSceneName = mgr.scene? mgr.scene.oScene.name : '';
if(frameCount%600 == 0 && mgrSceneName != 'home'){
try{
storeItem('game', game);
storeItem('mood', mood)
autoSaveStatus = timeToStr()
}
catch (e) {
autoSaveStatus = 'failed'
}
}
mood -= 0.02;
mood = constrain(mood, 0, 100);
mgr.draw();
let newStatus = formatString(statusFormat, {
mode:mgrSceneName,
time:timeToStr(),
autosave:autoSaveStatus,
sceneIndex:sceneIndex||0,
scenesLength:game? game.scenes.length-1 : 0,
mood:getToolsMood()
});
if(status != newStatus){
status = newStatus;
statusBar.html(status);
}
}
function mouseMoved(){
mood += 0.1
}
function mousePressed(){
mgr.handleEvent("mousePressed");
}
function mouseDragged(){
mgr.handleEvent("mouseDragged");
}
function mouseReleased(){
mgr.handleEvent("mouseReleased");
}
function keyPressed(){
mgr.handleEvent("keyPressed");
}
function gotFile(file){
dropedFile = file;
mgr.handleEvent("gotFile");
if (file.type === 'image') {
mgr.handleEvent('gotImage');
}
}
function resetSceneSelect(element, index){
element.html('');
for(i=0; i<game.scenes.length; i++){
element.option(i);
}
element.option(game.scenes.length + ' (new)', game.scenes.length);
element.selected(index);
}
function getToolsMood(){
let moods = ['<3', ':)', ':|', ':('];
return moods[floor(map(mood, 100, 0, 0, moods.length-1))];
}
function getToolsMoodText(){
let moods = ['in love', 'happy', 'bored', 'sad'];
return moods[floor(map(mood, 100, 0, 0, moods.length-1))];
}
// Add a new scene to the game and returns the new scene index.
function newScene(){
sceneId = game.scenes.length-1;
scene = {
imageData:'',
gates:[],
sceneId:sceneId,
};
return game.scenes.push(scene)-1;
}
// Add a new gate to the game and returns the new gate index.
function newGate(){
gate = {
poly: [],
sceneId: '0',
};
return game.scenes[sceneIndex].gates.push(gate)-1;
}
// Return true if the mouse is in the given poly.
// Poly must be a coords array.
function mouseInPoly(poly){
var vectorArray = []
for(let i = 0; i < poly.length ; i+=2){
vectorArray.push(createVector(poly[i], poly[i+1]));
}
return collidePointPoly(mouseX, mouseY, vectorArray);
}
function formatString (str, values) {
for (let key in values) {
str = str.replace(new RegExp("\\{" + key + "\\}", "gi"), values[key]);
}
return str;
}
function timeToStr() {
let timeFormat = '{h}:{m}:{s}'
return formatString(timeFormat, {h:nf(hour(),2), m:nf(minute(),2), s:nf(second(),2)});
}