Fork me on GitHub
-- title:   Game Screens
-- author:  congusbongus
-- desc:    game screens a.k.a. state, timed screens
-- site:    cxong.github.io
-- license: MIT License
-- version: 0.1
-- script:  moon
-- tags:    screen state time menu

WIDTH=240
HEIGHT=136
SFXNEXT=1

-- Base state class
class State
	new:=>
		@tt=0
        -- Set the next state to switch to
		@nextstate=self

	reset:=>
        -- Reset is called whenever the game enters this state
		@tt=0

	update:=>
		@tt+=1

	finish:=>
        -- Finish is called whenever the game exits this state
        return

	next:=>
        -- Returns the next state to switch to, or self to remain in the same state
        return self

	draw:=>return

-- Skip to the next state on any button press.
-- Contains a grace period to avoid accidentally skipping too early
class SkipState extends State
	new:(grace)=>
		super!
		@grace=grace

	finish:=>
		sfx(SFXNEXT)

	next:=>
		if @tt>@grace and (btnp(0) or btnp(1) or btnp(2) or btnp(3) or btnp(4) or btnp(5))
			@finish!
			@nextstate\reset!
			return @nextstate
		return self

-- Example of using SkipState to implement a splash screen
-- This screen is timed which means it wil auto-switch after a length of time
class TimedState extends SkipState
	new:=>
		super(10)
		@len=300

	draw:=>
		super!
		cls(0)
        print("This is the splash screen!", 10, 10, 13)
        print("It will auto-switch in #{math.ceil((@len-@tt)/60)} seconds", 10, 30, 13)

	next:=>
		if @tt>=@len
			@finish!
			@nextstate\reset!
			return @nextstate
		return super!

-- Example of using SkipState to implement a title screen
class TitleState extends SkipState
	new:=>
		super(10)

	draw:=>
		super!
		cls(1)
		print("This is the title screen!\nPress any key to go back to the\nsplash screen", 10, 10, 11)

splashState = TimedState!
titleState = TitleState!
splashState.nextstate = titleState
titleState.nextstate = splashState
state=splashState
state\reset!

export TIC=->
	state\update!
	state\draw!
	state=state\next!

-- <WAVES>
-- 000:01358acefeca853101368acefeca8531
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
-- </WAVES>

-- <SFX>
-- 000:00000000000000001000300040004000500050007000700090009000a000a000c000d000e000e000f000e000e000e000e000e000e000e000e000e000377000000000
-- 001:0100417061c09100a100c100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100f100450000000300
-- </SFX>

-- <SCREEN>
-- 010:0000000000dddd0dd0000dd00000000000dd000000000000dd000dd00000000000000000000000000dd000000000000000dd00000000000000000000000000000000000000000000dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 011:00000000000dd00dddd000000dddd000000000dddd00000ddddd0dddd000ddd0000000dddd0dddd00dd0000dddd00dddd0dddd0000000dddd00dddd0dddd000ddd000ddd00dddd00dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 012:00000000000dd00dd00d0dd0ddd0000000dd0ddd00000000dd000dd00d0dd0dd00000ddd000dd00d0dd000d00dd0ddd000dd00d00000ddd000ddd000dd00d0dd0dd0dd0dd0dd00d0dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 013:00000000000dd00dd00d0dd000ddd00000dd000ddd000000dd000dd00d0ddd000000000ddd0dd00d0dd000d00dd000ddd0dd00d0000000ddd0ddd000dd0000ddd000ddd000dd00d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 014:00000000000dd00dd00d0dd0dddd000000dd0dddd00000000ddd0dd00d00ddd000000dddd00dddd000ddd00dddd0dddd00dd00d00000dddd000dddd0dd00000ddd000ddd00dd00d0dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 015:000000000000000000000000000000000000000000000000000000000000000000000000000dd0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 030:0000000000dddd00dd0000000000000dd0dd000dd00000000000000000000dd0000000000000000000000000dd00dd000000000dd00000000dd000000000000dd000000000000000000000000000000000000000dd0000000000000000000000000000000000000000000000000000000000000000000000
-- 031:00000000000dd00ddddd00000d000d0000dd000dd00000000dddd0dd00d0ddddd00ddd0000000dddd0d000d0000ddddd00dddd0dddd000000000dddd000000ddd0000000dddd00ddd000dddd00ddd00dddd000dddd00dddd0000000000000000000000000000000000000000000000000000000000000000
-- 032:00000000000dd000dd0000000d0d0d0dd0dd000dd0000000d00dd0dd00d00dd000dd00d0ddd0ddd000d0d0d0dd00dd000ddd000dd00d00000dd0dd00d000000dd000000ddd000dd0dd0ddd000dd00d0dd00d0d00dd0ddd000000000000000000000000000000000000000000000000000000000000000000
-- 033:00000000000dd000dd0000000ddddd0dd0dd000dd0000000d00dd0dd00d00dd000dd00d0000000ddd0ddddd0dd00dd000ddd000dd00d00000dd0dd00d000000dd00000000ddd0ddd000ddd000dd00d0dd00d0d00dd000ddd0000000000000000000000000000000000000000000000000000000000000000
-- 034:0000000000dddd000ddd00000dd0dd0dd00ddd00ddd000000dddd00ddd0000ddd00ddd000000dddd00dd0dd0dd000ddd00dddd0dd00d00000dd0dd00d00000dddd00000dddd000ddd000dddd00ddd00dd00d00dddd0dddd00000000000000000000000000000000000000000000000000000000000000000
-- </SCREEN>

-- <PALETTE>
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
-- </PALETTE>