Fields of Mistria — Vanilla GML Reference FoM v1.0.8

BarkEmitter.gml

#macro ACTIVE_BARKS global.__active_barks
global.__active_barks = List();

//
function BarkEmitter(owner, x_offset=0, y_offset=0) constructor {
    self.owner = owner;
    self.x_offset = x_offset;
    self.y_offset = y_offset;
    self.active_bark = undefined;
    self.depth_base = 0;

    //
    self.bark_type = BarkType.Thought;
    self.bubble_index = 0;
    self.bubble_index_counter = 0;
    self.icon_index = 0;
    self.icon_index_counter = 0;

    self.state = BarkState.In;
    self.bubble_sprite = spr_ui_bark_bubble_think_opening;
    self.timer = 0;

    self.modifier = undefined;

    self.bubble = owner.create_renderable();
    self.bubble.speed = 0.0;
    self.bubble.visible = false;
    self.bubble.depth_sub_offset = -1;

    self.icon = owner.create_renderable();
    self.icon.speed = 0.0;
    self.icon.visible = false;
    self.icon.depth_sub_offset = -2;

    //
    function emit(bark_id, bark_type=BarkType.Thought, play_sound) {
        if ACTIVE_BARKS.contains(self) == false {
            ACTIVE_BARKS.push(self);
        }

        if self.modifier != undefined {
            bark_id = self.modifier(bark_id);
        }

        play_sound = play_sound == undefined ? MIST.running && bark_type != BarkType.Thought : play_sound;
        self.state = BarkState.In;
        self.active_bark = BARK_PROTOTYPES[bark_id];
        self.bark_type = bark_type;
        self.bubble_sprite = self.bark_type == BarkType.Thought
            ? spr_ui_bark_bubble_think_opening
            : spr_ui_bark_bubble_speak_opening;

        self.bubble_index = 0;
        self.bubble_index_counter = 0;

        var mist_override = MIST.blackboard.try_take("next_bark_sfx");

        if ((play_sound && self.active_bark.sound != undefined) || mist_override != undefined)
            && MIST.blackboard.get("mute_barks") != true
        {
            var sfx = mist_override ?? self.active_bark.sound;
            TANGO.play(sfx, self.owner.x, self.owner.y);
        }
    }

    //
    function is_barking() {
        return self.active_bark != undefined;
    }

    //
    function set_owner(new_owner) {
        self.owner = new_owner;
        self.bubble.set_owner(new_owner);
        self.icon.set_owner(new_owner);
    }

    //
    function hide() {
        self.bubble.visible = false;
        self.icon.visible = false;
    }

    function on_step(z=0) {
        if !self.is_barking() {
            return;
        }

        //
        var bubble_frame_times = BARK_OPENING_INFO[self.bark_type];
        self.bubble_index_counter += 1;
        if self.bubble_index_counter >= bubble_frame_times[self.bubble_index] {
            self.bubble_index += 1;
            self.bubble_index_counter = 0;

            if self.bubble_index >= array_length(bubble_frame_times) {
                self.bubble_index = 0;

                switch self.state {
                    case BarkState.In:
                        self.bubble_sprite = self.bark_type == BarkType.Thought
                            ? spr_ui_bark_bubble_think_open
                            : spr_ui_bark_bubble_speak_open;
                        self.state = BarkState.Sit;
                        self.icon_index = 0;
                        self.icon_index_counter = 0;
                        self.timer = BARK_TIMER;
                        break;
                    case BarkState.Sit:
                        //
                        break;
                    case BarkState.Out:
                        //
                        self.active_bark = undefined;
                        self.hide();
                        return;
                }
            }
        }

        if self.state == BarkState.Sit {
            self.icon_index_counter += 1;
            if self.icon_index_counter >= self.active_bark.frames[self.icon_index] {
                self.icon_index = min(self.icon_index + 1, array_length(self.active_bark.frames) - 1);
                self.icon_index_counter = 0;
            }

            self.timer -= 1;
            if self.timer <= 0 {
                self.bubble_sprite = self.bark_type == BarkType.Thought
                    ? spr_ui_bark_bubble_think_closing
                    : spr_ui_bark_bubble_speak_closing;
                self.state = BarkState.Out;
            }
        }

        var xx = self.owner.x + self.x_offset;
        var yy = self.owner.y + self.y_offset + z;
        var bubble_was_visible = self.bubble.visible;
        var icon_was_visible = self.icon.visible;

        self.bubble.visible = true;
        self.bubble.animation = self.bubble_sprite;
        self.bubble.animation_frame = self.bubble_index;
        self.bubble.x = xx;
        self.bubble.y = yy;
        self.bubble.depth = self.depth_base + self.owner.depth + self.y_offset;

        var showing_icon = self.state == BarkState.Sit;
        self.icon.visible = showing_icon;
        if showing_icon {
            self.icon.animation = self.active_bark.icon;
            self.icon.animation_frame = self.icon_index;
            self.icon.alpha = self.active_bark.opacity;
        }

        self.icon.x = xx;
        self.icon.y = yy;
        self.icon.depth = self.depth_base + self.owner.depth + self.y_offset;

        if !bubble_was_visible {
            self.bubble.teleport();
        }

        if !icon_was_visible {
            self.icon.teleport();
        }
    }
}