Strict
Import spritesheet
Import common
Import keyboard

Const DOOR_CLOSED: Int = 0
Const DOOR_OPENED: Int = 1
Const DOOR_CLOSING: Int = 2
Const DOOR_OPENING: Int = 3

Class Door
Private
	Field door_sprite: SpriteSheet
	Field scale: Float
	Field state: Int

Public
	Field x: Int, y: Int
	Field theKey: Key
	
	Method New()
		door_sprite = New SpriteSheet("door", 7)
		SetScale (1.0)
		Reset()
	End
	
	Method SetScale: Void(s: Float)
		scale = s
		door_sprite.scale_x = scale 
		door_sprite.scale_y = scale
	End
	
	Method Reset: Void()
		state = DOOR_CLOSED
		door_sprite.SetFrame(0)
	End
	
	Method GetState: Int()
		Return state
	End
	
	Method GetScale: Float()
		Return scale
	End
	
	Method GetInsideGeometry: IntRect(dest: IntRect)
		dest.w = 90.0*scale
		dest.h = 190.0*scale
		dest.x = x - dest.w/2
		dest.y = y - dest.h/2

		Return dest
	End
		
	Method Update: Void()
		If state = DOOR_OPENING
			door_sprite.NextFrame()
			If door_sprite.GetFrame() = door_sprite.TotalFrames()-1 Then state = DOOR_OPENED
		Elseif state = DOOR_CLOSING
			door_sprite.PrevFrame()
			If door_sprite.GetFrame() = 0 Then state = DOOR_CLOSED
		Endif	
	End
	
	
	Method Open: Void()
		If state = DOOR_CLOSED Then state = DOOR_OPENING
	End
	
	Method Close: Void()
		If state = DOOR_OPENED Then state = DOOR_CLOSING
	End


	Method Render: Void()
		door_sprite.Render(x, y)
	End
End
