~jadedctrl/gem-xwx-moe
~jadedctrl/gem-xwx-moe/gemujo_ludo/mods.niaj/fasado/subtitles/WaypointSubtitleDisplay.lua
~jadedctrl/gem-xwx-moe/gemujo_ludo/mods.niaj/fasado/subtitles/WaypointSubtitleDisplay.lua
0 | --[[ |
1 | Subtitles — adds subtitles to Minetest. |
2 |
|
3 | Copyright © 2022‒2023, Silver Sandstone <@SilverSandstone@craftodon.social> |
4 |
|
5 | Permission is hereby granted, free of charge, to any person obtaining a |
6 | copy of this software and associated documentation files (the "Software"), |
7 | to deal in the Software without restriction, including without limitation |
8 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9 | and/or sell copies of the Software, and to permit persons to whom the |
10 | Software is furnished to do so, subject to the following conditions: |
11 |
|
12 | The above copyright notice and this permission notice shall be included |
13 | in all copies or substantial portions of the Software. |
14 |
|
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21 | DEALINGS IN THE SOFTWARE. |
22 | ]] |
23 |
|
24 |
|
25 | --- Provides the WaypointSubtitleDisplay class, which displays subtitles floating in the world. |
26 |
|
27 |
|
28 | local S = subtitles.S; |
29 |
|
30 |
|
31 | --- Displays subtitles floating in the world. |
32 | -- @type WaypointSubtitleDisplay |
33 | subtitles.WaypointSubtitleDisplay = subtitles.SubtitleDisplay:extend(); |
34 |
|
35 | subtitles.WaypointSubtitleDisplay.NAME = 'waypoint'; |
36 | subtitles.WaypointSubtitleDisplay.ICON = 'subtitles_mode_waypoint.png'; |
37 | subtitles.WaypointSubtitleDisplay.TITLE = S('Floating text in world'); |
38 | subtitles.WaypointSubtitleDisplay.DESCRIPTION = S('Displays subtitles floating in 3D space.'); |
39 |
|
40 | subtitles.WaypointSubtitleDisplay.ASCEND_SPEED = 0.25; |
41 |
|
42 | function subtitles.WaypointSubtitleDisplay:init() |
43 | self.hud_ids = {}; |
44 | end; |
45 |
|
46 | function subtitles.WaypointSubtitleDisplay:add_sound(sound) |
47 | local player = self:get_player(); |
48 | if not player then |
49 | return; |
50 | end; |
51 |
|
52 | local pos = sound:get_pos() or player:get_pos(); |
53 |
|
54 | local hud = |
55 | { |
56 | hud_elem_type = 'waypoint'; |
57 | name = sound:get_description(); |
58 | precision = 0; |
59 | number = 0xFFFFFF; |
60 | world_pos = pos; |
61 | offset = {x = 0, y = 0}; |
62 | alignment = {x = 0, y = 0}; |
63 | }; |
64 |
|
65 | local hud_id = player:hud_add(hud); |
66 | self.hud_ids[sound] = hud_id; |
67 | end; |
68 |
|
69 | function subtitles.WaypointSubtitleDisplay:remove_sound(sound) |
70 | local hud_id = self.hud_ids[sound]; |
71 | if hud_id then |
72 | self.hud_ids[sound] = nil; |
73 | local player = self:get_player(); |
74 | if player then |
75 | player:hud_remove(hud_id); |
76 | end; |
77 | end; |
78 | end; |
79 |
|
80 | function subtitles.WaypointSubtitleDisplay:step(dtime) |
81 | local player = self:get_player(); |
82 | if not player then |
83 | return; |
84 | end; |
85 |
|
86 | for sound, hud_id in pairs(self.hud_ids) do |
87 | local hud = player:hud_get(hud_id); |
88 | if hud then |
89 | local pos = vector.add(hud.world_pos, vector.new(0, dtime * self.ASCEND_SPEED, 0)); |
90 | player:hud_change(hud_id, 'world_pos', pos); |
91 | end; |
92 | end; |
93 | end; |
94 |
|
95 | subtitles.WaypointSubtitleDisplay:register(); |