------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Hamza M. Hutchinson
-- Updated: 12/2/2008

dofile( "../../Assets/Scripts/key_name_mappings.lua" )
dofile( "../../Assets/Scripts/util_math.lua" )

local myData = Entity:Data()
local BREAK_THRESHOLD = 100
local collided = false

local function WithStuff( theKey, theValue )
  collided = true
end

while( 1 ) 
do
  coroutine.yield()

  collided = false

  Entity:PHY_Refresh( myData, true )
  table.foreach( myData.collisions, WithStuff )

  if( Global:IsPressed( key_names_to_values( "Arrow Up" ) ) ) then
    Entity:PHY_AddAccelOnce(0,20,0)
  end
  if( Global:IsPressed( key_names_to_values( "Arrow Down" ) ) ) then
    Entity:PHY_AddAccelOnce(0,-50,0)
  end

  if( collided )
  then
    Entity:SHR_Refresh( myData, true, true, false, true )
    if( vec3.magnitudeSq( myData.vel ) > BREAK_THRESHOLD )
    then
      local entity
      Global:PlaySound3D( "../../Assets/Sounds/glassbox_break.mp3", myData.pos.x, myData.pos.y, myData.pos.z )
      entity = Global:CreateEntity( "../../Assets/Xml/entities/e_Seed.xml" )
      entity:SHR_sPosition( myData.pos.x, myData.pos.y, myData.pos.z )
      Global:CreateEntityAt( "../../Assets/Xml/entities/e_BreakableCubePiece.xml",
                             myData.pos.x + 1.55, myData.pos.y, myData.pos.z,
                                               0,           90,            0 )
      Global:CreateEntityAt( "../../Assets/Xml/entities/e_BreakableCubePiece.xml",
                             myData.pos.x - 1.55, myData.pos.y, myData.pos.z,
                                               0,           90,            0 )
      Global:CreateEntityAt( "../../Assets/Xml/entities/e_BreakableCubePiece.xml",
                             myData.pos.x, myData.pos.y, myData.pos.z + 1.55,
                                        0,            0,                   0 )
      Global:CreateEntityAt( "../../Assets/Xml/entities/e_BreakableCubePiece.xml",
                             myData.pos.x, myData.pos.y, myData.pos.z - 1.55,
                                        0,            0,                   0 )
      Global:CreateEntityAt( "../../Assets/Xml/entities/e_BreakableCubePiece.xml",
                             myData.pos.x, myData.pos.y + 1.55, myData.pos.z,
                                       90,                   0,            0 )
      Global:CreateEntityAt( "../../Assets/Xml/entities/e_BreakableCubePiece.xml",
                             myData.pos.x, myData.pos.y + 1.55, myData.pos.z,
                                       90,                   0,            0 )
      Global:DeleteEntityByPtr( Entity:GetMyPointer() )
    end
  end

end