/*
 ==========================================
 ProSonic script test file
 Programmed by Damian Grove
 
 Compile using the ProSonic Script Compiler
 ==========================================
*/

include     "defs.txt"


function LEVELSTART
    show_watermark = 1
    resetsound
    d0 = zone
    d0 + 2
    d0 % 3
    d0 ++
    if zone == 0
        d0 = 4
    elseif zone == 16
        d0 = 2
        playpcm 53 1 0
    endif
    
    playfm d0 0 0
    
endfunction


function INGAME1
    // do stuff before running objects
    // textout 160 0 TCOLOR_WHITE "RUNNING INGAME1"
    
    if zone == 0
        if ticCounterL < 500
            textout 144 6 TCOLOR_WHITE "S2GG CRYSTAL EGG"
            textout 144 16 TCOLOR_WHITE "ARRANGED BY DALEKSAM"
        endif
    endif
    
    d2 = key[KEY_F2]
    d3 = key[KEY_F3]
    
    if d2 != 0
        if demo_mode == 0
            rest 200
            demo_mode = 1
        endif
    endif
    
    if d3 != 0
        if demo_mode == 0
            rest 500
            demo_mode = 2
        elseif demo_mode == 2
            rest 500
            demo_mode = 3
        endif
    endif
    
    
    
    d0 = key[KEY_F4]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 64
            d3 = 48
            d4 = 1
        else
            d2 = 0
            d3 = 0
            d4 = 0
        endif
        setgfxmode 256 192 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F5]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 0
            d3 = 0
            d4 = 1
        else
            d2 = 0
            d3 = 0
            d4 = 0
        endif
        setgfxmode 320 200 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F6]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 0
            d3 = 16
            d4 = 1
        else
            d2 = 0
            d3 = 16
            d4 = 0
        endif
        setgfxmode 320 224 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F7]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 0
            d3 = 0
            d4 = 1
        else
            d2 = 0
            d3 = 0
            d4 = 0
        endif
        setgfxmode 400 300 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F8]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 0
            d3 = 0
            d4 = 1
        else
            d2 = 0
            d3 = 0
            d4 = 0
        endif
        setgfxmode 512 384 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F9]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 0
            d3 = 0
            d4 = 1
        else
            d2 = 0
            d3 = 0
            d4 = 0
        endif
        setgfxmode 640 480 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F10]
    if d0 != 0
        d1 = key_shifts
        d1 & KB_SHIFT_FLAG
        if d1 != 0
            d2 = 0
            d3 = 0
            d4 = 1
        else
            d2 = 0
            d3 = 0
            d4 = 0
        endif
        setgfxmode 800 600 d2 d3 screen_double_x screen_double_y d4
    endif
    
    d0 = key[KEY_F11]
    if d0 != 0
        Level_Inactive_flag = 1
    endif
    
    d0 = key[KEY_F1]
    if d0 != 0
        prompt = 1
    endif
    
    d0 = key[KEY_PGUP]
    d1 = key[KEY_PGDN]
    if d0 != 0
        frame_skip ++
        frame_skip % 6
        rest 100
    elseif d1 != 0
        if frame_skip == 0
            frame_skip = 5
        else
            frame_skip --
        endif
        rest 100
    endif
    
    d0 = key[KEY_OPENBRACE]
    d1 = key[KEY_CLOSEBRACE]
    if d0 != 0
        screen_double_x ^ 1
        setgfxmode screen_resolution_x screen_resolution_y screen_padding_x screen_padding_y screen_double_x screen_double_y full_screen
    endif
    
    if d1 != 0
        screen_double_y ^ 1
        setgfxmode screen_resolution_x screen_resolution_y screen_padding_x screen_padding_y screen_double_x screen_double_y full_screen
    endif
    
    d0 = key[KEY_V]
    if d0 != 0
        show_version ^ 1
        rest 100
    endif
endfunction


function INGAME2
    // do stuff after running objects
    // textout 160 20 TCOLOR_WHITE "RUNNING INGAME2"
endfunction

function INGAME3
    // do stuff after screen buffers processed
    // textout 160 30 TCOLOR_WHITE "RUNNING INGAME3"

	if titlecard_sequence_end == 1
		if demo_mode == 1
			d0 = Timer_second
			d0 * 60
			d0 + Timer_millisecond
			d0 % 80
			if d0 >= 40
				d0 = screen_resolution_x
				d0 >> 1
				d0 - 44
				d1 = screen_resolution_y
				d1 >> 1
				d1 - 4
				textout d0 d1 TCOLOR_WHITE "PRESS ENTER"
			endif
		endif
		
		if edit_mode == 0
			// SCORE
			counterout COUNTER_BIG 0 p.score 96 8
			// TIME
			counterout COUNTER_BIG 1 timer_minute 56 24
			counterout COUNTER_BIG 2 timer_second 80 24
			// RINGS
			counterout COUNTER_BIG 1 p.ring_count 80 40
			// LIVES
			d0 = screen_resolution_y
			d0 - 16
			counterout COUNTER_SMALL 1 p.life_count 56 d0
		else
			counterout COUNTER_DEBUG 4 p.x_pos 48 16
			counterout COUNTER_DEBUG 4 p.y_pos 80 16
			counterout COUNTER_DEBUG 4 p.x_pos_pre 48 24
			counterout COUNTER_DEBUG 4 p.y_pos_pre 80 24
			counterout COUNTER_DEBUG 4 p.inertia 48 40
			d0 = p.angle
			d0 << 8
			counterout COUNTER_DEBUG 4 d0 80 40
			counterout COUNTER_DEBUG 4 p.x_vel 48 48
			counterout COUNTER_DEBUG 4 p.y_vel 80 48
			d0 = screen_resolution_y
			d0 - 8
			counterout COUNTER_DEBUG 4 zone 0 d0
			counterout COUNTER_DEBUG 4 p.Camera_x_pos 40 0
			counterout COUNTER_DEBUG 4 p.Camera_y_pos 72 0
			
			counterout COUNTER_DEBUG 4 edit_selector 0 8
			
			if edit_mode == 1
				textout 40 8 TCOLOR_WHITE "TILE LAYOUT EDITOR"
			elseif edit_mode == 2
				textout 40 8 TCOLOR_WHITE "BLOCK MAPPINGS EDITOR"
			elseif edit_mode == 3
				textout 40 8 TCOLOR_WHITE "BLOCK SOLIDITY EDITOR"
			else
				textout 40 8 TCOLOR_WHITE "OBJECT LAYOUT EDITOR"
			endif
		endif
	endif
endfunction


object OBJ_WARP
	d1 = o.x_pos
	d1 - p.camera_x_pos
	d1 - 40
	d2 = o.y_pos
	d2 - p.camera_y_pos
	d2 - 10
	textout d1 d2 TCOLOR_WHITE "ZONE  WARP"
	d1 + 24
	d2 + 12
	counterout COUNTER_DEBUG 4 o.subtype d1 d2
	
	d0 = p.x_pos
	d0 - o.x_pos
	d3 = p.y_pos
	d3 - o.y_pos
	if d0 < 32
		if d0 >= -32
			if d3 < 32
				if d3 >= -32
					startzone o.subtype 0 0
				endif
			endif
		endif
	endif
endobject


object OBJ_REX
	// textout 160 10 TCOLOR_WHITE "RUNNING OBJ_REX"
	o.x_vel = 50
	o.y_vel = -32
	move
	
	d0 = o.x_pos
	d0 - p.camera_x_pos
	d0 - 16
	d1 = o.y_pos
	d1 - p.camera_y_pos
	d1 - 8
	
	counterout COUNTER_DEBUG 4 o.x_pos d0 d1
	d1 + 8
	counterout COUNTER_DEBUG 4 o.y_pos d0 d1
	
	
	
	d0 = o.user[0]
	if d0 > 50
		return
	endif
	o.user[0] ++
	
	// d0 = timer_second
	// d0 % 2
	// if d0 == 0
	//	if timer_millisecond == 0
	//		textout 160 60 TCOLOR_WHITE "SPAWN"
	//		spawn OBJ_RING 0 0 0 o.x_pos o.y_pos
	//	endif
	// endif
endobject


object OBJ_BRIDGE
    // textout 160 10 TCOLOR_WHITE "RUNNING OBJ_BRIDGE"
    o.static_node = 1
    
    d6 = o.user[2]
    d7 = ticCounterL
    
    // update movement once every frame
    if d6 < d7
        o.user[2] = ticCounterL
        o.user[0] ++
        o.user[1] ++
    endif
    
    d0 = o.user[0]
    d1 = o.user[1]
    
    d0 % 280
    d1 % 212
    d0 << 1
    d1 << 1
    
    if d0 >= 280
        d7 = 560
        d7 - d0
        d0 = d7
    endif
    
    if d1 >= 212
        d7 = 424
        d7 - d1
        d1 = d7
    endif
    
    
    
    /*
    if p.life_count != 3
        o.anim_frame = 0
    endif

    // o.anim_frame = p.ring_count
    // drawsprite LAYERSH NORMAL 0 d0 d1

    o.anim = p.ring_count
    animate LAYERSH NORMAL 0 d0 d1
    */
    
    /*
    d0 = o.x_pos
    d1 = o.y_pos
    d0 - p.camera_x_pos
    d1 - p.camera_y_pos
    */
    
    o.anim_frame = 4
    
    drawsprite LAYERH MIRROR 24 d0 d1
    
    p.score = o.type
    /*
    d0 = ticCounterL
    d0 & 3
    if d0 == 0
        o.x_pos ++
        o.y_pos --
    elseif d0 == 2
        o.x_pos ++
    endif
    */
    
    
    if p.ring_count == 8
        d0 = p.extra_life_flags
        d0 & 1
        if d0 == 0
            p.extra_life_flags | 1
            p.life_count ++
        endif
    elseif p.ring_count == 16
        d0 = p.extra_life_flags
        d0 & 2
        if d0 == 0
            p.extra_life_flags | 2
            p.life_count ++
        endif
    endif
endobject
