Skip to content

Commit

Permalink
Update readme and remove isBreak
Browse files Browse the repository at this point in the history
  • Loading branch information
w0rm committed Dec 8, 2023
1 parent 4bba9ce commit b21731d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The game format is two (2) player 8-Ball. It is a simplified version of [WPA 8-B
5. On scratch, require the next player to place the cue ball anywhere on the table (ball-in-hand) ✅
6. If a player does not pocket one of their balls or scratches, the current player switches ✅
7. Ensure balls are pocketed only once or send an error
8. Support "spotted" balls when the numbered balls fall off the table

- Winning the game

Expand Down
12 changes: 9 additions & 3 deletions src/Ball.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Ball exposing (body, entity, rack, radius)

import Angle
import Axis3d
import Bodies exposing (Id(..))
import Color exposing (Color)
import EightBall
import Length exposing (Length, Meters)
import Mass exposing (Mass)
import Physics.Body as Body exposing (Body)
Expand Down Expand Up @@ -54,6 +58,8 @@ body id =
|> Body.withMaterial ballMaterial
|> Body.withDamping damping
|> Body.withBehavior (Body.dynamic weight)
-- rotate to see the numbers
|> Body.rotateAround Axis3d.x (Angle.degrees 90)


entity : Material.Texture Color -> Material.Texture Float -> Entity BodyCoordinates
Expand All @@ -68,8 +74,8 @@ entity baseColor roughnessTexture =
sphere


rack : Point2d Meters WorldCoordinates -> (Int -> Maybe id) -> List (Body id)
rack footSpot fn =
rack : Point2d Meters WorldCoordinates -> List (Body Id)
rack footSpot =
let
-- TODO: randomly shuffle the balls?
numbers =
Expand Down Expand Up @@ -105,7 +111,7 @@ rack footSpot fn =
|> Point3d.on SketchPlane3d.xy
|> Point3d.translateBy offset
in
( fn number, position )
( EightBall.numberedBall number |> Maybe.map Numbered, position )
)
|> List.filterMap
(\( maybeId, pos ) ->
Expand Down
11 changes: 2 additions & 9 deletions src/EightBall.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module EightBall exposing
, ShotEvent
, cueHitBall, cueHitWall, ballFellInPocket, ballHitWall, scratch
, Ball, oneBall, twoBall, threeBall, fourBall, fiveBall, sixBall, sevenBall, eightBall, nineBall, tenBall, elevenBall, twelveBall, thirteenBall, fourteenBall, fifteenBall, numberedBall, ballNumber
, WhatHappened(..), isBreak
, WhatHappened(..)
)

{-| Pool game rules. Agnostic to game engine.
Expand Down Expand Up @@ -51,7 +51,7 @@ module EightBall exposing
## Ruling
@docs WhatHappened, isBreak
@docs WhatHappened
-}

Expand Down Expand Up @@ -585,13 +585,6 @@ endGame (Pool poolData) =
}


{-| Returns True if the current shot is a break
-}
isBreak : Pool AwaitingPlayerShot -> Bool
isBreak (Pool data) =
lastEvent data.events == Just BallPlacedBehindHeadString


{-| Send a series of shot events.
Note: if no balls are hit by the cue ball, send an empty list.
Expand Down
27 changes: 7 additions & 20 deletions src/Game.elm
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ initialWorld table =
(Acceleration.metersPerSecondSquared 9.80665)
Direction3d.negativeZ
|> (\w -> List.foldl World.add w table.bodies)
|> (\w ->
List.foldl World.add w <|
Ball.rack
Table.footSpot
(EightBall.numberedBall >> Maybe.map Numbered)
)
|> (\w -> List.foldl World.add w (Ball.rack Table.footSpot))



Expand Down Expand Up @@ -283,7 +278,7 @@ update window msg oldModel =
{ model
| state = Simulating [] pool
, camera = Camera.zoomOut model.camera
, world = shoot axis startTime model.time (EightBall.isBreak pool) model.world
, world = shoot axis startTime model.time model.world
}

else
Expand Down Expand Up @@ -537,8 +532,8 @@ cueAxis ballPosition cameraAzimuth { hitTarget, cueElevation } =
{-| Apply impulse to the cue ball depending on the shooting strength.
The strength is calculated based on how long the spacebar has been pressed.
-}
shoot : Axis3d Meters WorldCoordinates -> Posix -> Posix -> Bool -> World Id -> World Id
shoot axis startTime endTime isBreak =
shoot : Axis3d Meters WorldCoordinates -> Posix -> Posix -> World Id -> World Id
shoot axis startTime endTime =
World.update
(\body ->
if Body.data body == CueBall then
Expand All @@ -549,13 +544,7 @@ shoot axis startTime endTime isBreak =
force =
Quantity.interpolateFrom
(Force.newtons 10)
(if isBreak then
-- Make break a bit stronger
Force.newtons 90

else
Force.newtons 60
)
(Force.newtons 60)
(shootingStrength startTime endTime)
in
Body.applyImpulse
Expand Down Expand Up @@ -785,9 +774,9 @@ view ballTextures roughnessTexture table window model =

lamp n =
Scene3d.Light.point Scene3d.Light.neverCastsShadows
{ position = Point3d.xyz (Length.meters ((n - 2) * 0.8)) Quantity.zero (Length.meters 0.5)
{ position = Point3d.xyz (Length.meters ((n - 2) * 0.8)) Quantity.zero (Length.meters 0.4)
, chromaticity = Scene3d.Light.fluorescent
, intensity = LuminousFlux.lumens 2000
, intensity = LuminousFlux.lumens 2500
}

environmentalLighting =
Expand Down Expand Up @@ -900,8 +889,6 @@ bodyToEntity roughnessTexture ballTextures table body =
|> Maybe.withDefault (Material.constant Color.black)
in
Ball.entity baseColor roughnessTexture
-- rotate to see the numbers
|> Scene3d.rotateAround Axis3d.x (Angle.degrees 90)

CueBall ->
Ball.entity (Material.constant Color.white) roughnessTexture
Expand Down

0 comments on commit b21731d

Please sign in to comment.