Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added PauseMenu using update Button using updated MultiController =)

  • Loading branch information...
commit 088ddecd6badc4ad2b1319ab8024f3c0486bf049 1 parent 4802c20
Andrey Borunov authored
View
81 blases/src/main/scala/net/scage/blases/Blases.scala
@@ -5,76 +5,44 @@ import net.scage.ScageLib._
import net.scage.support.{State, Vec}
import net.scage.support.physics.ScagePhysics
import net.scage.support.tracer3.{TraceTrait, Trace, CoordTracer}
-import net.scage.support.physics.objects.{StaticLine, DynaBall, StaticPolygon}
-import collection.mutable.{HashMap, ArrayBuffer}
-import net.scage.ScageScreen
+import net.scage.support.physics.objects.{StaticLine, DynaBall}
+import collection.mutable.ArrayBuffer
import net.scage.blases.Relatives._
-import ui.Button
+import ui.PauseMenu
+import net.scage.Screen
+import net.scage.handlers.controller2.MultiController
-object Blases extends ScageScreen("Blases Game") {
+object Blases extends Screen("Blases Game") with MultiController {
val physics = ScagePhysics()
val tracer = CoordTracer.create[Blase](solid_edges = false)
- private var current_level = 0
- private val levels = ArrayBuffer(/*Level1, Level2, Level3, Level4, */Level5)
+ private[blases] var current_level = 0
+ private[blases] val levels = ArrayBuffer(Level1, Level2, Level3, Level4, Level5)
- private var score = 0
+ private[blases] var score = 0
private[blases] var score_for_level = 10000
action(1000) {
- if(is_game_started && current_game_status == IN_PLAY) score_for_level -= 50
+ if(is_game_started) score_for_level -= 50
}
action {
physics.step()
- checkGameStatus()
- if(current_game_status != IN_PLAY) {
+
+ if(is_game_started && tracer.tracesList.isEmpty) PauseMenu.showLoseLevelMenu()
+ else if(levels(current_level).isWin) {
score += score_for_level
- pause()
- }
- }
-
- val IN_PLAY = 0
- val WIN = 1
- val LOSE = 2
- private var current_game_status = IN_PLAY
- def checkGameStatus() {
- if(is_game_started && tracer.tracesList.isEmpty) current_game_status = LOSE
- if(levels(current_level).isWin) current_game_status = WIN
- }
-
- interface {
- /*print("Score: "+score, 20, windowHeight-20, WHITE)
- print(score_for_level, 20, windowHeight-40, WHITE)*/
- if(onPause) {
- current_game_status match {
- case WIN =>
- if(current_level < levels.length-1) {
- print("You win! Score for the level:\n"+score_for_level+"\n\nOverall score:\n"+score+"\n\nWanna go to the next level? (Y/N)", windowCenter + Vec(-60, 60), RED)
- } else print("You beat the game!!! Final score:\n"+score+"\n\nWanna play again from the beginning? (Y/N)", windowCenter + Vec(-60, 60), RED)
- case LOSE => print("You lose. Final score:\n"+score+"\n\nWanna play the last level again? (Y/N)", windowCenter + Vec(-60, 60), RED)
- case _ =>
- }
+ if(current_level == levels.length-1) PauseMenu.showBeatGameMenu()
+ else PauseMenu.showWinLevelMenu()
}
}
- keyNoPause(KEY_Y, onKeyDown = if(onPause && current_game_status != IN_PLAY) {
- current_game_status match {
- case WIN =>
- if(current_level < levels.length-1) {
- current_level += 1
- } else {
- score = 0
- current_level = 0
- }
- case LOSE =>
- score -= score_for_level
+ keyNoPause(KEY_ESCAPE, onKeyDown = {
+ PauseMenu.status match {
+ case -1 => PauseMenu.showEscMenu()
+ case PauseMenu.PRESS_ESC => PauseMenu.hide()
case _ =>
}
- restart()
- pauseOff()
})
- keyNoPause(KEY_N, onKeyDown = if(onPause && (current_game_status != IN_PLAY)) stop())
-
key(KEY_SPACE, onKeyDown = if(selected_blase != no_selection) selected_blase.velocity = Vec.zero)
render {
@@ -89,13 +57,11 @@ object Blases extends ScageScreen("Blases Game") {
}
private[blases] var selected_blase = no_selection
- /*private val replay_level_button = new Button()*/
-
leftMouse(onBtnDown = {mouse_coord =>
if(!is_game_started) {
val new_blase_position = (mouse_coord - levels(current_level).startCoord).n*50 + levels(current_level).startCoord
- val new_blase = new Blase(new_blase_position, mouse_coord - levels(current_level).startCoord)
+ new Blase(new_blase_position, mouse_coord - levels(current_level).startCoord)
is_game_started = true
} else if(selected_blase.id == no_selection.id) {
val blases = tracer.tracesNearCoord(mouse_coord, -1 to 1, condition = {blase => blase.location.dist(mouse_coord) <= 20})
@@ -104,13 +70,12 @@ object Blases extends ScageScreen("Blases Game") {
}
} else {
val new_blase_position = (mouse_coord - selected_blase.location).n*50 + selected_blase.location
- val new_blase = new Blase(new_blase_position, mouse_coord - selected_blase.location)
+ new Blase(new_blase_position, mouse_coord - selected_blase.location)
selected_blase = no_selection
}
})
rightMouse(onBtnDown = {mouse_coord =>
- /*selected_blase = no_selection*/
if(tracer.tracesList.length > 1) {
val blases = tracer.tracesNearCoord(mouse_coord, -1 to 1, condition = {blase => blase.location.dist(mouse_coord) <= 20})
if(!blases.isEmpty) {
@@ -135,14 +100,14 @@ object Blases extends ScageScreen("Blases Game") {
score = 0
current_level = 0
- pauseOff()
+
+ PauseMenu.hide()
}
private var is_game_started = false
init {
levels(current_level).load()
is_game_started = false
- current_game_status = IN_PLAY
score_for_level = 10000
}
View
4 blases/src/main/scala/net/scage/blases/Relatives.scala
@@ -4,7 +4,7 @@ import net.scage.support.Vec
import net.scage.ScageLib._
object Relatives {
- def rVec(x:Float, y:Float):Vec = Vec(x/1024f*windowWidth, y/768f*windowHeight)
+ def rVec(x:Float, y:Float):Vec = Vec((x/1024f*windowWidth).toInt, (y/768f*windowHeight).toInt)
def rInt(i:Int):Int = (i*windowWidth/1024f).toInt
- def rVec(vec: Vec): Vec = vec / (1024f) * windowWidth
+ def rVec(vec: Vec): Vec = Vec((vec.x/1024f*windowWidth).toInt, (vec.y/768f*windowHeight).toInt)
}
View
4 blases/src/main/scala/net/scage/blases/levels/Level5.scala
@@ -63,9 +63,9 @@ object Level5 extends Level {
drawStartFinish()
}
- interface {
+ /*interface {
print(mouseCoord, 20, 20, GREEN)
- }
+ }*/
clear {
physics.removePhysicals(first, second, third, fourth, fifth, sixth, seventh, eighth)
View
74 blases/src/main/scala/net/scage/blases/tests/MultiControllerTest.scala
@@ -0,0 +1,74 @@
+package net.scage.blases.tests
+
+import net.scage.support.Vec
+import net.scage.handlers.Renderer
+import net.scage.{Screen, Scage, ScreenApp}
+import net.scage.ScageLib._
+import net.scage.handlers.controller2.{SingleController, MultiController}
+
+class Button(message: String, message_coord: Vec, width: Int, screen: Screen, onBtnPressed: => Any) {
+ val vertices = List(Vec(message_coord) + Vec(-5, 20),
+ Vec(message_coord) + Vec(-5 + width, 20),
+ Vec(message_coord) + Vec(-5 + width, -10),
+ Vec(message_coord) + Vec(-5, -10))
+ val vertices_zipped = if (vertices.length >= 2) {
+ val vertices_shift = vertices.last :: vertices.init
+ vertices_shift.zip(vertices)
+ } else List[(Vec, Vec)]()
+ def containsCoord(coord: Vec): Boolean = {
+ def _areLinesIntersect(a: Vec, b: Vec, c: Vec, d: Vec): Boolean = {
+ val common = (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x)
+ if (common == 0) false
+ else {
+ val rH = (a.y - c.y) * (d.x - c.x) - (a.x - c.x) * (d.y - c.y)
+ val sH = (a.y - c.y) * (b.x - a.x) - (a.x - c.x) * (b.y - a.y)
+
+ val r = rH / common
+ val s = sH / common
+
+ if (r >= 0 && r <= 1 && s >= 0 && s <= 1) true
+ else false
+ }
+ }
+ if (vertices.length < 2) false
+ else {
+ val a = coord
+ val b = Vec(Integer.MAX_VALUE, coord.y)
+ val intersections = vertices_zipped.foldLeft(0) {
+ case (result, (c, d)) => if (_areLinesIntersect(a, b, c, d)) result + 1 else result
+ }
+ intersections % 2 != 0
+ }
+ }
+
+ var visible = true
+
+ screen.render {
+ if(visible) {
+ drawPolygon(vertices, BLACK)
+ print(message, Vec(message_coord), BLACK)
+ }
+ }
+
+ screen.leftMouse(onBtnDown = {m => if(visible && containsCoord(m)) onBtnPressed})
+}
+
+object MultiControllerTest extends ScreenApp("MultiController Test", window_width=640, window_height = 480, "MultiController Test") with MultiController {
+ val pressme_button = new Button("Press Me", Vec(320, 240) + Vec(-40, 40), 100, MultiControllerTest, {
+ println("MultiControllerTest: Pressed")
+ MultiControllerTestScreen2.run()
+ })
+
+ backgroundColor = WHITE
+}
+
+object MultiControllerTestScreen2 extends Screen("MultiController Test Screen2") with MultiController {
+ preinit {
+ MultiControllerTest.changeTitle("MultiController Test Screen2")
+ }
+
+ val exit_button = new Button("Exit", Vec(320, 240) + Vec(-40, 40), 100, MultiControllerTestScreen2, {
+ println("MultiControllerTestScreen2: Pressed")
+ stop()
+ })
+}
View
38 blases/src/main/scala/net/scage/blases/ui/Button.scala
@@ -1,17 +1,24 @@
package net.scage.blases.ui
-import net.scage.support.Vec
-import net.scage.Scage
import net.scage.handlers.Renderer
import net.scage.ScageLib._
import net.scage.blases.Relatives._
+import net.scage.{Screen, Scage}
+import net.scage.support.{ScageColor, Vec}
+import net.scage.handlers.controller2.MultiController
-class Button(message: String, message_coord: Vec, width: Int, screen: Scage with Renderer) {
- def containsCoord(coord: Vec): Boolean = {
- val vertices = List(rVec(message_coord) + Vec(-5, 20),
- rVec(message_coord) + Vec(-5 + width, 20),
- rVec(message_coord) + Vec(-5 + width, -10),
- rVec(message_coord) + Vec(-5, -10))
+class Button(message: String,
+ var coord: Vec,
+ width: Int,
+ screen: Screen with MultiController,
+ onBtnPressed: => Any,
+ var color:ScageColor = BLACK,
+ var visible:Boolean = true) {
+ def containsCoord(other_coord: Vec): Boolean = {
+ val vertices = List(rVec(coord) + Vec(-5, 20),
+ rVec(coord) + Vec(-5 + width, 20),
+ rVec(coord) + Vec(-5 + width, -10),
+ rVec(coord) + Vec(-5, -10))
val vertices_zipped = if (vertices.length >= 2) {
val vertices_shift = vertices.last :: vertices.init
vertices_shift.zip(vertices)
@@ -32,8 +39,8 @@ class Button(message: String, message_coord: Vec, width: Int, screen: Scage with
}
if (vertices.length < 2) false
else {
- val a = coord
- val b = Vec(Integer.MAX_VALUE, coord.y)
+ val a = other_coord
+ val b = Vec(Integer.MAX_VALUE, other_coord.y)
val intersections = vertices_zipped.foldLeft(0) {
case (result, (c, d)) => if (_areLinesIntersect(a, b, c, d)) result + 1 else result
}
@@ -41,12 +48,11 @@ class Button(message: String, message_coord: Vec, width: Int, screen: Scage with
}
}
- private val render_id = screen.render {
- //drawPolygon(vertices, BLACK)
- print(message, rVec(message_coord), BLACK)
+ screen.interface {
+ if(visible) {
+ print(message, rVec(coord), color)
+ }
}
- def remove() {
- screen.delRenders(render_id)
- }
+ screen.leftMouseNoPause(onBtnDown = {m => if(visible && containsCoord(m)) onBtnPressed})
}
View
24 blases/src/main/scala/net/scage/blases/ui/MainMenu.scala
@@ -2,25 +2,17 @@ package net.scage.blases.ui
import net.scage.support.Vec
import net.scage.blases.Blases
-import net.scage.{Scage, ScageScreenApp}
import net.scage.ScageLib._
+import net.scage.handlers.controller2.MultiController
+import net.scage.{ScreenApp, Scage, ScageScreenApp}
-object MainMenu extends ScageScreenApp("Blases Main Menu", 640, 480, "Blases") {
+object MainMenu extends ScreenApp("Blases Main Menu", 640, 480, "Blases") with MultiController {
backgroundColor = WHITE
- val new_game_button = new Button("New Game", Vec(512, 384) + Vec(-40, 40), 100, MainMenu)
- val options_button = new Button("Options", Vec(512, 384) + Vec(-40, 0), 100, MainMenu)
- val exit_button = new Button("Exit", Vec(512, 384) + Vec(-40, -40), 100, MainMenu)
-
- leftMouse(onBtnDown = {
- m =>
- if (new_game_button.containsCoord(m)) {
- Blases.run()
- backgroundColor = WHITE
- } else if (options_button.containsCoord(m)) {
- OptionsMenu.run()
- } else if (exit_button.containsCoord(m)) {
- Scage.stopApp()
- }
+ val new_game_button = new Button("New Game", Vec(512, 384) + Vec(-40, 40), 100, MainMenu, {
+ Blases.run()
+ backgroundColor = WHITE
})
+ val options_button = new Button("Options", Vec(512, 384) + Vec(-40, 0), 100, MainMenu, OptionsMenu.run())
+ val exit_button = new Button("Exit", Vec(512, 384) + Vec(-40, -40), 100, MainMenu, Scage.stopApp())
}
View
18 blases/src/main/scala/net/scage/blases/ui/OptionsMenu.scala
@@ -1,18 +1,10 @@
package net.scage.blases.ui
-import net.scage.ScageScreen
import net.scage.support.Vec
+import net.scage.handlers.controller2.MultiController
+import net.scage.{Screen, ScageScreen}
-object OptionsMenu extends ScageScreen("Blases Options") {
- val screen_button = new Button("Screen", Vec(512, 384) + Vec(-40, 40), 100, OptionsMenu)
- val back_button = new Button("Back", Vec(512, 384) + Vec(-40, 0), 100, OptionsMenu)
-
- leftMouse(onBtnDown = {
- m =>
- if (screen_button.containsCoord(m)) {
- ScreenOptionsMenu.run()
- } else if (back_button.containsCoord(m)) {
- stop()
- }
- })
+object OptionsMenu extends Screen("Blases Options") with MultiController {
+ val screen_button = new Button("Screen", Vec(512, 384) + Vec(-40, 40), 100, OptionsMenu, ScreenOptionsMenu.run())
+ val back_button = new Button("Back", Vec(512, 384) + Vec(-40, 0), 100, OptionsMenu, stop())
}
View
102 blases/src/main/scala/net/scage/blases/ui/PauseMenu.scala
@@ -0,0 +1,102 @@
+package net.scage.blases.ui
+
+import net.scage.support.Vec
+import net.scage.blases.Blases._
+import net.scage.Scage
+import net.scage.ScageLib._
+import net.scage.blases.Blases
+import net.scage.blases.Relatives._
+
+object PauseMenu {
+ private val continue_level_button = new Button("Continue", Vec(512, 384) + Vec(-60, 40), 100, Blases, {
+ hide()
+ }, RED, false)
+ private val next_level_button = new Button("Next Level", Vec(512, 384) + Vec(-60, 40), 100, Blases, {
+ if(current_level < levels.length-1) {
+ current_level += 1
+ restart()
+ hide()
+ }
+ }, RED, false)
+ private val replay_level_button = new Button("Replay Level", Vec(512, 384) + Vec(-60, 0), 100, Blases, {
+ score -= score_for_level
+ restart()
+ hide()
+ }, RED, false)
+ private val main_menu_button = new Button("To Main Menu", Vec(512, 384) + Vec(-60, -40), 100, Blases, {
+ stop()
+ }, RED, false)
+ private val exit_button = new Button("Exit", Vec(512, 384) + Vec(-60, -80), 100, Blases, {
+ Scage.stopApp()
+ }, RED, false)
+ private val play_game_again_button = new Button("Play Levels Again", Vec(512, 384) + Vec(-60, 40), 100, Blases, {
+ score = 0
+ current_level = 0
+ restart()
+ hide()
+ }, RED, false)
+
+ val WIN_LEVEL = 0
+ val LOSE_LEVEL = 1
+ val BEAT_GAME = 2
+ val PRESS_ESC = 3
+
+ private var _status = -1
+ def status = _status
+
+ interface {
+ _status match {
+ case WIN_LEVEL =>
+ print("You win! Score for the level: "+score_for_level+". Overall score: "+score, rVec(512-60, 384+80), RED)
+ case LOSE_LEVEL =>
+ print("You lose. Final score: "+score, rVec(512-60, 384+80), RED)
+ case BEAT_GAME =>
+ print("You beat the game!!! Final score: "+score, rVec(512-60, 384+80), RED)
+ case PRESS_ESC =>
+ print("Pause", rVec(512-60, 384+80), RED)
+ case _ =>
+ }
+ }
+
+ def showWinLevelMenu() {
+ pause()
+ _status = WIN_LEVEL
+ next_level_button.visible = true // 40
+ replay_level_button.visible = true // 0
+ main_menu_button.visible = true // -40
+ exit_button.visible = true // -80
+ }
+ def showLoseLevelMenu() {
+ pause()
+ _status = LOSE_LEVEL
+ replay_level_button.visible = true // 40
+ main_menu_button.visible = true // 0
+ exit_button.visible = true // -40
+ }
+ def showBeatGameMenu() {
+ pause()
+ _status = BEAT_GAME
+ play_game_again_button.visible = true // 40
+ replay_level_button.visible = true // 0
+ main_menu_button.visible = true // -40
+ exit_button.visible = true // -80
+ }
+ def showEscMenu() {
+ pause()
+ _status = PRESS_ESC
+ continue_level_button.visible = true // 40
+ replay_level_button.visible = true // 0
+ main_menu_button.visible = true // -40
+ exit_button.visible = true // -80
+ }
+ def hide() {
+ _status = -1
+ continue_level_button.visible = false
+ next_level_button.visible = false
+ replay_level_button.visible = false
+ main_menu_button.visible = false
+ exit_button.visible = false
+ play_game_again_button.visible = false
+ pauseOff()
+ }
+}
View
29 blases/src/main/scala/net/scage/blases/ui/ScreenOptionsMenu.scala
@@ -2,28 +2,13 @@ package net.scage.blases.ui
import net.scage.ScageLib._
import net.scage.support.Vec
-import net.scage.ScageScreen
import MainMenu._
+import net.scage.handlers.controller2.MultiController
+import net.scage.{Screen, ScageScreen}
-object ScreenOptionsMenu extends ScageScreen("Blases Options Screen") {
- val resolution1_button = new Button("640 x 480", Vec(512, 384) + Vec(-40, 40), 100, ScreenOptionsMenu)
- val resolution2_button = new Button("800 x 600", Vec(512, 384) + Vec(-40, 0), 100, ScreenOptionsMenu)
- val resolution3_button = new Button("1024 x 768", Vec(512, 384) + Vec(-40, -40), 100, ScreenOptionsMenu)
- val back_button = new Button("Back", Vec(512, 384) + Vec(-40, -80), 100, ScreenOptionsMenu)
-
- leftMouse(onBtnDown = {
- m =>
- if (resolution1_button.containsCoord(m)) {
- changeResolution(640, 480)
- backgroundColor = WHITE
- } else if (resolution2_button.containsCoord(m)) {
- changeResolution(800, 600)
- backgroundColor = WHITE
- } else if (resolution3_button.containsCoord(m)) {
- changeResolution(1024, 768)
- backgroundColor = WHITE
- } else if (back_button.containsCoord(m)) {
- stop()
- }
- })
+object ScreenOptionsMenu extends Screen("Blases Options Screen") with MultiController {
+ val resolution1_button = new Button("640 x 480", Vec(512, 384) + Vec(-40, 40), 100, ScreenOptionsMenu, changeResolution(640, 480))
+ val resolution2_button = new Button("800 x 600", Vec(512, 384) + Vec(-40, 0), 100, ScreenOptionsMenu, changeResolution(800, 600))
+ val resolution3_button = new Button("1024 x 768", Vec(512, 384) + Vec(-40, -40), 100, ScreenOptionsMenu, changeResolution(1024, 768))
+ val back_button = new Button("Back", Vec(512, 384) + Vec(-40, -80), 100, ScreenOptionsMenu, stop())
}
Please sign in to comment.
Something went wrong with that request. Please try again.