@@ -4,7 +4,7 @@ package games
4
4
import org .scalajs .dom
5
5
import scala .util .Random
6
6
7
- case class AstroLander (bounds : Point , resetGame : () => Unit ) extends Game {
7
+ case class AstroLander (bounds : Point , resetGame : () => Unit ) extends Game {
8
8
val points = {
9
9
var current = 450
10
10
var pts = List .empty[Point ]
@@ -28,10 +28,10 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
28
28
x
29
29
}
30
30
31
- (0 to 21 ).foreach{ n =>
32
- if (n == flat+ 1 ) current = current
33
- else if (n == cliff1+ 1 ) current = current - Random .nextInt(25 ) - 150
34
- else if (n == cliff2+ 2 ) current = current - Random .nextInt(25 ) + 150
31
+ (0 to 21 ).foreach { n =>
32
+ if (n == flat + 1 ) current = current
33
+ else if (n == cliff1 + 1 ) current = current - Random .nextInt(25 ) - 150
34
+ else if (n == cliff2 + 2 ) current = current - Random .nextInt(25 ) + 150
35
35
else current = current - Random .nextInt(50 ) + 25
36
36
37
37
if (current > bounds.y) current = (2 * bounds.y - current).toInt
@@ -49,15 +49,14 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
49
49
50
50
def shipPoints = Seq (
51
51
craftPos + Point (15 , 0 ).rotate(theta),
52
- craftPos + Point (7 , 0 ).rotate(theta + 127.5 / 180 * Math .PI ),
53
- craftPos + Point (7 , 0 ).rotate(theta - 127.5 / 180 * Math .PI )
52
+ craftPos + Point (7 , 0 ).rotate(theta + 127.5 / 180 * Math .PI ),
53
+ craftPos + Point (7 , 0 ).rotate(theta - 127.5 / 180 * Math .PI )
54
54
)
55
55
def draw (ctx : dom.CanvasRenderingContext2D ) = {
56
56
ctx.textAlign = " left"
57
57
ctx.fillStyle = Color .Black
58
58
ctx.fillRect(0 , 0 , bounds.x, bounds.y)
59
59
60
-
61
60
ctx.fillStyle = if (craftVel.length < 3 ) Color .Green else Color .White
62
61
ctx.fillText(" Speed: " + (craftVel.length * 10 ).toInt.toDouble / 10 , 20 , 50 )
63
62
@@ -69,7 +68,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
69
68
70
69
ctx.beginPath()
71
70
ctx.moveTo(0 , bounds.y)
72
- for (p <- points) ctx.lineTo(p.x, p.y)
71
+ for (p <- points) ctx.lineTo(p.x, p.y)
73
72
ctx.lineTo(bounds.x, bounds.y)
74
73
ctx.fill()
75
74
@@ -87,7 +86,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
87
86
p + Point (a, a).rotate(angle - offset),
88
87
p + Point (b, b).rotate(angle - offset + width),
89
88
p + Point (c, c).rotate(angle - offset),
90
- p + Point (b, b).rotate(angle - offset - width),
89
+ p + Point (b, b).rotate(angle - offset - width),
91
90
p + Point (a, a).rotate(angle - offset)
92
91
)
93
92
}
@@ -96,7 +95,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
96
95
}
97
96
98
97
ctx.strokeStyle = Color .Red
99
- if (fuel > 0 ){
98
+ if (fuel > 0 ) {
100
99
if (lastKeys(37 )) drawFlame(craftPos, theta + math.Pi / 4 )
101
100
if (lastKeys(39 )) drawFlame(craftPos, theta - math.Pi / 4 )
102
101
if (lastKeys(40 )) drawFlame(craftPos, theta)
@@ -106,7 +105,7 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
106
105
var lastKeys : Set [Int ] = Set ()
107
106
def update (keys : Set [Int ]) = {
108
107
lastKeys = keys
109
- if (fuel > 0 ){
108
+ if (fuel > 0 ) {
110
109
if (keys(37 )) craftVel += Point (0.5 , 0 ).rotate(theta + math.Pi / 4 )
111
110
if (keys(39 )) craftVel += Point (0.5 , 0 ).rotate(theta - math.Pi / 4 )
112
111
if (keys(40 )) craftVel += Point (0.5 , 0 ).rotate(theta)
@@ -116,29 +115,28 @@ case class AstroLander(bounds: Point, resetGame: () => Unit) extends Game{
116
115
craftVel += Point (0 , 0.2 )
117
116
craftPos += craftVel
118
117
119
-
120
-
121
- val hit = points.flatMap{ p =>
118
+ val hit = points.flatMap { p =>
122
119
val prevIndex = points.lastIndexWhere(_.x < craftPos.x)
123
120
if (prevIndex == - 1 || prevIndex == 21 ) None
124
- else {
121
+ else {
125
122
val prev = points(prevIndex)
126
123
val next = points(prevIndex + 1 )
127
124
val height = (craftPos.x - prev.x) / (next.x - prev.x) * (next.y - prev.y) + prev.y
128
125
if (height > craftPos.y) None
129
- else Some {
130
- val groundGradient = math.abs((next.y - prev.y) / (next.x - prev.x))
131
- val landingSkew = math.abs(craftVel.x / craftVel.y)
132
-
133
- if (groundGradient > 0.1 ) Failure (" landing area too steep" )
134
- else if (landingSkew > 1 ) Failure (" too much horiontal velocity" )
135
- else if (craftVel.length > 3 ) Failure (" coming in too fast" )
136
- else Success
137
- }
126
+ else
127
+ Some {
128
+ val groundGradient = math.abs((next.y - prev.y) / (next.x - prev.x))
129
+ val landingSkew = math.abs(craftVel.x / craftVel.y)
130
+
131
+ if (groundGradient > 0.1 ) Failure (" landing area too steep" )
132
+ else if (landingSkew > 1 ) Failure (" too much horiontal velocity" )
133
+ else if (craftVel.length > 3 ) Failure (" coming in too fast" )
134
+ else Success
135
+ }
138
136
}
139
137
}
140
138
141
- hit.headOption.map{
139
+ hit.headOption.map {
142
140
case Success =>
143
141
result = Some (" You have landed successfully." )
144
142
resetGame()
0 commit comments