game_opening_pivot = nil

open1 = nil
open2 = nil
open3 = nil
open4 = nil
open5 = nil
open6 = nil

text_fill = nil
text_line = nil

open1_text1 = nil
open1_text2 = nil
open2_text1 = nil
open2_text2 = nil
open3_text1 = nil
open4_text = nil
open5_text1 = nil
open5_text2 = nil
open5_text3 = nil
open5_text4 = nil
open5_text5 = nil
open5_text6 = nil
open6_text1 = nil
open6_text2 = nil
press_enter = nil

menu_click = E.load_sound(media_dir .. "menu_click.ogg")
intro_music = E.load_sound(media_dir .. "mozart_night.ogg")

time_accumulator = 0

function destroy_game_opening()
	E.delete_entity(game_opening_pivot)
	E.free_sound(menu_click)
	E.free_sound(intro_music)
end

local function centered_string(text, line, fill)
        local string = E.create_string(text)
        local pivot = E.create_pivot()
        
        --local x0, y0, z0, x1, y1, z1 = E.get_entity_bound(string)
        
        E.set_string_line(string, line)
        E.set_string_fill(string, fill)
        E.parent_entity(string, pivot)
        
        --E.set_entity_position(string, -(x1 - x0)/2, -0.5, 0)
        E.set_entity_position(string, 0, -0.5, viewport.depth_bisect-1)
        
        return pivot, viewport.width, viewport.height--(x1 - x0), (y1 - y0)
end

local function init_game_opening()
	
    --camera = E.create_camera(E.camera_type_perspective)
    game_opening_pivot = E.create_pivot()

    local images = E.create_pivot()
    local text = E.create_pivot()
	
	E.parent_entity(main_menu_pivot, camera)
    E.parent_entity(images, main_menu_pivot )
    E.parent_entity(text, images )

    E.set_background(0.0, 0.0, 0.0)
    
    --E.play_sound(intro_music)
    E.loop_sound(intro_music)
    E.set_sound_amplitude(intro_music, 0.8)
    
    open1 = make_opaque_image_billboard(media_dir .. "open1.jpg")
    E.set_entity_alpha(open1, 0.0) -- hidden
    fit_to_view(open1, 0.7, 0.768, 0, 0)
    E.parent_entity(open1, images)
    
    open2 = make_opaque_image_billboard(media_dir .. "open2.jpg")
    E.set_entity_alpha(open2, 0.0) -- hidden
    fit_to_view(open2, 0.718, 0.7, 0, 0)
    E.parent_entity(open2, images)
    
    open3 = make_opaque_image_billboard(media_dir .. "open3.jpg")
    E.set_entity_alpha(open3, 0.0) -- hidden
    fit_to_view(open3, 0.8, 0.692, 0, 0.0)
    E.parent_entity(open3, images)
    
    open4 = make_opaque_image_billboard(media_dir .. "open4.jpg")
    E.set_entity_alpha(open4, 0.0) -- hidden
    fit_to_view(open4, 0.82, 0.55, 0, 0)
    E.parent_entity(open4, images)
    
    open5 = make_opaque_image_billboard(media_dir .. "open5.jpg")
    E.set_entity_alpha(open5, 0.0) -- hidden
    fit_to_view(open5, 0.75, 0.697, 0, 0.0)
    E.parent_entity(open5, images)
    
    open6 = make_opaque_image_billboard(media_dir .. "open6.jpg")
    E.set_entity_alpha(open6, 0.0) -- hidden
    fit_to_view(open6, 0.775, 0.52, 0, 0)
    E.parent_entity(open6, images)
    
    --E.set_typeface(media_dir .. "VeraBD.ttf", 0.001, 0.0)
    --E.set_typeface(media_dir .. "Nou.ttf", 0.001, 0.03)

    text_fill = E.create_brush()
    E.set_brush_color(text_fill, 1.0, 0.0, 0.0, 0.8)
    E.set_brush_flags(text_fill, E.brush_flag_unlit, true)

    text_line = E.create_brush()
    E.set_brush_color(text_line, 1.0, 0.0, 0.0, 0.8)
    E.set_brush_flags(text_line, E.brush_flag_unlit, true)

    -- open1_text
    --"Your name is Shane Wayne. You're an average schmuck"
    --"with a crappy gig down at the local art museum."
    open1_text1, w, h = centered_string("Your name is Wayne Kitsch. You're an average schmuck", text_line, text_fill)
    E.set_entity_alpha(open1_text1, 0.0) -- hidden
    E.set_entity_flags(open1_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(open1_text1, 0.065, 0.065, 1)
    E.set_entity_position(open1_text1, -w + (w+0.06)/4 , -h + 0.15, 0)
    E.parent_entity(open1_text1, text)
    
    open1_text2, w, h = centered_string("with a crappy gig down at the local art museum.", text_line, text_fill)
    E.set_entity_alpha(open1_text2, 0.0) -- hidden
    E.set_entity_flags(open1_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(open1_text2, 0.065, 0.065, 1)
    E.set_entity_position(open1_text2, -w + (w+0.06)/4, -h + 0.10, 0)
    E.parent_entity(open1_text2, text)
    
    -- open2_text
    --"Your payment for standing 8 hours a day is blisters,"
    --"calluses, boredom and minimum wage."
    open2_text1, w, h = centered_string("Your payment for standing 8 hours a day is blisters,", text_line, text_fill)
    E.set_entity_alpha(open2_text1, 0.0) -- hidden
    E.set_entity_flags(open2_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(open2_text1, 0.065, 0.065, 1)
    E.set_entity_position(open2_text1, -w + (w+0.045)/4 , -h + 0.20, 0)
    E.parent_entity(open2_text1, text)
    
    open2_text2, w, h = centered_string("calluses, boredom and minimum wage.", text_line, text_fill)
    E.set_entity_alpha(open2_text2, 0.0) -- hidden
    E.set_entity_flags(open2_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(open2_text2, 0.065, 0.065, 1)
    E.set_entity_position(open2_text2, -w + (w+0.045)/4, -h + 0.15, 0)
    E.parent_entity(open2_text2, text)

    -- open3_text
    --"To make matters worse, you despise art. You don't 'get'"
    --"it. And each day sucks worse than the previous."
    open3_text1, w, h = centered_string("To make matters worse, you despise art. You don't 'get'", text_line, text_fill)
    E.set_entity_alpha(open3_text1, 0.0) -- hidden
    E.set_entity_flags(open3_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(open3_text1, 0.065, 0.065, 1)
    E.set_entity_position(open3_text1, -w + (w+0.045)/4 , -h + 0.20, 0)
    E.parent_entity(open3_text1, text)
    
    open3_text2, w, h = centered_string("it. And each day sucks worse than the previous.", text_line, text_fill)
    E.set_entity_alpha(open3_text2, 0.0) -- hidden
    E.set_entity_flags(open3_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(open3_text2, 0.065, 0.065, 1)
    E.set_entity_position(open3_text2, -w + (w+0.045)/4, -h + 0.15, 0)
    E.parent_entity(open3_text2, text)
    
    -- open4_text
    --"Until one day..."
    open4_text, w, h = centered_string("Until one day...", text_line, text_fill)
    E.set_entity_alpha(open4_text, 0.0) -- hidden
    E.set_entity_flags(open4_text, E.entity_flag_hidden, true)
    E.set_entity_scale(open4_text, 0.065, 0.065, 1)
    E.set_entity_position(open4_text, -0.18 , -h + 0.3, 0)
    E.parent_entity(open4_text, text)
    
    -- open5_text
    --"\"Ok tough guy, today's your lucky day, I'll make"
    --"you rich. Listen, my client wants a painting for"
    --"his dining room. Steal the Kandinsky and I'll hook"
    --"you up so you can retire in the Caribbean."
    --"Otherwise, your ass is grass. Case the joint at"
    --"work today and steal that shiz tonight.\" "
    open5_text1, w, h = centered_string("\"Ok tough guy, today's your lucky day, I'll make", text_line, text_fill)
    E.set_entity_alpha(open5_text1, 0.0) -- hidden
    E.set_entity_flags(open5_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(open5_text1, 0.065, 0.065, 1)
    E.set_entity_position(open5_text1, -w + (w+0.5)/4 , -h + 0.65, 0)
    E.parent_entity(open5_text1, text)
    
    open5_text2, w, h = centered_string("you rich. Listen, my client wants a painting for", text_line, text_fill)
    E.set_entity_alpha(open5_text2, 0.0) -- hidden
    E.set_entity_flags(open5_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(open5_text2, 0.065, 0.065, 1)
    E.set_entity_position(open5_text2, -w + (w+0.5)/4, -h + 0.60, 0)
    E.parent_entity(open5_text2, text)
    
    open5_text3, w, h = centered_string("his dining room. Steal the Kandinsky and I'll", text_line, text_fill)
    E.set_entity_alpha(open5_text3, 0.0) -- hidden
    E.set_entity_flags(open5_text3, E.entity_flag_hidden, true)
    E.set_entity_scale(open5_text3, 0.065, 0.065, 1)
    E.set_entity_position(open5_text3, -w + (w+0.5)/4 , -h + 0.55, 0)
    E.parent_entity(open5_text3, text)

    open5_text4, w, h = centered_string("hook you up so you can retire in the Caribbean.", text_line, text_fill)
    E.set_entity_alpha(open5_text4, 0.0) -- hidden
    E.set_entity_flags(open5_text4, E.entity_flag_hidden, true)
    E.set_entity_scale(open5_text4, 0.065, 0.065, 1)
    E.set_entity_position(open5_text4, -w + (w+0.5)/4, -h + 0.50, 0)
    E.parent_entity(open5_text4, text)
    
    open5_text5, w, h = centered_string("Otherwise, your ass is grass. Case the joint at", text_line, text_fill)
    E.set_entity_alpha(open5_text5, 0.0) -- hidden
    E.set_entity_flags(open5_text5, E.entity_flag_hidden, true)
    E.set_entity_scale(open5_text5, 0.065, 0.065, 1)
    E.set_entity_position(open5_text5, -w + (w+0.5)/4, -h + 0.45, 0)
    E.parent_entity(open5_text5, text)
    
    open5_text6, w, h = centered_string("work today and steal that shiz tonight.\"", text_line, text_fill)
    E.set_entity_alpha(open5_text6, 0.0) -- hidden
    E.set_entity_flags(open5_text6, E.entity_flag_hidden, true)
    E.set_entity_scale(open5_text6, 0.065, 0.065, 1)
    E.set_entity_position(open5_text6, -w + (w+0.5)/4, -h + 0.40, 0)
    E.parent_entity(open5_text6, text)
    
    -- open6_text
    --"\"But I don't know jack about art.\""
    --"\"Not my problem. Steal the Kandinsky\""
    open6_text1, w, h = centered_string("\"But I don't know jack about art.\"", text_line, text_fill)
    E.set_entity_alpha(open6_text1, 0.0) -- hidden
    E.set_entity_flags(open6_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(open6_text1, 0.065, 0.065, 1)
    E.set_entity_position(open6_text1, -w + (w+2)/4 , -h + 0.6, 0)
    E.parent_entity(open6_text1, text)
    
    open6_text2, w, h = centered_string("\"Not my problem. Steal the Kandinsky\"", text_line, text_fill)
    E.set_entity_alpha(open6_text2, 0.0) -- hidden
    E.set_entity_flags(open6_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(open6_text2, 0.065, 0.065, 1)
    E.set_entity_position(open6_text2, -w + (w+0.8)/4, -h + 0.7, 0)
    E.parent_entity(open6_text2, text)
    
    press_enter, w, h = centered_string("Press Enter to Continue", text_line, text_fill)
    --E.set_entity_alpha(press_enter, 0.0) -- hidden
    --E.set_entity_flags(press_enter, E.entity_flag_hidden, true)
    E.set_entity_scale(press_enter, 0.045, 0.045, 1)
    E.set_entity_position(press_enter, (w + 0.1)/4, -h + 0.05, 0)
    E.parent_entity(press_enter, text)
    
    game_state = game_opening
end

init_game_opening()