Skip to content

Commit

Permalink
Integrate scalafmt CI step (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC authored Aug 11, 2022
1 parent 836212a commit 2bf99b8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
java-version: 'adopt:1.11.0-11'
node-version: '16.7.0'

# - name: Check code format
# run: sbt scalafmtCheckAll
- name: Check code format
run: sbt scalafmtCheckAll

- name: Compile
run: CI=true sbt compile
Expand Down
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")

50 changes: 24 additions & 26 deletions src/main/scala/scalajsGames/games/AstroLander.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package games
import org.scalajs.dom
import scala.util.Random

case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game {
val points = {
var current = 450
var pts = List.empty[Point]
Expand All @@ -28,10 +28,10 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
x
}

(0 to 21).foreach{n =>
if (n == flat+1) current = current
else if (n == cliff1+1) current = current - Random.nextInt(25) - 150
else if (n == cliff2+2) current = current - Random.nextInt(25) + 150
(0 to 21).foreach { n =>
if (n == flat + 1) current = current
else if (n == cliff1 + 1) current = current - Random.nextInt(25) - 150
else if (n == cliff2 + 2) current = current - Random.nextInt(25) + 150
else current = current - Random.nextInt(50) + 25

if (current > bounds.y) current = (2 * bounds.y - current).toInt
Expand All @@ -49,15 +49,14 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{

def shipPoints = Seq(
craftPos + Point(15, 0).rotate(theta),
craftPos + Point(7, 0).rotate(theta + 127.5/180 * Math.PI),
craftPos + Point(7, 0).rotate(theta - 127.5/180 * Math.PI)
craftPos + Point(7, 0).rotate(theta + 127.5 / 180 * Math.PI),
craftPos + Point(7, 0).rotate(theta - 127.5 / 180 * Math.PI)
)
def draw(ctx: dom.CanvasRenderingContext2D) = {
ctx.textAlign = "left"
ctx.fillStyle = Color.Black
ctx.fillRect(0, 0, bounds.x, bounds.y)


ctx.fillStyle = if (craftVel.length < 3) Color.Green else Color.White
ctx.fillText("Speed: " + (craftVel.length * 10).toInt.toDouble / 10, 20, 50)

Expand All @@ -69,7 +68,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{

ctx.beginPath()
ctx.moveTo(0, bounds.y)
for(p <- points) ctx.lineTo(p.x, p.y)
for (p <- points) ctx.lineTo(p.x, p.y)
ctx.lineTo(bounds.x, bounds.y)
ctx.fill()

Expand All @@ -87,7 +86,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
p + Point(a, a).rotate(angle - offset),
p + Point(b, b).rotate(angle - offset + width),
p + Point(c, c).rotate(angle - offset),
p + Point(b, b).rotate(angle - offset - width),
p + Point(b, b).rotate(angle - offset - width),
p + Point(a, a).rotate(angle - offset)
)
}
Expand All @@ -96,7 +95,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
}

ctx.strokeStyle = Color.Red
if (fuel > 0){
if (fuel > 0) {
if (lastKeys(37)) drawFlame(craftPos, theta + math.Pi / 4)
if (lastKeys(39)) drawFlame(craftPos, theta - math.Pi / 4)
if (lastKeys(40)) drawFlame(craftPos, theta)
Expand All @@ -106,7 +105,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
var lastKeys: Set[Int] = Set()
def update(keys: Set[Int]) = {
lastKeys = keys
if (fuel > 0){
if (fuel > 0) {
if (keys(37)) craftVel += Point(0.5, 0).rotate(theta + math.Pi / 4)
if (keys(39)) craftVel += Point(0.5, 0).rotate(theta - math.Pi / 4)
if (keys(40)) craftVel += Point(0.5, 0).rotate(theta)
Expand All @@ -116,29 +115,28 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
craftVel += Point(0, 0.2)
craftPos += craftVel



val hit = points.flatMap{ p =>
val hit = points.flatMap { p =>
val prevIndex = points.lastIndexWhere(_.x < craftPos.x)
if (prevIndex == -1 || prevIndex == 21) None
else{
else {
val prev = points(prevIndex)
val next = points(prevIndex + 1)
val height = (craftPos.x - prev.x) / (next.x - prev.x) * (next.y - prev.y) + prev.y
if (height > craftPos.y) None
else Some{
val groundGradient = math.abs((next.y - prev.y) / (next.x - prev.x))
val landingSkew = math.abs(craftVel.x / craftVel.y)

if (groundGradient > 0.1) Failure("landing area too steep")
else if (landingSkew > 1) Failure("too much horiontal velocity")
else if(craftVel.length > 3) Failure("coming in too fast")
else Success
}
else
Some {
val groundGradient = math.abs((next.y - prev.y) / (next.x - prev.x))
val landingSkew = math.abs(craftVel.x / craftVel.y)

if (groundGradient > 0.1) Failure("landing area too steep")
else if (landingSkew > 1) Failure("too much horiontal velocity")
else if (craftVel.length > 3) Failure("coming in too fast")
else Success
}
}
}

hit.headOption.map{
hit.headOption.map {
case Success =>
result = Some("You have landed successfully.")
resetGame()
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/scalajsGames/games/Snake.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ case class Snake(bounds: Point, resetGame: () => Unit) extends Game {
x match {
case Apple(_, s) =>
/*
* Instead of using red apple and bonus apple default score (2pts and 5pts):
* - Red apple: +1 score (from 2 / 2 = 1)
* - Bonus apple: +2 score (from 5 / 2 = 2)
*
* This is for a better understanding for the user about the values of the apples
*/
* Instead of using red apple and bonus apple default score (2pts and 5pts):
* - Red apple: +1 score (from 2 / 2 = 1)
* - Bonus apple: +2 score (from 5 / 2 = 2)
*
* This is for a better understanding for the user about the values of the apples
*/
score += s / 2
length += s
case _ =>
Expand Down

0 comments on commit 2bf99b8

Please sign in to comment.