Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
*/
inventory_has_empty_slot() {
const { columns, rows } = this.game_ui_options.inventory;
return this.state.inventory.size < columns * rows;
}
/**
* Take a game object as argument and removes its reference from the inventory state.
* @param {MtlGameObject} obj
*/
remove_object_from_inventory(obj) {
this.state.inventory.delete(obj);
}
/**
* Takes a game object as argument, removes the reference from inventory 7
* and delete the object from the scene it's defined into.
* @param {MtlGameObject} obj
*/
consume_game_object(obj) {
this.remove_object_from_inventory(obj);
for (const s of this.scenes) {
const found_obj = s.game_objects.find(o => o === obj);
if (found_obj) {
s.game_objects.splice(s.game_objects.indexOf(found_obj), 1);
break;
}
}
}
/**
* Returns true if the number of scenes having finished to load their loadable resources is equal to the total number of scenes.
* @returns {Boolean}
*/
all_resources_loaded() {
return this.load_state.loadable_elements === this.load_state.loaded_elements;
}
/**
* Returns a concatenation of all game objects of all scenes.
* @returns {Array<MtlGameObject>}
*/
get_game_objects() {
return this.scenes.reduce((acc, scene) => acc.concat(scene.game_objects), []);
}
/**
* Populates the instance with a litteral game desriptor.
* The expected data format is identical to what's exported from the Mentalo app editor,
* or what's returned from the Mentalo API database.
* @param {Object} data
*/
load_data(data) {
this.name = data.name;
this.starting_scene_index = data.starting_scene_index || 0;
this.state.scene = this.starting_scene_index;
this.scenes = data.scenes.map(scene => {
const scene_instance = new MtlScene();
scene_instance.load_data(scene);
scene_instance.on_load_group_complete(() => {
this.load_state.loaded_scenes++;
if (this.load_state.loaded_scenes === this.scenes.length) {
this.on_load_resources_callback && this.on_load_resources_callback();
}
})
return scene_instance;
});
this.game_ui_options = Object.assign(data.game_ui_options, {
general: {
background_color: data.game_ui_options.general.background_color,
animation_canvas_dimensions: Object.assign(data.game_ui_options.general.animation_canvas_dimensions, {
height: function () {
const split_ratio = this.ratio.split(":").map(n => parseInt(n));
const factor = split_ratio[1] / split_ratio[0];
return Number((this.width * factor).toFixed());
},
}),
}
});
}
/**
* Sets the callback to call when all resources of all scenes are fully loaded.
* @param {Function} callback
*/
on_load_resources(callback) {
this.on_load_resources_callback = callback;
}
}
module.exports = MtlGame;
},{"./scene":17,"./scene-types":16}],14:[function(require,module,exports){
"use strict";
/**
* A structure to manage a group of object that extends from the ./Loadable class.
* For each handled loadable, increments a loadable_elements state, and once the loadable has finished
* to load whatever it needs to load, the loaded_elements state is incremented.
*/
class LoadableGroup {
constructor() {
this.loadable_elements = 0;
this.loaded_elements = 0;
}
/**
* Attaches a on_load callback to the Loadable object given as argument.
* Increments the loadable_elements state.
* @param {Loadable} object Any object that extends the Loadable class
*/
add_loadable(object) {
this.loadable_elements++;
/**
* The callback that will be call by the Loadable object when it will have finished to load what it needs to load.
*/
object.on_load(() => {
this.loaded_elements++;
if (this.loadable_elements === this.loaded_elements && this.on_load_group_complete_custom_callback) {
this.on_load_group_complete_custom_callback();
}
});
}
/**
* Defines a custom callback to call when all handled loadable objects have finished loading.
* @param {Function} callback
*/
on_load_group_complete(callback) {
this.on_load_group_complete_custom_callback = callback;
}
}
module.exports = LoadableGroup;
},{}],15:[function(require,module,exports){
"use strict";
/**
* An abstract type that must be inherited by any object that have a resource to load.
* Can be an image or a sound.
*/
class Loadable {
/**
* Initialization of the actual loadable element.
* @param {HTMLElement} loadable_element Image() or Audio() for example
* @param {String} field_name the name of the field that references the loadable element in the instance.
* @param {String} event_name the name of the event that is dispatched by the loadable
* html element when it has loaded a resource ("load" or "loadeddata")
*/
constructor(loadable_element, field_name, event_name) {
this.loaded = false;
this.init_loadable_element(loadable_element, field_name, event_name);
}
/**
* Initializes event listeners for the registered loadable_element.
* @param {HTMLElement} loadable_element
* @param {String} field_name the name of the field that references the element
* @param {String} event_name the event to use for resource loading (images uses onload, audio uses onloadeddata ...)
*/
init_loadable_element(loadable_element, field_name, event_name) {
this[field_name] = loadable_element;
this.load_listener = () => {
this.loaded = true;
this.on_load_callback();
this.clear();
};
this[field_name].addEventListener(event_name, this.load_listener);
this.clear = () => {
this[field_name].removeEventListener(event_name, this.load_listener);
}
}
/**
* Sets a callback to call when the loadable_element has finished to load its resource.
* @param {Function} callback
*/
on_load(callback) {
this.on_load_custom_callback = callback;
}
/**
* The callback that's called when the loadable element as finished loading its resource
*/
on_load_callback() {
this.loaded = true;
this.on_load_custom_callback && this.on_load_custom_callback();
}
}
module.exports = Loadable;
},{}],16:[function(require,module,exports){
/**
* An enum like object to describe the 2 possible type that can have a MtlScene
*/
module.exports = {
PLAYABLE: "Playable",
CINEMATIC: "Cinematic",
};
},{}],17:[function(require,module,exports){
"use strict";
const SCENE_TYPES = require("./scene-types");
const MtlAnimation = require("./animation");
const MtlSoundTrack = require("./sound-track");
const MtlChoice = require("./choice");
const MtlGameObject = require("./game-object");
const LoadableGroup = require("./loadable-group");
/**
* The type used for the scenes of a MtlGame.
* Extends the LoadableGroup class because it manages multiple objects of the class Loadable.
*/
class MtlScene extends LoadableGroup {
/**
* Initializes the instance with default empty data.
*/
constructor() {
super();
const default_data = {
name: "",
_type: SCENE_TYPES.PLAYABLE,
animation: new MtlAnimation(),
sound_track: new MtlSoundTrack(),
choices: [],
text_box: "",
game_objects: [],
cinematic_duration: 0,
end_cinematic_options: {
destination_scene_index: -1,
quit: false,
}
};
this.name = default_data.name;
this._type = default_data._type;
this.animation = default_data.animation;
this.sound_track = default_data.sound_track;
this.choices = default_data.choices;
this.text_box = default_data.text_box;
this.game_objects = default_data.game_objects;
this.cinematic_duration = default_data.cinematic_duration || 0;
this.end_cinematic_options = default_data.end_cinematic_options;
}
/**
* Populates the instance from a litteral descriptor.
* Creates and populates the instances of Animation, Soundtrack and GameObjects
* @param {Object} data
*/
load_data(data) {
this.name = data.name;
this._type = data._type;
const animation = new MtlAnimation();
animation.init(data.animation);
this.animation.clear();
this.animation = animation;
this.animation.image.src && this.add_loadable(this.animation);
const sound_track = new MtlSoundTrack();
sound_track.init(data.sound_track);
this.sound_track.clear();
this.sound_track = sound_track;
this.sound_track.src && this.add_loadable(this.sound_track);
this.choices = data.choices.map(c => new MtlChoice(c));
this.text_box = data.text_box;
this.cinematic_duration = data.cinematic_duration || 0;
this.game_objects = data.game_objects.map(gob => {
const inst = new MtlGameObject();
inst.load_data(gob);
this.add_loadable(inst);
return inst;
});
this.end_cinematic_options = data.end_cinematic_options;
}
}
module.exports = MtlScene;
},{"./animation":10,"./choice":11,"./game-object":12,"./loadable-group":14,"./scene-types":16,"./sound-track":18}],18:[function(require,module,exports){
"use strict";
const Loadable = require("./loadable");
/**
* The data type used to represent and manage the soundtrack of a scene
*/
class MtlSoundTrack extends Loadable {
constructor() {
super(new Audio(), "audio", "loadeddata");
this.name = "";
this.initialized = false;
}
/**
* Reset the instance to empty values.
*/
reset() {
this.init_loadable_element(new Audio(), "audio", "loadeddata");
this.name = "";
this.initialized = false;
}
/**
* Populates the instance from a litteral descriptor
* @param {Object} data
*/
init(data) {
this.empty = data.src === "";
this.audio.src = data.src;
this.name = data.name;
this.audio.loop = data._loop;
this.initialized = true;
}
/**
* Plays the audio resource.
*/
play() {
this.audio.play();
}
/**
* Stops the audio player.
*/
stop() {
this.audio.pause();
this.audio.currentTime = 0;
}
/**
* Returns the state of the inner audio element
* @returns {Boolean}
*/
is_playing() {
return !this.audio.paused
}
}
module.exports = MtlSoundTrack;
},{"./loadable":15}],19:[function(require,module,exports){
"use strict";
const FrameRateController = require("../lib/frame-rate-controller");
const { get_optimal_visible_foreground_color } = require("../lib/color-tools");
const { get_canvas_font, get_canvas_char_size } = require("../lib/font-tools");
const SCENE_TYPES = require("../model/scene-types");
const ChoiceCpt = require("../ui-components/choice-cpt");
const ChoicesPanelCpt = require("../ui-components/choices-panel-cpt");
const ClosingIconCpt = require("../ui-components/closing-icon-cpt");
const GameObjectCpt = require("../ui-components/game-object-cpt");
const InventoryCpt = require("../ui-components/inventory-cpt");
const InventoryObjectCpt = require("../ui-components/inventory-object-cpt");
const InventorySlotCpt = require("../ui-components/inventory-slot-cpt");
const SceneAnimationCpt = require("../ui-components/scene-animation-cpt");
const TextBoxCpt = require("../ui-components/text-box-cpt");
const UserErrorPopup = require("../ui-components/user-error-popup");
/**
* The class that handles all interactions with the canvas 2D drawing context to draw the game.
*/
class MtlRender {
constructor(params) {
this.params = params;
const frame_rate = this.params.frame_rate || 30;
this.fps_controller = new FrameRateController(frame_rate);
this.params.container.style.backgroundColor = this.params.get_game_settings("general").background_color;
this.canvas = document.createElement("canvas");
this.canvas.style.backgroundColor = "black";
window.mentalo_drawing_context = this.canvas.getContext("2d");
this.canvas_dimensions = {};
this.canvas_zones = {};
this.event_listeners = {
game_objects: [],
inventory: [],
text_box: [],
};
this.state = {
user_error_popup: { is_set: false, text: "", on_close: function () { } }
};
this.loading_frame = 0;
}
/**
* Initializes the rendering of the game, creates the components, the canvas zones and requests full screen.
*/
init() {
if (this.params.fullscreen) {
this.set_full_screen();
}
this.set_canvas_dimensions();
this.create_canvas_zones();
this.params.container.appendChild(this.canvas);
const ctx = window.mentalo_drawing_context;
ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
ctx.msImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
this.create_components();
}
/**
* Removes the event listeners attached to the components.
*/
clear_event_listeners() {
Object.values(this.components).forEach(cpt => {
cpt.clear_event_listeners();
});
}
/**
* Removes the elements rendered as children of the rendering components
* (Example a TextBoxCpt is a child the scene_animation component)
* @param {Object} options can defined a set of constructor names to exclude from clean up
*/
clear_children(options = { exclude: [] }) {
Object.values(this.components).forEach(cpt => {
cpt.clear_children(options);
});
}
/**
* Reset the scene text box visibility state to true.
* The text box visibility state is controlled by the scene_animation component and not directly by the text box component.
*/
reset_text_box_visibility() {
this.components.scene_animation.set_text_box_visibility(true)
}
/**
* Tries to enable the window fullscreen mode.
*/
set_full_screen() {
const body = document.body;
body.requestFullScreen = body.requestFullScreen
|| body.webkitRequestFullScreen
|| body.msRequestFullscreen
|| body.mozRequestFullScreen;
try {
body.requestFullScreen();
} catch (err) {
console.error(err.message)
}
}
/**
* Exits the window fullscreen mode
*/
exit_fullscreen() {
if (document.fullscreenElement) {
document.exitFullscreen();
}
}
/**
* Updates the error popup state with new values given as argument.
* @param {Object} params Must have a text<String> entry and a on_close<Function> entry.
*/
set_user_error_popup(params) {
this.state.user_error_popup = {
is_set: true,
text: params.text,
on_close: function () {
params.on_close && params.on_close();
},
};
this.clear_event_listeners();
// Clears children excluding everything but errorpopup
this.clear_children({ exclude: ["TextBoxCpt", "InventoryCpt", "ChoicesPanelCpt", "GameObjectCpt"] });
}
/**
* Unsets error popup
*/
clear_user_error_popup() {
this.reset_user_error_popup();
this.clear_event_listeners();
this.clear_children({ exclude: ["TextBoxCpt", "InventoryCpt", "ChoicesPanelCpt", "GameObjectCpt"] });
}
/**
* Sets error popup to empty values
*/
reset_user_error_popup() {
this.state.user_error_popup = {
is_set: false,
text: "",
on_close: function () { }
};
}
/**
* Callback called when error popup is closed
*/
on_close_user_error_popup() {
this.state.user_error_popup.on_close();
this.clear_user_error_popup();
}
/**
* Parses game data and calculates optimal dimensions for the canvas.
*/
set_canvas_dimensions() {
const { get_game_settings } = this.params;
const screen_dim = {
width: window.screen.width,
height: window.screen.height,
};
const choices_panel_settings = get_game_settings("choices_panel")
const ctx = this.canvas.getContext("2d");
this.canvas_dimensions = (() => {
const image_h = get_game_settings("general").animation_canvas_dimensions.height();
const image_w = get_game_settings("general").animation_canvas_dimensions.width;
const inventory_w = this.get_inventory_base_width();
const base_w = image_w + inventory_w;
const choices_panel_h = (() => {
ctx.save();
ctx.font = get_canvas_font(choices_panel_settings);
const max_lines_per_row = this.get_choices_max_lines_per_row();
const char_size = get_canvas_char_size(ctx);
const text_line_h = char_size.text_line_height;
ctx.restore();
return (max_lines_per_row[0] * text_line_h + (2 * choices_panel_settings.choice_padding * char_size.width))
+ (max_lines_per_row[1] > 0
? (max_lines_per_row[1] * text_line_h + (2 * choices_panel_settings.choice_padding * char_size.width))
: 0)
+ (2 * choices_panel_settings.container_padding * char_size.width);
})();
const max_playground_width = screen_dim.width - 200;
const max_playground_height = screen_dim.height - 200;
const base_h = image_h + choices_panel_h;
const screen_ratio = screen_dim.width / screen_dim.height;
const playground_ratio = base_w / base_h;
const result = {
width: 0,
height: 0,
image: { width: 0, height: 0 },
inventory: { width: 0, height: 0 },
choices_panel: { width: 0, height: 0 },
scale_ratio: 1,
};
if (screen_ratio > playground_ratio) {
// Screen is more panoramic than game canvas so we scale game canvas to maximum height
const scaled_w = max_playground_height * playground_ratio
result.width = scaled_w <= max_playground_width ? scaled_w : max_playground_width;
} else {
// Image is more panoramic so its scaled to the maximum width
const scaled_h = max_playground_width * (base_h / base_w)
result.height = scaled_h <= max_playground_height ? scaled_h : max_playground_height;
result.width = result.height * playground_ratio;
}
result.scale_ratio = result.width / base_w;
result.image = {
width: image_w * result.scale_ratio,
height: image_h * result.scale_ratio,
};
result.inventory = {
width: inventory_w * result.scale_ratio,
height: result.image.height,
};
result.choices_panel = {
width: result.width,
height: choices_panel_h * result.scale_ratio,
};
result.height = result.image.height + result.choices_panel.height;
return result;
})();
this.canvas.width = this.canvas_dimensions.width;
this.canvas.height = this.canvas_dimensions.height;
}
/**
* @returns {Integer} The maximum number of rows that the choices_panel can have in the loaded game.
*/
get_max_choices_row_per_scene() {
let choices_max_row_nb = 1;
if (Math.max(...this.params.get_game_scenes().map(s => s.choices.length)) > 2) {
choices_max_row_nb = 2
}
return choices_max_row_nb;
}
/**
* @returns {Integer} The maximum number of text rows that a choice can have in the loaded game
*/
get_choices_max_lines_per_row() {
const choices_max_row_nb = this.get_max_choices_row_per_scene();
const formatted_choices_scenes = this.get_scenes_formatted_choices();
// get the largest number un text lines per choice row
const max_lines_per_row = [0, 0];
Array.from({ length: choices_max_row_nb }).forEach((_row, i) => {
const slice_index = i === 0 ? [0, 2] : [2, 4];
const largest = formatted_choices_scenes
.map(s_choices => Math.max(...s_choices
.slice(...slice_index)
.map(c => c.text_lines.length)))
.reduce((res, nb) => Math.max(res, nb), 0);
max_lines_per_row[i] = largest;
});
return max_lines_per_row;
}
/**
* The width of the inventory panel
* @returns {Integer}
*/
get_inventory_base_width() {
const { get_game_settings } = this.params;
const image_h = get_game_settings("general").animation_canvas_dimensions.height();
const inventory_style = get_game_settings("inventory");
const gap = inventory_style.gap;
const h = image_h - (2 * inventory_style.padding);
const gap_h = (inventory_style.rows - 1) * gap;
const gap_w = (inventory_style.columns - 1) * gap
const slot_side = (h - gap_h) / inventory_style.rows;
return (inventory_style.columns * slot_side)
+ (2 * inventory_style.padding)
+ gap_w;
}
/**
* Parses the raw text of each choices in each scene and returns them with
* an additional text_lines field with the text split into lines ready to be rendered in canvas.
* @returns {Array<{...MtlChoice, text_lines<Array<String>>}}
*/
get_scenes_formatted_choices() {
const scenes = this.params.get_game_scenes();
const { get_game_settings } = this.params;
const settings = this.params.get_game_settings("choices_panel");
const ctx = window.mentalo_drawing_context;
ctx.save();
ctx.font = get_canvas_font(settings);
const char_size = get_canvas_char_size(ctx);
ctx.restore();
const container_padding = settings.container_padding * char_size.width;
const choice_padding = settings.choice_padding * char_size.width;
const container_width = get_game_settings("general").animation_canvas_dimensions.width
+ this.get_inventory_base_width()
- (2 * container_padding);
const choice_max_width = ((container_width / 2) - (2 * choice_padding)) * .9; // width * .9 is an error offset
const max_chars_per_row = choice_max_width / char_size.width;
return scenes.map(s => s.choices.map(c => {
const words = c.text.split(" ");
const lines = [""];
let line_i = 0;
words.forEach(w => {
if ((lines[line_i] + w).length >= max_chars_per_row) {
line_i++;
lines.push("");
}
lines[line_i] = `${lines[line_i]}${lines[line_i] === "" ? "" : " "}${w}`;
});
return Object.assign({ ...c }, { text_lines: lines });
}));
}
/**
* Precalculates the bounding boxes for each canvas zone.
*/
create_canvas_zones() {
const metrics = this.canvas_dimensions;
const { get_game_settings } = this.params;
this.canvas_zones = {
root: {
left: 0, top: 0,
right: metrics.width, bottom: metrics.height,
width: metrics.width, height: metrics.height,
padding: 0,
},
scene_animation: {
left: 0,
top: 0,
right: metrics.image.width,
bottom: metrics.image.height,
width: metrics.image.width,
height: metrics.image.height,
clear_color: "black",
padding: 0,
},
inventory: {
left: metrics.image.width,
top: 0,
right: metrics.width,
bottom: metrics.inventory.height,
width: metrics.width - metrics.image.width,
height: metrics.inventory.height,
clear_color: get_game_settings("inventory").background_color,
padding: get_game_settings("inventory").padding,
},
choices_panel: {
left: 0,
top: metrics.image.height,
right: metrics.width,
bottom: metrics.height,
width: metrics.width,
height: metrics.height - metrics.image.height,
clear_color: get_game_settings("choices_panel").background_color,
padding: get_game_settings("choices_panel").container_padding
},
};
}
/**
* @returns {Boolean} true if scene is of type Playable
*/
scene_is_not_cinematic() {
return this.params.get_scene()._type === SCENE_TYPES.PLAYABLE;
}
/**
* Creates the tree of all the components to render. All component extends the UiComponent class.
* The root component instances (Scene animation, Inventory, Choices panel) will only be created once,
* but children components are functions of their parents so they are recreated when get_children is called on a parent component.
* For example A TextBoxCpt is a children a SceneAnimationCpt, so the text box component will be recreated each time
* scene_animation.get_children() is called. This allow to display those components as functions of dynamical states.
*/
create_components() {
const { get_game_settings, get_inventory, get_scene, get_game_scenes } = this.params;
const { scale_ratio } = this.canvas_dimensions;
const scene_is_not_cinematic = this.scene_is_not_cinematic.bind(this);
this.components = {
scene_animation: new SceneAnimationCpt({
bounding_zone: this.canvas_zones.scene_animation,
get_animation: () => get_scene().animation,
next_frame_ready: () => this.fps_controller.nextFrameReady(),
get_children: () => {
const scene = get_scene();
return scene.game_objects.map(o => {
const parent_zone = this.canvas_zones.scene_animation;
const obj_pos = {
x: (o.position.x * scale_ratio) + parent_zone.left,
y: (o.position.y * scale_ratio) + parent_zone.top,
};
const obj_dim = {
w: o.image.width * scale_ratio,
h: o.image.height * scale_ratio,
};
const obj_bounds = {
left: obj_pos.x,
right: obj_pos.x + obj_dim.w,
top: obj_pos.y,
bottom: obj_pos.y + obj_dim.h,
width: obj_dim.w,
height: obj_dim.h,
};
const game_objects_cpt_params = {
bounding_zone: obj_bounds,
position: obj_pos,
dimensions: obj_dim,
image: o.image,
is_in_inventory: () => get_inventory().has(o),
};
const obj_cpt = new GameObjectCpt(game_objects_cpt_params);
obj_cpt.add_event_listener({
event_type: "mousemove",
listener: e => {
if (!get_inventory().has(o)) {
const cursor_is_over_obj = e.offsetX >= obj_bounds.left
&& e.offsetX <= obj_bounds.right
&& e.offsetY >= obj_bounds.top
&& e.offsetY <= obj_bounds.bottom;
obj_cpt.state.draw_border = cursor_is_over_obj;
}
}
});
obj_cpt.add_event_listener({
event_type: "click",
listener: e => {
if (!get_inventory().has(o)
&& e.offsetX >= obj_bounds.left
&& e.offsetX <= obj_bounds.right
&& e.offsetY >= obj_bounds.top
&& e.offsetY <= obj_bounds.bottom) {
this.params.on_game_object_click(o);
this.clear_event_listeners();
this.clear_children({ exclude: "TextBoxCpt" });
}
}
});
return obj_cpt;
})
.concat(scene.text_box ? [
(() => {
const text_box_settings = get_game_settings("text_boxes")
const text_box_bounds = (() => {
const parent_zone = this.canvas_zones.scene_animation;
const padding = text_box_settings.padding * scale_ratio;
const margin = text_box_settings.margin * scale_ratio;
const left = parent_zone.left + margin;
const right = parent_zone.right - margin;
const bottom = parent_zone.bottom - margin;
const top = bottom - (2 * padding);
return {
left,
right,
top,
bottom,
width: right - left,
height: bottom - top,
}
})();
const closing_icon_radius = 10 * scale_ratio;
const closing_icon_center = {
x: text_box_bounds.right - (closing_icon_radius / 2),
y: text_box_bounds.top + (closing_icon_radius / 2),
};
const closing_icon_params = {
color: text_box_settings.font_color,
radius: closing_icon_radius,
center: closing_icon_center,
background_color: text_box_settings.background_color,
line_width: Math.floor(2 * scale_ratio),
bounding_zone: {
left: closing_icon_center.x - closing_icon_radius,
right: closing_icon_center.x + closing_icon_radius,
top: closing_icon_center.y - closing_icon_radius,
bottom: closing_icon_center.y + closing_icon_radius,
width: 2 * closing_icon_radius,
height: 2 * closing_icon_radius,
},
};
const text_box = new TextBoxCpt({
text: get_scene().text_box,
settings: Object.assign({ ...text_box_settings }, {
font_size: text_box_settings.font_size * scale_ratio,
padding: text_box_settings.padding * scale_ratio,
margin: text_box_settings.margin * scale_ratio,
rounded_corners_radius: text_box_settings.rounded_corners_radius * scale_ratio,
border_width: text_box_settings.border_width * scale_ratio,
}),
bounding_zone: text_box_bounds,
get_visibility_state: () => this.components.scene_animation.state.text_box_visible,
get_children: () => [new ClosingIconCpt(closing_icon_params)],
});
const close_text_box_icon = text_box.children[0];
text_box.children[0].add_event_listener({
event_type: "click",
listener: e => {
const bounds = close_text_box_icon.params.bounding_zone;
const click_over_icon = e.offsetX >= bounds.left
&& e.offsetX <= bounds.right
&& e.offsetY >= bounds.top
&& e.offsetY <= bounds.bottom;
const { scene_animation } = this.components;
if (click_over_icon && scene_animation.state.text_box_visible) {
scene_animation.set_text_box_visibility(false);
}
}
});
return text_box;
})(),
] : [])
.concat(this.state.user_error_popup.is_set ? [
(() => {
const { text } = this.state.user_error_popup;
const default_font = get_canvas_font();
const use_settings = get_game_settings("text_boxes");
const parent_bounds = this.canvas_zones.scene_animation;
const ctx = window.mentalo_drawing_context;
ctx.save();
ctx.font = default_font;
const char_size = get_canvas_char_size(ctx);
ctx.restore();
const popup_width = parent_bounds.width / 3;
const padding = char_size.height * 2;
const max_chars_per_row = (popup_width - (2 * padding)) / char_size.width;
const text_lines = [""];
let line_i = 0;
text.split(" ").forEach(word => {
if (`${text_lines[line_i]}${word} `.length > max_chars_per_row) {
line_i++;
text_lines.push("");
}
text_lines[line_i] += `${word} `;
});
const line_h = char_size.text_line_height;
const popup_height = (text_lines.length * line_h) + (2 * padding);
const background_color = use_settings.background_color;
const text_color = use_settings.font_color;
const popup_bounds = {
left: parent_bounds.left + (parent_bounds.width / 2) - (popup_width / 2),
top: parent_bounds.top + (parent_bounds.height / 2) - (popup_height / 2),
right: parent_bounds.left + (parent_bounds.width / 2) + (popup_width / 2),
bottom: parent_bounds.top + (parent_bounds.height / 2) - (popup_height / 2) + popup_height,
width: popup_width,
height: popup_height,
clear_color: background_color,
};
const popup_params = {
bounding_zone: popup_bounds,
font: default_font,
font_metrics: char_size,
text_lines,
text_color,
padding,
get_children: () => {
const center = {
x: popup_bounds.right - 5,
y: popup_bounds.top + 5,
};
const radius = 15;
const closing_icon_bounds = {
left: center.x - radius,
right: center.x + radius,
top: center.y - radius,
bottom: center.y + radius,
width: radius * 2,
height: radius * 2,
};
const closing_icon_params = {
color: text_color,
radius,
center,
background_color,
line_width: 2,
bounding_zone: closing_icon_bounds,
};
const closing_icon = new ClosingIconCpt(closing_icon_params);
closing_icon.add_event_listener({
event_type: "click",
listener: e => {
if (e.offsetX >= closing_icon_bounds.left
&& e.offsetX <= closing_icon_bounds.right
&& e.offsetY >= closing_icon_bounds.top
&& e.offsetY <= closing_icon_bounds.bottom) {
this.on_close_user_error_popup();
}
}
});
return [closing_icon];
}
};
const popup = new UserErrorPopup(popup_params);
return popup;
})()