From aef448119557bbb3898c38670ac07d8ed803d6e7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 18 Feb 2009 12:50:01 +0100 Subject: [PATCH] Used the new generic input functionality in place of the old one. --- src/net/habraun/kong/Main.scala | 32 +++++++++++++++++++------------- src/net/habraun/kong/Paddle.scala | 9 ++++++++- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/net/habraun/kong/Main.scala b/src/net/habraun/kong/Main.scala index 84e6596..3a41f92 100644 --- a/src/net/habraun/kong/Main.scala +++ b/src/net/habraun/kong/Main.scala @@ -20,7 +20,10 @@ package net.habraun.kong +import input._ + import java.awt._ +import java.awt.event._ import java.awt.geom._ import javax.swing._ @@ -67,12 +70,21 @@ object Main { canvas.getLayer.addChild(middleLine) // Configure the input handler - val inputHandler = new InputHandler - canvas.getRoot.getDefaultInputManager.setKeyboardFocus(inputHandler) + object PlayerLeft extends Player + object PlayerRight extends Player + object UpKey extends Key + object DownKey extends Key + val keyMap = (new KeyMap) + .addMapping(PlayerLeft, UpKey, KeyEvent.VK_W) + .addMapping(PlayerLeft, DownKey, KeyEvent.VK_S) + .addMapping(PlayerRight, UpKey, KeyEvent.VK_UP) + .addMapping(PlayerRight, DownKey, KeyEvent.VK_DOWN) + val keyHandler = new KeyHandler(keyMap) + canvas.getRoot.getDefaultInputManager.setKeyboardFocus(keyHandler) // Initialize paddles - val paddle1 = new Paddle(border + Paddle.radius, screenSizeY / 2) - val paddle2 = new Paddle(screenSizeX - border - Paddle.radius, screenSizeY / 2) + val paddle1 = new Paddle(PlayerLeft, border + Paddle.radius, screenSizeY / 2) + val paddle2 = new Paddle(PlayerRight, screenSizeX - border - Paddle.radius, screenSizeY / 2) val paddles = paddle1::paddle2::Nil // Initialize scene graph nodes for paddles @@ -126,15 +138,9 @@ object Main { while (true) { // Process input for (i <- 0 until paddles.length) { - if (inputHandler.isUpPressed(i)) { - paddles(i).movementUp - } - else if (inputHandler.isDownPressed(i)) { - paddles(i).movementDown - } - else { - paddles(i).movementStop - } + keyHandler.doIfOrElse((paddles(i).getPlayer, UpKey, () => paddles(i).movementUp):: + (paddles(i).getPlayer, DownKey, () => paddles(i).movementDown)::Nil, + () => paddles(i).movementStop) } // Step the physics simulation diff --git a/src/net/habraun/kong/Paddle.scala b/src/net/habraun/kong/Paddle.scala index fd99f3e..6deaa0f 100644 --- a/src/net/habraun/kong/Paddle.scala +++ b/src/net/habraun/kong/Paddle.scala @@ -19,13 +19,16 @@ package net.habraun.kong + +import input._ + import net.phys2d.math._ import net.phys2d.raw._ import net.phys2d.raw.shapes._ -class Paddle(initialX: Float, initialY: Float) { +class Paddle(player: Player, initialX: Float, initialY: Float) { val body = new Body(new Circle(Paddle.radius), 100) body.setPosition(initialX, initialY) @@ -36,6 +39,10 @@ class Paddle(initialX: Float, initialY: Float) { + def getPlayer = player + + + def movementUp { body.setForce(0, -Paddle.speed) } -- 2.11.4.GIT