This commit is contained in:
2019-11-20 19:10:35 +01:00
commit b8cdce07af
6 changed files with 2010 additions and 0 deletions

16
mttt.nim Normal file
View File

@@ -0,0 +1,16 @@
import dom, jsconsole, sugar
import src/game
const
canvasId = "game_canvas"
proc onTick(game: Game, time: float) =
discard window.requestAnimationFrame((time: float) => onTick(game, time))
game.nextFrame(time)
proc onLoad(event: Event) {.exportc.} =
var game = newGame(canvasId, window.innerWidth, window.innerHeight)
onTick(game, 60)
window.onload = onLoad

20
mttt.nimble Normal file
View File

@@ -0,0 +1,20 @@
# Package
version = "0.1.0"
author = "luxick"
description = "Meta Tic Tac Toe"
license = "GPL-2.0"
srcDir = "src"
bin = @["mttt"]
backend = "js"
# Dependencies
requires "nim >= 1.0.0"
task debug, "Compile debug client":
exec "nim js --out:public/mttt.js -d:local mttt.nim"
task release, "Compile release client":
exec "nim js --out:public/mttt.js mttt.nim"

29
public/index.html Normal file
View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Meta Tic Tac Toe</title>
<script type="text/javascript" src="mttt.js"></script>
<link rel="stylesheet" type="text/css" href="mttt.css">
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
<meta name="viewport" content="width=device-width" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
</head>
<body>
<!-- <div>This site requires javascript...</div> -->
<div id="game_center">
<div id="game">
<canvas id="game_canvas"></canvas>
</div>
</div>
</body>
</html>

0
public/mttt.css Normal file
View File

1854
public/mttt.js Normal file

File diff suppressed because it is too large Load Diff

91
src/game.nim Normal file
View File

@@ -0,0 +1,91 @@
import dom, math, strformat
import gamelight/[graphics, geometry, vec, utils]
import libmttt
type
Game* = ref object
renderer: Renderer2D
lastUpdate, totalTime: float
paused: bool
scene: Scene
state: GameState
timeElement: Element
onGameStart*: proc (game: Game)
Scene {.pure.} = enum
MainMenu, Game
const
gameBgColor = "#e6e6e6"
font = "Helvetica, monospace"
padding = 10 # Padding around the game area in pixels
var
renderWidth, renderHeight: int ## Canvas render area in pixels
proc toTimeString(milliseconds: float): string =
let seconds = ((milliseconds / 1000) mod 60).toInt()
let minutes = ((milliseconds / (1000 * 60)) mod 60).toInt()
let hours = ((milliseconds / (1000 * 60 * 60)) mod 24).toInt()
result = fmt"{hours}:{minutes}:{seconds}"
proc switchScene(game: Game, scene: Scene) =
case scene:
of Scene.MainMenu:
discard
of Scene.Game:
var elements: seq[Element] = @[]
let timeTextPos = (padding.toFloat, padding.toFloat)
elements.add game.renderer.createTextElement("Time", timeTextPos, "#000000", 26, font)
let timePos = (padding.toFloat, padding.toFloat + 35.0)
game.timeElement = game.renderer.createTextElement("0", timePos, "#000000", 14, font)
proc newGame*(canvasId: string, width, height: int): Game =
var
player1 = Player(name: "Player 1")
player2 = Player(name: "Player 2")
renderWidth = width
renderHeight = height
result = Game(
renderer: newRenderer2D(canvasId, width, height),
scene: Scene.Game,
state: newGame(player1, player2),
paused: false
)
switchScene(result, Scene.Game)
proc update(game: Game, time: float) =
# Update the game logic
# Return early if paused.
if game.paused or game.scene != Scene.Game: return
game.totalTime += time
proc drawMainMenu(game: Game) =
discard
proc drawGame(game: Game) =
# Draw changing UI Elements
game.timeElement.innerHTML = game.totalTime.toTimeString
proc draw(game: Game) =
# Draw the current screen on the canvas
# Fill background color.
game.renderer.fillRect(0.0, 0.0, renderWidth, renderHeight, gameBgColor)
case game.scene
of Scene.MainMenu:
drawMainMenu(game)
of Scene.Game:
drawGame(game)
proc nextFrame*(game: Game, frameTime: float) =
# Determine id an update is necessary
game.update(frameTime)
game.draw()