Skip to content

Commit

Permalink
tetroid preview
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperVi committed Aug 31, 2019
1 parent c0ad6dc commit bcac07b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 21 deletions.
18 changes: 17 additions & 1 deletion src/Grid.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Grid exposing (Cell, Color, Direction(..), Grid, Position, addPositions, checkGridFallDownCollision, checkGridMovementCollision, checkGridOverlap, clearPlanes, filterOutPlanes, getPlanesToRemove, isPlaneFull, isPositionNextToGrid, mergeGrids, setPosition, subtractPositions)
module Grid exposing (Cell, Color, Direction(..), Grid, Position, addPositions, checkGridFallDownCollision, checkGridMovementCollision, checkGridOverlap, clearPlanes, filterOutPlanes, getPlanesToRemove, isPlaneFull, isPositionNextToGrid, mergeGrids, reColorGrid, setPosition, subtractPositions)


type Direction
Expand Down Expand Up @@ -167,3 +167,19 @@ clearPlanes grid cellCount height =
getPlanesToRemove grid 0 height cellCount
in
( shiftRemainingGrid (filterOutPlanes grid <| planesToRemove) planesToRemove, List.length <| planesToRemove )


reColorGrid : Color -> Grid -> Grid
reColorGrid color grid =
case grid of
[] ->
[]

[ x ] ->
[ Cell color x.position ]

x :: xs ->
List.concat
[ [ Cell color x.position ]
, reColorGrid color xs
]
106 changes: 86 additions & 20 deletions src/Scene.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Scene exposing (renderGameScene, renderNextTetroidScene)

import Grid exposing (Color, Grid, Position, mergeGrids)
import Grid exposing (Color, Grid, Position, checkGridFallDownCollision, mergeGrids, reColorGrid)
import Html exposing (Html, br, button, div, h1, h2, p, span, text)
import Html.Attributes exposing (class, disabled, height, id, style, width)
import Html.Events exposing (onClick)
Expand All @@ -9,8 +9,12 @@ import Math.Matrix4 as Mat4 exposing (Mat4)
import Math.Vector3 as Vec3 exposing (Vec3, toRecord, vec3)
import Messages exposing (Msg)
import Model exposing (Model)
import WebGL exposing (Mesh, Shader)
import Movement exposing (fallDown, isCollidingWithFloor)
import Tetroids exposing (Tetroid)
import WebGL exposing (Mesh, Shader, alpha)
import WebGL.Settings exposing (Setting)
import WebGL.Settings.Blend exposing (..)
import WebGL.Settings.DepthTest exposing (..)


renderGameScene : Model -> Html Msg
Expand All @@ -27,9 +31,19 @@ renderGameScene model =
Nothing ->
model.grid
)
++ cellsToWebGLEntetiesAlpha 1
False
model
(case model.activeTetroid of
Just tetroid ->
previewTetroid model tetroid

Nothing ->
[]
)

settings =
[ WebGL.alpha True, WebGL.antialias, WebGL.depth 1 ]
options =
[ WebGL.alpha True, WebGL.antialias, WebGL.depth 1, WebGL.preserveDrawingBuffer, WebGL.clearColor 0 0 0 0 ]

--dimensions to calc responsiveness
mainWrapperWidth =
Expand All @@ -42,7 +56,7 @@ renderGameScene model =
fitGameScenee { width = round leftColumnWidth - 20, height = model.windowSize.height - 100 }
in
WebGL.toHtmlWith
settings
options
[ width (gameSceneDimensions.width - 20)
, height (gameSceneDimensions.height - 50)
, style "object-fit" "cover"
Expand Down Expand Up @@ -72,11 +86,37 @@ fitGameScenee canvasSize =
{ width = canvasSize.width, height = round (height * toFloat canvasSize.width / width) }


previewTetroid : Model -> Tetroid -> Grid
previewTetroid model activeTetroid =
let
-- set down till collision
downedTetroid =
bottomOutTetroid model activeTetroid
in
reColorGrid (Color 255 10 10) downedTetroid


bottomOutTetroid : Model -> Tetroid -> Grid
bottomOutTetroid model tetroid =
if checkGridFallDownCollision tetroid.grid model.grid || isCollidingWithFloor tetroid model.dimensions then
tetroid.grid

else
bottomOutTetroid model
(case model.activeTetroid of
Just tetroidY ->
fallDown tetroid

Nothing ->
tetroid
)


renderNextTetroidScene : Model -> Html Msg
renderNextTetroidScene model =
let
settings =
[ WebGL.alpha True, WebGL.antialias, WebGL.depth 1 ]
options =
[ WebGL.alpha True, WebGL.antialias, WebGL.depth 1, WebGL.preserveDrawingBuffer ]

mainWrapperWidth =
toFloat model.windowSize.width * 0.9
Expand All @@ -87,7 +127,7 @@ renderNextTetroidScene model =
div []
[ h2 [ style "text-align" "left" ] [ text "Next Tetroid" ]
, WebGL.toHtmlWith
settings
options
[ width (round rightColumwidth)
, height (round rightColumwidth)
, style "margin" "auto"
Expand Down Expand Up @@ -149,7 +189,7 @@ manipulatePerspective isStaticCamera zoom width height model =
0.6

eye =
vec3 (0.1 * cos (degrees (model.mousePosition.x * sensitivity))) -(0.5 - model.mousePosition.y / (height * 2)) (0.1 * sin (degrees (model.mousePosition.x * sensitivity)))
vec3 (0.1 * cos (degrees 95)) 0 (0.1 * sin (degrees 95))
|> Vec3.normalize
|> Vec3.scale zoom
in
Expand Down Expand Up @@ -203,7 +243,7 @@ cellsToWebGLEnteties isStaticCamera model cells =
[]

[ x ] ->
[ WebGL.entity
[ WebGL.entityWith [ default ]
vertexShader
fragmentShader
(cellToMesh x)
Expand All @@ -212,7 +252,7 @@ cellsToWebGLEnteties isStaticCamera model cells =

x :: xs ->
concat
[ [ WebGL.entity
[ [ WebGL.entityWith [ default ]
vertexShader
fragmentShader
(cellToMesh x)
Expand All @@ -222,6 +262,32 @@ cellsToWebGLEnteties isStaticCamera model cells =
]


cellsToWebGLEntetiesAlpha : Float -> Bool -> Model -> List Cell -> List WebGL.Entity
cellsToWebGLEntetiesAlpha alpha isStaticCamera model cells =
case cells of
[] ->
[]

[ x ] ->
[ WebGL.entityWith [ add dstColor srcColor, default ]
vertexShader
fragmentShader
(cellToMesh x)
(uniforms isStaticCamera model)
]

x :: xs ->
concat
[ [ WebGL.entityWith [ add dstColor srcColor, default ]
vertexShader
fragmentShader
(cellToMesh x)
(uniforms isStaticCamera model)
]
, cellsToWebGLEntetiesAlpha alpha isStaticCamera model xs
]



-- Mesh

Expand Down Expand Up @@ -279,12 +345,12 @@ cellToMesh cell =
shade2 =
Vec3.scale 0.9 shade1
in
[ face cellColor rft rfb rbb rbt -- right
, face shade2 rft rfb lfb lft -- front
, face shade1 rft lft lbt rbt -- top
, face shade1 rfb lfb lbb rbb -- bot
, face cellColor lft lfb lbb lbt --left
, face shade2 rbt rbb lbb lbt -- back
[ flatFace cellColor rft rfb rbb rbt -- right
, flatFace shade2 rft rfb lfb lft -- front
, flatFace shade1 rft lft lbt rbt -- top
, flatFace shade1 rfb lfb lbb rbb -- bot
, flatFace cellColor lft lfb lbb lbt --left
, flatFace shade2 rbt rbb lbb lbt -- back
]
|> List.concat
|> WebGL.triangles
Expand All @@ -309,10 +375,10 @@ playareaBase : Model -> Mesh Vertex
playareaBase model =
let
color =
vec3 50 50 50
vec3 100 100 100

plateheight =
1.5
1.25

--right front top
rft =
Expand Down Expand Up @@ -353,7 +419,7 @@ playareaBase model =
Vec3.scale 0.9 shade1
in
[ flatFace color rft rfb rbb rbt -- right
, flatFace (vec3 255 100 100) rft rfb lfb lft -- front
, flatFace (vec3 255 50 50) rft rfb lfb lft -- front
, flatFace shade1 rft lft lbt rbt -- top
, flatFace shade1 rfb lfb lbb rbb -- bot
, flatFace color lft lfb lbb lbt --left
Expand Down

0 comments on commit bcac07b

Please sign in to comment.