|
| 1 | +// Copyright 2025 DeepMind Technologies Limited. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.androidenv.catch |
| 16 | + |
| 17 | +import android.graphics.Canvas |
| 18 | +import android.view.MotionEvent |
| 19 | +import com.google.androidenv.catch.sprite.Background |
| 20 | +import com.google.androidenv.catch.sprite.Ball |
| 21 | +import com.google.androidenv.catch.sprite.LineSegment |
| 22 | +import com.google.androidenv.catch.sprite.Paddle |
| 23 | +import java.time.Duration |
| 24 | +import java.time.Instant |
| 25 | +import kotlin.random.Random |
| 26 | + |
| 27 | +/** The class that contains the game logic. */ |
| 28 | +open class GameLogic( |
| 29 | + // Expected number of frames per second. |
| 30 | + fps: Int = 60, |
| 31 | + // Pseudo random number generator. |
| 32 | + private val rand: Random = Random.Default, |
| 33 | + // Width and height of the game in pixels. |
| 34 | + private val width: Int, |
| 35 | + private val height: Int, |
| 36 | + // UI objects in the game. |
| 37 | + private var background: Background = Background(), |
| 38 | + private var ball: Ball = Ball(maxX = width, maxY = height, rand = rand), |
| 39 | + private var paddle: Paddle = Paddle(maxX = width, y = height), |
| 40 | +) { |
| 41 | + |
| 42 | + private val sleepTime: Duration = Duration.ofMillis((1000.0 / fps).toLong()) |
| 43 | + |
| 44 | + /** Reinitializes the state of the game. */ |
| 45 | + // Need to make this open to allow for testing. |
| 46 | + open fun reset() { |
| 47 | + this.ball.reset() |
| 48 | + } |
| 49 | + |
| 50 | + /** Runs one "throw" of a [ball] that needs to be caught by the [paddle]. */ |
| 51 | + // Need to make this open to allow for testing. |
| 52 | + open fun run(): Boolean { |
| 53 | + var lastTimestamp = Instant.now() |
| 54 | + do { |
| 55 | + Thread.sleep(sleepTime.toMillis()) |
| 56 | + val now = Instant.now() |
| 57 | + val interval = Duration.between(lastTimestamp, now) |
| 58 | + lastTimestamp = now |
| 59 | + ball.update(interval) |
| 60 | + } while (!ball.isOutOfBounds()) |
| 61 | + |
| 62 | + return ball.intersects(LineSegment(paddle.topLeft(), paddle.topRight())) |
| 63 | + } |
| 64 | + |
| 65 | + /** Processes a user event (e.g. a touchscreen event) and updates the [paddle] accordingly. */ |
| 66 | + fun handleTouch(event: MotionEvent) { |
| 67 | + paddle.x = event.x.toInt() |
| 68 | + } |
| 69 | + |
| 70 | + /** Renders the game on [c]. */ |
| 71 | + open fun render(c: Canvas) { |
| 72 | + background.draw(c) |
| 73 | + ball.draw(c) |
| 74 | + paddle.draw(c) |
| 75 | + } |
| 76 | +} |
0 commit comments