Skip to content

Commit

Permalink
Version 1.0 (#1)
Browse files Browse the repository at this point in the history
* chore: WiP

* chore:  Some tidy up

* bugfix:  jsEnv := PhantomJSEnv(autoExit = false).value blocks testing
  • Loading branch information
SeriousSoftware committed Sep 4, 2016
1 parent 07a7520 commit 7756148
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 57 deletions.
33 changes: 23 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
# sbt
lib_managed
project/project
target
# Log files
*.class
*.log

# sbt specific
.cache
dist/*
lib_managed/
project/**/project
project/activator-sbt*
src_managed/
target/

# Worksheets (Eclipse or IntelliJ)
*.sc

# Eclipse
.cache
# Scala-IDE specific
.classpath
.project
.scala_dependencies
.settings
.target
.worksheet

# IntelliJ
.idea
*.iml
*.iws
.idea/
/classes/
/out/

# ScalaTest
exportToHTML

# Mac
.DS_Store

# Log files
*.log
# Windows
desktop.ini
thumbs.db
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Simple HTML5 Canvas game ported to Scala.js

Original tutorial in Javascript :
[how-to-make-a-simple-html5-canvas-game](http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/)
[How to make a simple HTML5 Canvas game](http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/)

Play the [live demo](http://goo.gl/oqSFCa).
Play the [live demo](http://goo.gl/oqSFCa). Scala doc is [here](https://amsterdam-scala.github.io/Sjs-Simple-HTML5-canvas-game/docs/api/index.html#nl.amsscala.package).

Further Resources, Notes, and Considerations
43 changes: 22 additions & 21 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
name := "Simple Game"
version := "0.0-SNAPSHOT"

// ** Meta data **
description := "Simple HTML5 Canvas game ported to Scala.js."
startYear := Some(2016)
licenses += ("EUPL v.1.1", url("http://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11"))

organization := "nl.amsscala"
organizationName := "Amsterdam.scala Meetup Group"
name := "Simple Game"
version := "0.0"
description := "Simple HTML5 Canvas game ported to Scala.js."
organization := "nl.amsscala"
organizationName := "Amsterdam.scala Meetup Group"
organizationHomepage := Some(url("http://www.meetup.com/amsterdam-scala/"))
homepage := Some(url("http://github.com/amsterdam-scala/Sjs-Full-Window-HTML5-Canvas"))
homepage := Some(url("http://github.com/amsterdam-scala/Sjs-Full-Window-HTML5-Canvas"))
startYear := Some(2016)
licenses += "EUPL v.1.1" -> url("http://joinup.ec.europa.eu/community/eupl/og_page/european-union-public-licence-eupl-v11")

// KEEP THIS normalizedName CONSTANTLY THE SAME, otherwise the outputted JS filename will be changed.
normalizedName := "main"

normalizedName := "main"

// ** Scala dependencies **
scalaVersion in ThisBuild := "2.11.8"

libraryDependencies ++= Seq(
"be.doeraene" %%% "scalajs-jquery" % "0.9.0",
"org.scala-js" %%% "scalajs-dom" % "0.9.1",
"org.scalatest" %%% "scalatest" % "3.0.0" % "test",
"com.lihaoyi" %%% "scalatags" % "0.6.0"
"be.doeraene" %%% "scalajs-jquery" % "0.9.0",
"com.lihaoyi" %%% "scalatags" % "0.6.0",
"org.scala-js" %%% "scalajs-dom" % "0.9.1",
"org.scalatest" %%% "scalatest" % "3.0.0" % "test"
)
skip in packageJSDependencies := false // All JavaScript dependencies to be concatenated to a single file

scalacOptions in (Compile,doc) ++= Seq("-doc-root-content", baseDirectory.value+"/src/main/scala-2.11/root-doc.md",
"-groups", "-implicits")

// ** Scala.js configuration **
lazy val root = (project in file(".")).enablePlugins(ScalaJSPlugin)
// lazy val root = (project in file(".")).
enablePlugins(ScalaJSPlugin)

// Necessary for testing
jsDependencies += RuntimeDOM
scalaJSUseRhino in Global := false
//testFrameworks += new TestFramework("utest.runner.Framework")

// If true, a launcher script src="../[normalizedName]-launcher.js will be generated
// that always calls the main def indicated by the used JSApp trait.
Expand All @@ -47,15 +47,16 @@ persistLauncher in Test := false

// Workbench settings **
if (sys.env.isDefinedAt("CI")) {
println("Workbench disabled", sys.env.getOrElse("CI", "?"))
println("Workbench disabled ", sys.env.getOrElse("CI", "?"))
Seq.empty
} else {
println("Workbench enabled")
workbenchSettings
}

if (sys.env.isDefinedAt("CI")) normalizedName := normalizedName.value // Dummy
else refreshBrowsers <<= refreshBrowsers.triggeredBy(fastOptJS in Compile)
else // Update without refreshing the page every time fastOptJS completes
updateBrowsers <<= updateBrowsers.triggeredBy(fastOptJS in Compile)

if (sys.env.isDefinedAt("CI")) normalizedName := normalizedName.value
else // Workbench has to know how to restart your application
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala-2.11/nl/amsscala/simplegame/Page.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ protected trait Page {
// Create the canvas
private[simplegame] val canvas = dom.document.createElement("canvas").asInstanceOf[dom.html.Canvas]
canvas.setAttribute("crossOrigin", "anonymous")
private[this] val ctx = canvas.getContext("2d") // .asInstanceOf[dom.CanvasRenderingContext2D]
private[simplegame] val ctx = canvas.getContext("2d").asInstanceOf[dom.CanvasRenderingContext2D]
private[this] val (bgImage, heroImage, monsterImage) = (Image("img/background.png"), Image("img/hero.png"), Image("img/monster.png"))

/**
Expand Down Expand Up @@ -64,7 +64,7 @@ protected trait Page {
}

canvas.width = dom.window.innerWidth.toInt
canvas.height = dom.window.innerHeight.toInt - 24
canvas.height = dom.window.innerHeight.toInt - 25
println(s"Dimension of canvas set to ${canvas.width},${canvas.height}")
canvas.textContent = "Your browser doesn't support the HTML5 CANVAS tag."

Expand Down
10 changes: 5 additions & 5 deletions src/main/scala-2.11/nl/amsscala/simplegame/game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected trait Game {
* @param monstersCaught The score
* @param newGame Flags new game
*/
private case class GameState(
case class GameState(
hero: Hero[Int],
monster: Monster[Int],
monstersCaught: Int = 0,
Expand Down Expand Up @@ -132,7 +132,7 @@ private case class GameState(
* @param pos Monsters' position
* @tparam T Numeric generic abstraction
*/
private class Monster[T: Numeric](val pos: Position[T]) {
class Monster[T: Numeric](val pos: Position[T]) {
override def equals(that: Any): Boolean = that match {
case that: Monster[T] => this.pos == that.pos
case _ => false
Expand All @@ -143,15 +143,15 @@ private class Monster[T: Numeric](val pos: Position[T]) {
pos.isValidPosition(Position(canvas.width, canvas.height).asInstanceOf[Position[T]], Hero.size.asInstanceOf[T])
}

private object Monster {
object Monster {
// def apply[T: Numeric](pos: Position[T]) = new Monster(pos)
def apply[T: Numeric](x: T, y: T) = new Monster(Position(x, y))
}

private class Hero[A: Numeric](override val pos: Position[A]) extends Monster[A](pos)
class Hero[A: Numeric](override val pos: Position[A]) extends Monster[A](pos)

/** Compagnion object of class Hero */
private object Hero {
object Hero {
val size = 32
val speed = 256

Expand Down
7 changes: 7 additions & 0 deletions src/main/scala-2.11/root-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is the documentation for a simple HTML5 Canvas game written in Scala, and cross compiled to run in the browser targeting the HTML5 Canvas.

== Package structure ==

Notable packages include:

- [[nl.amsscala.simplegame `nl.amsscala.simplegame`]]
16 changes: 1 addition & 15 deletions src/test/scala-2.11/nl/amsscala/simplegame/GameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,12 @@ class GameSuite extends SuiteSpec {
game.updateGame(1D, mutable.Map(Down -> dummyTimeStamp, Right -> dummyTimeStamp), canvas) shouldBe games.last

}
it("sad path") {
// Illegal key code
it("sad path") { // Illegal key code
game.updateGame(1D, mutable.Map(0 -> dummyTimeStamp), canvas) shouldBe game
}
it("bad path") { // No move due a of out canvas limit case
game.updateGame(1.48828125D, mutable.Map(Right -> dummyTimeStamp, Down -> dummyTimeStamp), canvas) shouldBe game
}
it("experiment") {

println(games.mkString("\n"))

val gs = new GameState(canvas, -1, false)
canvas.setAttribute("crossOrigin", "anonymous")
val ctx = canvas.getContext("2d").asInstanceOf[dom.CanvasRenderingContext2D]

val y: scala.collection.mutable.Seq[Int] =
ctx.getImageData(0, 0, canvas.width, canvas.height).data // .asInstanceOf[js.Array[Int]]

println(s"Data, ${y.hashCode()}")
}

}
}
Expand Down
39 changes: 37 additions & 2 deletions src/test/scala-2.11/nl/amsscala/simplegame/PageSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@ package nl.amsscala.simplegame

import org.scalajs.dom

class PageSuite extends SuiteSpec /*with Page*/ {
val page = new Page {}
import scala.scalajs.js
import scalatags.JsDom.all._

class PageSuite extends SuiteSpec {
val page = new Page {
canvas.width = 1242 // 1366
canvas.height = 674 // 768
}

describe("A Hero") {
describe("should tested within the limits") {

it("good path") {
{
page.render(GameState(Hero(621, 337), Monster(0, 0), 0, false))
val imageData: scala.collection.mutable.Seq[Int] =
page.ctx.getImageData(0, 0, page.canvas.width, page.canvas.height).data

imageData.hashCode() shouldBe -1753260013
}

{
page.render(GameState(Hero(365, 81), Monster(0, 0), 0, false))

dom.document.body.appendChild(div(
cls := "content", style := "text-align:center; background-color:#3F8630;",
canvas
).render)

val imageData = page.ctx.getImageData(0, 0, page.canvas.width, page.canvas.height)

// imageData.data.sum shouldBe -1753260013
}

{
page.render(GameState(Hero(877, 593), Monster(0, 0), 0, false))
val imageData: scala.collection.mutable.Seq[Int] =
page.ctx.getImageData(0, 0, page.canvas.width, page.canvas.height).data

imageData.hashCode() shouldBe -1753260013
}

}

Expand Down

0 comments on commit 7756148

Please sign in to comment.