Skip to content
Permalink
Browse files
Adding dots on mouse click
  • Loading branch information
Dave Smith committed Jun 7, 2020
1 parent c090708 commit ca6f16c09c0d1be29da960dc26a9b7b5e8198bab
Showing 1 changed file with 67 additions and 18 deletions.
@@ -2,7 +2,7 @@ import indigo._
import scala.scalajs.js.annotation.JSExportTopLevel

@JSExportTopLevel("IndigoGame")
object HelloIndigo extends IndigoSandbox[Unit, Unit] {
object HelloIndigo extends IndigoSandbox[Unit, Model] {

val magnification = 3

@@ -14,9 +14,10 @@ object HelloIndigo extends IndigoSandbox[Unit, Unit] {

val assetName = AssetName("dots")

val assets: Set[indigo.AssetType] = Set(
AssetType.Image(AssetName("dots"), AssetPath("assets/dots.png"))
)
val assets: Set[indigo.AssetType] =
Set(
AssetType.Image(AssetName("dots"), AssetPath("assets/dots.png"))
)

val fonts: Set[indigo.FontInfo] =
Set()
@@ -27,30 +28,78 @@ object HelloIndigo extends IndigoSandbox[Unit, Unit] {
): indigo.Startup[indigo.StartupErrors, Unit] =
Startup.Success(())

def initialModel(startupData: Unit): Unit =
()
def initialModel(startupData: Unit): Model =
Model.initial(
config.viewport.giveDimensions(magnification).center
)

def updateModel(
context: indigo.FrameContext,
model: Unit
): indigo.GlobalEvent => indigo.Outcome[Unit] =
_ => Outcome(())
model: Model
): indigo.GlobalEvent => indigo.Outcome[Model] = {
case MouseEvent.Click(x, y) =>
val adjustedPosition = Point(x, y) - model.center

Outcome(
model.addDot(
Dot(
Point.distanceBetween(model.center, Point(x, y)).toInt,
Radians(
Math.atan2(
adjustedPosition.x.toDouble,
adjustedPosition.y.toDouble
)
)
)
)
)

case FrameTick =>
Outcome(model.update(context.delta))

case _ =>
Outcome(model)
}

def present(
context: indigo.FrameContext,
model: Unit
model: Model
): indigo.SceneUpdateFragment =
SceneUpdateFragment(
Graphic(Rectangle(0, 0, 32, 32), 1, Material.Textured(assetName)),
Graphic(Rectangle(0, 0, 32, 32), 1, Material.Textured(assetName))
).addGameLayerNodes(
drawDots(model.center, model.dots)
)

def drawDots(
center: Point,
dots: List[Dot]
): List[Graphic] =
dots.map { dot =>
val position = Point(
(Math.sin(dot.angle.value) * dot.orbitDistance + center.x).toInt,
(Math.cos(dot.angle.value) * dot.orbitDistance + center.y).toInt
)

Graphic(Rectangle(0, 0, 32, 32), 1, Material.Textured(assetName))
.withCrop(Rectangle(16, 16, 16, 16))
.withRef(8, 8)
.moveTo(
Signal
.Orbit(config.viewport.giveDimensions(magnification).center, 30)
.map(_.toPoint)
.at(context.gameTime.running)
)
)
.moveTo(position)
}

}

case class Model(center: Point, dots: List[Dot]) {
def addDot(dot: Dot): Model =
this.copy(dots = dot :: dots)

def update(timeDelta: Seconds): Model =
this.copy(dots = dots.map(_.update(timeDelta)))
}
object Model {
def initial(center: Point): Model = Model(center, Nil)
}
case class Dot(orbitDistance: Int, angle: Radians) {
def update(timeDelta: Seconds): Dot =
this.copy(angle = angle + Radians.fromSeconds(timeDelta))
}

0 comments on commit ca6f16c

Please sign in to comment.