Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
games.js 145 KiB
Newer Older
  • Learn to ignore specific revisions
  • 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
                            ] : []);
                    },
                }),
                inventory: new InventoryCpt({
                    bounding_zone: this.canvas_zones.inventory,
                    get_inventory,
                    is_visible: scene_is_not_cinematic,
                    invisible_clear_color: get_game_settings("general").background_color,
                    get_children: () => {
                        const slots = [];
                        let settings = get_game_settings("inventory");
                        settings = {
                            ...settings,
                            slot_rounded_corner_radius: settings.slot_rounded_corner_radius * scale_ratio,
                            slot_border_width: settings.slot_border_width * scale_ratio,
                        };
    
                        const slots_zone = {
                            left: this.canvas_zones.inventory.left + settings.padding,
                            top: this.canvas_zones.inventory.top + settings.padding,
                            right: this.canvas_zones.inventory.right - settings.padding,
                            bottom: this.canvas_zones.inventory.bottom - settings.padding,
                            width: this.canvas_zones.inventory.width - (2 * settings.padding),
                            height: this.canvas_zones.inventory.height - (2 * settings.padding)
                        };
    
                        const gap = settings.gap;
                        const h = slots_zone.height;
                        const gap_h = (settings.rows - 1) * gap;
                        const slot_side = (h - gap_h) / settings.rows;
                        const slots_total_w = (slot_side * settings.columns) + (gap * (settings.columns - 1));
                        const center_slot_x_offset = (slots_zone.width - (slots_total_w)) / 2;
    
                        let slot_i = 0;
                        Array.from({ length: settings.rows }).forEach((_row, i) => {
                            const top = slots_zone.top + (i * (slot_side + gap));
                            Array.from({ length: settings.columns }).forEach((_col, j) => {
                                const left = slots_zone.left + (j * (slot_side + gap)) + center_slot_x_offset;
    
                                const slot_bounds = {
                                    left,
                                    top,
                                    right: left + slot_side,
                                    bottom: top + slot_side,
                                    width: slot_side,
                                    height: slot_side
                                };
    
                                const stroke_color = get_optimal_visible_foreground_color(settings.background_color);
    
                                const slot_params = {
                                    bounding_zone: slot_bounds,
                                    stroke_color,
                                    is_visible: scene_is_not_cinematic,
                                    settings,
                                    get_children: () => {
                                        const inventory_object = new InventoryObjectCpt({
                                            slot_index: slot_i,
                                            is_visible: scene_is_not_cinematic,
                                            settings,
                                            stroke_color,
                                            get_game_object: index => {
                                                const obj = Array.from(get_inventory())[index];
                                                if (obj) {
                                                    const obj_dim = obj.get_dimensions();
                                                    const obj_ratio = obj_dim.width / obj_dim.height;
                                                    const scaled_dim = { width: 0, height: 0 };
                                                    const pos = { x: 0, y: 0 };
                                                    if (obj_ratio > 1) {
                                                        // object image landscape oriented
                                                        scaled_dim.width = slot_side;
                                                        scaled_dim.height = slot_side * (obj_dim.height / obj_dim.width);
                                                        pos.x = slot_bounds.left;
                                                        pos.y = slot_bounds.top + ((slot_side / 2) - (scaled_dim.height / 2));
                                                    } else if (obj_ratio < 1) {
                                                        // portrait oriented
                                                        scaled_dim.height = slot_side;
                                                        scaled_dim.width = slot_side * obj_ratio;
                                                        pos.y = slot_bounds.top;
                                                        pos.x = slot_bounds.left + ((slot_side / 2) - (scaled_dim.width / 2));
                                                    } else {
                                                        // square
                                                        scaled_dim.width = slot_side;
                                                        scaled_dim.height = slot_side;
                                                        pos.x = slot_bounds.left;
                                                        pos.y = slot_bounds.top;
                                                    }
                                                    return {
                                                        ref: obj,
                                                        position: pos,
                                                        dimensions: scaled_dim,
                                                    };
                                                } else {
                                                    return undefined;
                                                }
                                            },
                                            bounding_zone: slot_bounds,
                                            get_children: () => [
                                                new ClosingIconCpt({
                                                    is_visible: scene_is_not_cinematic,
                                                    color: "#bf5e43", // Some kind of red
                                                    center: {
                                                        x: slot_bounds.left + (slot_side / 2),
                                                        y: slot_bounds.top + (slot_side / 2)
                                                    },
                                                    radius: slot_side / 4,
                                                    line_width: slot_side / 20 > 2 ? slot_side / 20 : 2,
                                                    bounding_zone: { ...slot_bounds, clear_color: "rgba(0,0,0,0.2)" },
                                                }),
                                            ]
                                        });
    
                                        inventory_object.add_event_listener({
                                            event_type: "mousemove",
                                            listener: e => {
                                                const obj = inventory_object.params.get_game_object(inventory_object.params.slot_index);
                                                const bounds = inventory_object.params.bounding_zone;
    
                                                inventory_object.state.draw_inventory_close_icon = inventory_object.params.is_visible()
                                                    && !!obj
                                                    && get_scene().game_objects.includes(obj.ref)
                                                    && (
                                                        e.offsetX >= bounds.left
                                                        && e.offsetX <= bounds.right
                                                        && e.offsetY >= bounds.top
                                                        && e.offsetY <= bounds.bottom
                                                    );
                                            }
                                        });
    
                                        inventory_object.children[0].add_event_listener({
                                            event_type: "click",
                                            listener: e => {
                                                if (!inventory_object.params.is_visible()) return false;
                                                const obj = inventory_object.params.get_game_object(inventory_object.params.slot_index);
                                                const bounds = inventory_object.params.bounding_zone;
                                                const mouse_over_slot = e.offsetX >= bounds.left
                                                    && e.offsetX <= bounds.right
                                                    && e.offsetY >= bounds.top
                                                    && e.offsetY <= bounds.bottom;
                                                if (!!obj && mouse_over_slot && get_scene().game_objects.includes(obj.ref)) {
                                                    this.params.on_drop_inventory_object(obj.ref);
                                                    this.clear_event_listeners();
                                                    this.clear_children({ exclude: "TextBoxCpt" });
                                                }
                                            }
                                        });
    
                                        return [inventory_object];
                                    },
                                };
    
                                const slot = new InventorySlotCpt(slot_params);
                                slots.push(slot);
                                slot_i++;
                            });
                        });
                        return slots;
                    },
                }),
                choices_panel: (() => {
    
                    const panel_params = {
                        bounding_zone: this.canvas_zones.choices_panel,
                        is_visible: scene_is_not_cinematic,
                        invisible_clear_color: get_game_settings("general").background_color,
                        get_children: () => Array.from({ length: this.get_max_choices_row_per_scene() * 2 }).map((_, i) => {
                            const base_settings = get_game_settings("choices_panel")
                            const choices_settings = Object.assign({ ...base_settings }, {
                                font_size: base_settings.font_size * scale_ratio,
                                active_choice_border_width: base_settings.active_choice_border_width * scale_ratio,
                                active_choice_rounded_corners_radius: base_settings.active_choice_rounded_corners_radius * scale_ratio,
                            });
    
                            const ctx = window.mentalo_drawing_context;
                            ctx.save();
                            ctx.font = get_canvas_font(choices_settings);
                            const char_w = get_canvas_char_size(ctx).width;
    
                            choices_settings.container_padding *= char_w;
                            choices_settings.choice_padding *= char_w;
    
                            const writable_parent_zone = {
                                left: this.canvas_zones.choices_panel.left + choices_settings.container_padding,
                                right: this.canvas_zones.choices_panel.right - choices_settings.container_padding,
                                top: this.canvas_zones.choices_panel.top + choices_settings.container_padding,
                                bottom: this.canvas_zones.choices_panel.bottom - choices_settings.container_padding,
                                width: 0, height: 0,
                            };
    
                            writable_parent_zone.width = writable_parent_zone.right - writable_parent_zone.left;
                            writable_parent_zone.height = writable_parent_zone.bottom - writable_parent_zone.top;
    
                            const choice_width = writable_parent_zone.width / 2;
                            const max_lines_per_row = this.get_choices_max_lines_per_row();
                            const text_line_h = get_canvas_char_size(ctx).text_line_height;
    
                            const choices_height_per_row = [
                                (max_lines_per_row[0] * text_line_h) + (2 * choices_settings.choice_padding),
                                (max_lines_per_row[1] > 0
                                    ? (max_lines_per_row[1] * text_line_h) + (2 * choices_settings.choice_padding)
                                    : 0),
                            ];
    
                            const choice_bounds = {
                                left: writable_parent_zone.left + ((i % 2) * choice_width),
                                top: writable_parent_zone.top + ((i > 1 ? 1 : 0) * choices_height_per_row[0]),
                                right: writable_parent_zone.left + ((i % 2) * choice_width) + choice_width,
                                bottom: writable_parent_zone.top + ((i > 1 ? 1 : 0) * choices_height_per_row[1]) + choices_height_per_row[i > 1 ? 1 : 0],
                                width: choice_width,
                                height: choices_height_per_row[i > 1 ? 1 : 0],
                            };
    
                            const choice_cpt = new ChoiceCpt({
                                is_visible: scene_is_not_cinematic,
                                bounding_zone: choice_bounds,
                                text_line_h,
                                get_formatted_choice: () => {
                                    const scene_choices = this.get_scenes_formatted_choices()[
                                        get_game_scenes().indexOf(get_scene())
                                    ];
                                    return scene_choices.length - 1 >= i ? scene_choices[i] : undefined;
                                },
                                settings: choices_settings,
                            });
    
                            choice_cpt.add_event_listener({
                                event_type: "mousemove",
                                listener: e => choice_cpt.state.active =
                                    choice_cpt.params.is_visible()
                                    && !!choice_cpt.params.get_formatted_choice()
                                    && e.offsetX >= choice_bounds.left
                                    && e.offsetX <= choice_bounds.right
                                    && e.offsetY >= choice_bounds.top
                                    && e.offsetY <= choice_bounds.bottom
                            });
    
                            choice_cpt.add_event_listener({
                                event_type: "click",
                                listener: e => {
                                    const choice = choice_cpt.params.get_formatted_choice();
                                    if (choice_cpt.params.is_visible()
                                        && !!choice
                                        && e.offsetX >= choice_bounds.left
                                        && e.offsetX <= choice_bounds.right
                                        && e.offsetY >= choice_bounds.top
                                        && e.offsetY <= choice_bounds.bottom) {
                                        const success = this.params.on_choice_click(choice);
                                        if (success) {
                                            this.clear_event_listeners();
                                            this.clear_children();
                                        }
                                    }
                                }
                            });
    
                            ctx.restore();
    
                            return choice_cpt;
                        })
                    };
    
                    return new ChoicesPanelCpt(panel_params);
                })(),
            };
        }
    
        /**
         * Draw a loading state on the black screen while some game resources are loading.
         * This shouldn't show up a lot except if some resources are really big...
         */
        draw_loading() {
            const ctx = window.mentalo_drawing_context;
            ctx.save();
            ctx.font = '25px monospace';
            ctx.fillStyle = "black";
            ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);
            ctx.fillStyle = "white";
            ctx.fillText("Loading", 50, window.innerHeight / 2);
            const dots = Array.from({ length: ++this.loading_frame }).map(() => '.').join('');
            ctx.font = '8px monospace';
            ctx.fillText(dots, 50, window.innerHeight / 2 + 20);
            ctx.restore();
        }
    
        /**
         * Executes the draw method of each registered component.
         */
        draw_game() {
            Object.values(this.components).forEach(cpt => cpt.draw());
        }
    }
    
    module.exports = MtlRender;
    },{"../lib/color-tools":5,"../lib/font-tools":6,"../lib/frame-rate-controller":7,"../model/scene-types":16,"../ui-components/choice-cpt":21,"../ui-components/choices-panel-cpt":22,"../ui-components/closing-icon-cpt":23,"../ui-components/game-object-cpt":24,"../ui-components/inventory-cpt":25,"../ui-components/inventory-object-cpt":26,"../ui-components/inventory-slot-cpt":27,"../ui-components/scene-animation-cpt":28,"../ui-components/text-box-cpt":29,"../ui-components/user-error-popup":31}],20:[function(require,module,exports){
    const supported_locales = ["en", "fr", "es"];
    
    /**
     * Translations for the default error messages.
     */
    
    const translations = {
        "First scene has an empty image, game cannot be loaded": {
            fr: "La première scène a une image vide, le jeu ne peut pas être chargé",
            es: "La primera escena tiene una imagen vacía, el juego no se puede cargar"
        },
        "Destination scene has not been set.": {
            fr: "La scène de destination n'a pas été définie.",
            es: "La escena de destino no ha sido definida.",
        },
        "Next scene has not been set.": {
            fr: "La scène suivante n'a pas été définie",
            es: "La siguiente escena no ha sido definida.",
        },
    };
    
    function get_translated(str, locale) {
        return translations[str] && locale !== "en" && supported_locales.includes(locale)
            ? translations[str][locale]
            : str;
    }
    
    module.exports = {
        get_translated,
        supported_locales,
    };
    },{}],21:[function(require,module,exports){
    "use strict";
    
    const { get_canvas_font } = require("../lib/font-tools");
    const { draw_rect } = require("../lib/shape-tools");
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The rendering component for a scene choice.
     */
    class ChoiceCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * Draw the choice on the registered 2D drawing context
         */
        draw() {
            super.draw();
            const {
                get_formatted_choice,
                bounding_zone,
                settings,
                text_line_h,
            } = this.params;
    
            const writable_zone = {
                left: bounding_zone.left + settings.choice_padding,
                right: bounding_zone.right - settings.choice_padding,
                top: bounding_zone.top + settings.choice_padding,
                bottom: bounding_zone.bottom - settings.choice_padding,
                width: bounding_zone.width - (2 * settings.choice_padding),
                height: bounding_zone.height - (2 * settings.choice_padding),
            };
    
            const choice = get_formatted_choice();
    
            const ctx = window.mentalo_drawing_context;
            ctx.textAlign = settings.text_align;
            ctx.font = get_canvas_font(settings);
            ctx.fillStyle = settings.font_color;
            ctx.textBaseline = "top";
    
            if (choice) {
                if (this.state.active) {
                    draw_rect(ctx, bounding_zone.left, bounding_zone.top, bounding_zone.width, bounding_zone.height, {
                        fill_color: settings.active_choice_background_color,
                        rounded_corners_radius: settings.active_choice_rounded_corners_radius,
                        border: {
                            width: settings.active_choice_border_width,
                            color: settings.font_color,
                        }
                    });
                }
    
                const get_text_x_position = function () {
                    switch (ctx.textAlign) {
                        case "left":
                            return writable_zone.left;
                        case "right":
                            return writable_zone.right;
                        case "center":
                            return writable_zone.left + (writable_zone.width / 2);
                    }
                };
    
                choice.text_lines.forEach((line, i) => {
                    const text_pos = {
                        x: get_text_x_position(),
                        y: writable_zone.top + (i * text_line_h),
                    };
                    ctx.fillText(line, text_pos.x, text_pos.y);
                });
            }
        }
    }
    
    module.exports = ChoiceCpt;
    },{"../lib/font-tools":6,"../lib/shape-tools":8,"./ui-component":30}],22:[function(require,module,exports){
    "use strict";
    
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The rendering component for the game choices panel
     */
    class ChoicesPanelCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * Draw the choices panel and the choices as children components on the registered 2D drawing context
         */
        draw() {
            super.draw();
            if (this.params.is_visible()) {
                this.clear_bounding_zone();
                this.draw_children();
            } else {
                this.clear_bounding_zone({ clear_color: this.params.invisible_clear_color })
            }
        }
    }
    
    module.exports = ChoicesPanelCpt;
    },{"./ui-component":30}],23:[function(require,module,exports){
    "use strict";
    
    const MtlUiComponent = require("./ui-component");
    
    /**
     * A rendering component that displays a closing icon (a cross inside a circle)
     */
    class ClosingIconCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * Uses standard vectorial drawing methods to draw a circle and a cross at 
         * given coordinates on the registered 2D drawing context.
         */
        draw() {
            super.draw();
            this.clear_bounding_zone();
            const { color, center, radius = 5, line_width = 2, background_color = "rgba(0,0,0,0)" } = this.params;
            const ctx = window.mentalo_drawing_context;
            const padding = radius / 1.5;
    
            const cross_coords = {
                left: center.x - radius + padding,
                right: center.x + radius - padding,
                top: center.y - radius + padding,
                bottom: center.y + radius - padding,
            };
    
            ctx.fillStyle = background_color;
            ctx.beginPath();
            ctx.arc(center.x, center.y, radius, 0, 2 * Math.PI);
            ctx.fill();
    
            ctx.strokeStyle = color;
            ctx.lineWidth = line_width;
            ctx.beginPath();
            ctx.arc(center.x, center.y, radius, 0, 2 * Math.PI);
            ctx.stroke();
    
            ctx.beginPath();
            ctx.moveTo(cross_coords.left, cross_coords.top);
            ctx.lineTo(cross_coords.right, cross_coords.bottom);
            ctx.moveTo(cross_coords.left, cross_coords.bottom)
            ctx.lineTo(cross_coords.right, cross_coords.top);
            ctx.stroke();
        }
    }
    
    module.exports = ClosingIconCpt;
    },{"./ui-component":30}],24:[function(require,module,exports){
    "use strict";
    
    const MtlUiComponent = require("./ui-component");
    
    /**
     * A component that draws a GameObject
     */
    class GameObjectCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * Draws the image of the object on the canvas.
         */
        draw() {
            super.draw();
            const { position, dimensions, image, is_in_inventory } = this.params;
            if (is_in_inventory()) {
                return;
            }
    
            const ctx = window.mentalo_drawing_context;
    
            ctx.drawImage(
                image,
                0, 0,
                image.width, image.height,
                position.x, position.y,
                dimensions.w, dimensions.h,
            );
    
            if (this.state.draw_border) {
                ctx.lineWidth = 1;
                ctx.strokeStyle = "rgba(180, 180, 180, 0.5)";
                ctx.strokeRect(position.x - 5, position.y - 5, dimensions.w + 10, dimensions.h + 10);
            }
        }
    }
    
    module.exports = GameObjectCpt;
    },{"./ui-component":30}],25:[function(require,module,exports){
    "use strict";
    
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The component that draws the inventory panel
     */
    class InventoryCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * Draws the panel on the canvas, T
         * the grid slots and the game object images are draws as children components (InventorySlotCpt and InventoryObjectCpt)
         */
        draw() {
            super.draw();
            if (this.params.is_visible()) {
                this.clear_bounding_zone();
                this.draw_children();
            } else {
                this.clear_bounding_zone({ clear_color: this.params.invisible_clear_color })
            }
        }
    }
    
    module.exports = InventoryCpt;
    },{"./ui-component":30}],26:[function(require,module,exports){
    "use strict";
    
    const { draw_rect } = require("../lib/shape-tools");
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The component to display a game object image inside a grid slot of the inventory panel.
     */
    class InventoryObjectCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * If a game object exists for the slot bound to this instance, its image will be drawn cropped inside a bounding rectangle.
         * When a game object is hovered in the inventory, the draw_inventory_closing_icon is updated and a red cross will be drawn 
         * on top of the object image. If the cross get's clicked the object is removed from inventory.
         */
        draw() {
            super.draw();
            const { get_game_object, slot_index, settings, bounding_zone, stroke_color } = this.params;
            const game_object = get_game_object(slot_index);
            if (game_object) {
                const ctx = window.mentalo_drawing_context;
    
                const image = game_object.ref.image;
                draw_rect(ctx, bounding_zone.left, bounding_zone.top, bounding_zone.width, bounding_zone.height, {
                    fill_image: {
                        src: image,
                        dw: game_object.dimensions.width,
                        dh: game_object.dimensions.height,
                        dx: game_object.position.x,
                        dy: game_object.position.y,
                    },
                    rounded_corners_radius: settings.slot_rounded_corner_radius,
                    border: {
                        width: settings.slot_border_width,
                        color: stroke_color,
                    },
                });
    
                if (this.state.draw_inventory_close_icon) {
                    this.draw_children(); // drop object icon
                }
            }
        }
    }
    
    module.exports = InventoryObjectCpt;
    },{"../lib/shape-tools":8,"./ui-component":30}],27:[function(require,module,exports){
    "use strict";
    
    const { draw_rect } = require("../lib/shape-tools");
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The component to draw a grid slot in the inventory panel.
     */
    class InventorySlotCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
        }
    
        /**
         * Draws a rectangle as the grid slot.
         * Calls draw_children to draw the image of the game object stored in that slot if there is one.
         */
        draw() {
            super.draw();
            const { bounding_zone, stroke_color, settings } = this.params;
            const { left, top, width, height } = bounding_zone;
            const ctx = window.mentalo_drawing_context;
    
            draw_rect(ctx, left, top, width, height, {
                fill_color: "rgba(0,0,0,0)",
                rounded_corners_radius: settings.slot_rounded_corner_radius,
                border: {
                    width: settings.slot_border_width,
                    color: stroke_color,
                }
            });
    
            this.draw_children();
        }
    }
    
    module.exports = InventorySlotCpt;
    },{"../lib/shape-tools":8,"./ui-component":30}],28:[function(require,module,exports){
    "use strict";
    
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The component that draws the scene animation.
     */
    class SceneAnimationCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
            this.framecount = 0;
            // This state controls the visibility of the text box child component.
            this.state.text_box_visible = true;
        }
    
        /**
         * Sets the text_box_visibility state.
         * @param {Boolean} value 
         */
        set_text_box_visibility(value) {
            this.state.text_box_visible = value;
        }
    
        /**
         * Increments the animation frame count
         */
        update_framecount() {
            this.framecount = this.framecount + 1 <= Number.MAX_SAFE_INTEGER ? this.framecount + 1 : 0;
        }
    
        /**
         * Draw the scene animation on the canvas  at the frame given by the animation state
         * and draw each child component like text box, game objects and error popup.
         */
        draw() {
            super.draw();
            const { bounding_zone, get_animation, next_frame_ready } = this.params;
            if (next_frame_ready()) {
                this.clear_bounding_zone();
                const ctx = window.mentalo_drawing_context;
    
                const animation = get_animation();
                animation.update_frame(this.framecount);
                const dim = animation.dimensions;
                const w = dim.width;
                const h = dim.height;
                const offsetX = animation.frame * w;
    
                const cw = bounding_zone.width;
                const ch = bounding_zone.height;
    
                // center the image
                if (!animation.canvas_precalc) {
                    animation.canvas_precalc = {
                        dx: bounding_zone.left,
                        dy: bounding_zone.top,
                        dw: cw,
                        dh: ch,
                    };
    
                    if (w / h > cw / ch) {
                        // image is more panoramic than canvas
                        animation.canvas_precalc.dw = cw;
                        animation.canvas_precalc.dh = cw * (h / w);
                        // center vertically
                        animation.canvas_precalc.dy = bounding_zone.top + ((ch - animation.canvas_precalc.dh) / 2);
                    } else if (cw / ch > w / h) {
                        // canvas is more panoramic
                        animation.canvas_precalc.dh = ch;
                        animation.canvas_precalc.dw = ch * (w / h);
                        // center horizontally
                        animation.canvas_precalc.dx = bounding_zone.left + ((cw - animation.canvas_precalc.dw) / 2);
                    } else {
                        // same ratio
                        animation.canvas_precalc.dh = ch;
                        animation.canvas_precalc.dw = cw;
                    }
                }
    
                const { dx, dy, dw, dh } = animation.canvas_precalc;
                ctx.drawImage(
                    animation.image,
                    offsetX, 0,
                    w, h,
                    dx, dy,
                    dw, dh
                );
    
                this.draw_children();
                this.update_framecount();
            }
        }
    }
    
    module.exports = SceneAnimationCpt;
    },{"./ui-component":30}],29:[function(require,module,exports){
    "use strict";
    
    const { get_canvas_font, get_canvas_char_size } = require("../lib/font-tools");
    const { draw_rect } = require("../lib/shape-tools");
    const MtlUiComponent = require("./ui-component");
    
    /**
     * The component that handles the displaying of a scene text boxe.
     */
    class TextBoxCpt extends MtlUiComponent {
        constructor(params) {
            super(params);
            this.state.text = {};
        }
    
        /**
         * Initialize precalculations for the text box, text dimensions, split lines, etc.
         */
        init() {
            const { text, settings, bounding_zone } = this.params;
            const ctx = window.mentalo_drawing_context;
            ctx.font = get_canvas_font(settings);
    
            const char_size = get_canvas_char_size(ctx);
            const line_height = char_size.height * 1.2;
            const error_offset = 3 * char_size.width;
            const chars_per_line = (bounding_zone.width - error_offset) / char_size.width;
            const output_lines = [];
            const lines = text.split("\n");
    
            lines.forEach(line => {
                const res = [""];
                let target_i = 0;
                for (const word of line.split(" ")) {
                    if ((res[target_i] + word + " ").length > chars_per_line) {
                        res.push("");
                        target_i++;
                    }
                    res[target_i] += word + " ";
                }
    
                res.forEach(l => output_lines.push(l));
            });
    
            const text_height = output_lines.length * line_height;
    
            const updated_bounds = Object.assign({ ...bounding_zone }, {
                height: bounding_zone.height + text_height,
                top: bounding_zone.top - text_height,
            });
    
            this.state.text = {
                lines: output_lines,
                line_height,
                bounding_zone: updated_bounds,
            };
    
            // Update closing icon position
            const closing_icon = this.children[0];
            let closing_icon_bounds = closing_icon.params.bounding_zone;
            closing_icon_bounds = Object.assign(closing_icon_bounds, {
                top: closing_icon_bounds.top - text_height,
                bottom: closing_icon_bounds.bottom - text_height,
            });
            closing_icon.params.center.y -= text_height;
    
            this.state.initialized = true;
        }
    
        /**
         * Draw the text box and the text inside of it.
         * The text is not drawn at once, the characters are drawn one by one at each frame 
         * and a local text.stream state is updated until text is complete.
         */
        draw() {
            if (!this.params.get_visibility_state()) {
                return;
            }
    
            super.draw();
    
            const { settings } = this.params;
    
            if (!this.state.initialized) {
                this.init();
            }
    
            const ctx = window.mentalo_drawing_context;
            const { lines, line_height, bounding_zone } = this.state.text;
    
            ctx.font = get_canvas_font(settings);
            ctx.textAlign = settings.text_align;
            ctx.textBaseline = "top";
    
            const box_pos = { x: bounding_zone.left, y: bounding_zone.top };
            const box_width = bounding_zone.width;
            const box_height = bounding_zone.height;
    
            draw_rect(ctx, box_pos.x, box_pos.y, box_width, box_height, {
                fill_color: settings.background_color,
                rounded_corners_radius: settings.rounded_corners_radius,
                border: {
                    width: settings.border_width,
                    color: settings.font_color,
                },
            });
    
            ctx.fillStyle = settings.font_color;
    
            const get_text_position = () => {
                const { padding } = settings;
                switch (ctx.textAlign) {
                    case "left":
                        return {
                            x: box_pos.x + padding,
                            y: box_pos.y + padding,
                        }
                    case "right":
                        return {
                            x: box_pos.x + box_width - padding,
                            y: box_pos.y + padding,
                        }
                    case "center":
                        return {
                            x: box_pos.x + box_width / 2,
                            y: box_pos.y + padding,
                        }
                    default:
                        return {
                            x: box_pos.x + padding,
                            y: box_pos.y + padding,
                        }
                }
            };
    
            const text_pos = get_text_position();
    
            this.state.text.stream = this.state.text.stream || {
                line_chars: 0,
                line_index: 0,
                complete: false,
            };
    
            const streamed_lines = lines.map((line, i) => {
                const stream_state = this.state.text.stream;
                if (i < stream_state.line_index || stream_state.complete) {
                    return line;
                } else if (i > stream_state.line_index) {
                    return "";
                } else {
                    const slice = line.slice(0, ++stream_state.line_chars);
                    if (slice.length === line.length) {
                        stream_state.complete = lines.length - 1 === stream_state.line_index;
                        stream_state.line_index = stream_state.complete
                            ? stream_state.line_index
                            : stream_state.line_index + 1;
                        stream_state.line_chars = 0;
                    }
                    return slice;
                }
            });
    
            streamed_lines.forEach(line => {
                ctx.fillText(line, text_pos.x, text_pos.y);
                text_pos.y += line_height;
            });
    
            if (this.state.text.stream.complete) {
                this.draw_children(); // draw closing_icon
            }
        }
    }
    
    module.exports = TextBoxCpt;
    },{"../lib/font-tools":6,"../lib/shape-tools":8,"./ui-component":30}],30:[function(require,module,exports){
    "use strict";
    
    const { draw_rect } = require("../lib/shape-tools");
    
    /**
     * A generic class that must be extended by all components registered in MtlRender.components.
     * It provides helping methods to handle event listeners and children components.
     */
    class MtlUiComponent {
        constructor(params) {
            this.set_str_identifier();
            this.params = params;
            this.params.event_listeners = this.params.event_listeners || [];
            this.params.get_children = this.params.get_children || (() => []);
            this.params.is_visible = this.params.is_visible || (() => true);
            this.children = [];
            this.children_set = false;
            this.set_children();
            this.state = {
                event_listeners_initialized: false,
            };
    
            this.init_event_listeners();
        }
    
        /**
         * Provide a identifier for the object. Default is contructor name.
         * @param {String} str 
         */
        set_str_identifier(str = this.constructor.name) {
            this.str_identifier = str;
        }
    
        /**
         * Draws a black rectangle on all the surface of the bounding zone defined for this component
         * @param {Object} options can provide a clear_color<String> rgba value
         */
        clear_bounding_zone(options = {}) {
            const { bounding_zone } = this.params;
            draw_rect(window.mentalo_drawing_context, bounding_zone.left, bounding_zone.top, bounding_zone.width, bounding_zone.height, {
                fill_color: options.clear_color || bounding_zone.clear_color || "rgba(0,0,0,0)"
            });
        }
    
        /**
         * Removes the event listeners attached to this component and to children components.
         * @param {Object} options can pass a recursive flag wether children must be cleared or not. Default is true.
         */
        clear_event_listeners(options = { recursive: true }) {
            this.params.event_listeners.forEach(ref => {
                window.removeEventListener(ref.event_type, ref.listener);
            });
    
            if (options.recursive) {
                this.children.forEach(child => {
                    child.clear_event_listeners();
                });
            }
    
            this.state.event_listeners_initialized = false;
        }
    
        /**
         * Removes children components from this component.
         * Options can define some component to exlude identifying them by their str_identifier.
         * The excluded component will be kept as persistent components.
         * @param {Object} options 
         */
        clear_children(options) {
            const { exclude } = options;
            const persistent_children = [];
    
            this.children.forEach(child => {
                if (exclude.includes(child.str_identifier)) {
                    persistent_children.push(child);
                }
            });
    
            this.children = persistent_children;
            this.children_set = false;
        }
    
        /**
         * Register an array of children components in this component.