module Tile
    ( tileToRenderList
    , makeTileFromPoly
    ) where
import Data.Tile
import Geometry

tileToRenderList :: Tile -> [(Point3,Point3)]
tileToRenderList t = polyToTris $ zip ps3 coords3
  where
    ps = _tilePoly t
    coords = map (calcTexCoord (_tileZero t) (_tileX t)) ps
    ps3 = map (addZ 0) ps
    coords3 = map (addZ (_tileZ t)) coords

calcTexCoord
    :: Point2   -- ^ tile (0,0)
    -> Point2   -- ^ tile (1,0)
    -> Point2   -- ^ world point
    -> Point2
calcTexCoord c p x = V2 (vectorAmountIn xdir) (vectorAmountIn ydir)
  where
    vectorAmountIn dir = 0.02 * dotV (p -.- c) dir
    xdir = x -.- c
    ydir = vNormal xdir

makeTileFromPoly :: [Point2] -> Float -> Tile
makeTileFromPoly poly z = Tile
    { _tilePoly = poly
    , _tileZero = c
    , _tileX  = c +.+ normalizeV ((poly !! 1) -.- c)
    , _tileZ = z
    }
  where
    c = head poly
