Created
September 19, 2017 20:57
-
-
Save zacharycarter/b1eb4cce330ada585b25567bb1532d88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let doc = """ | |
RTS. | |
Usage: | |
rts <hostname> <port> | |
""" | |
import asyncdispatch, zengine, sdl2, opengl, random, glm, strutils, docopt, websocket, logging, message, json | |
let args = docopt(doc, version = "RTS 0.1.0") | |
const | |
ScreenWidth = 960 | |
ScreenHeight = 540 | |
SquareSize = 20 | |
Columns = 12 | |
Rows = 20 | |
LateralSpeed = 10 | |
TurningSpeed = 12 | |
FastFallAwaitCounter = 30 | |
FadingTime = 33 | |
type | |
GridSquare {.pure.} = enum | |
Empty, Moving, Full, Block, Fading | |
var | |
socket: AsyncWebSocket | |
let targetFramePeriod = 16.0 # 16 milliseconds corresponds to 60 fps | |
var frameTime: float64 = 0 | |
proc limitFrameRate(clock: Timer) = | |
let now = timeElapsed(clock) * 1000 | |
if frameTime > now: | |
delay(uint32 frameTime - now) # Delay to maintain steady frame rate | |
frameTime += targetFramePeriod | |
proc initGame() {.async.} = | |
let | |
hostname = $args["<hostname>"] | |
port = $args["<port>"] | |
socket = await newAsyncWebsocket(hostname, Port(parseInt(port)), "", ssl = false, protocols = @["rts"]) | |
info "Connected to ws://$1:$2/rts" % [hostname, port] | |
await socket.sock.sendText(toJson(createHelloMessage("bot")), true) | |
proc reader() {.async.} = | |
while true: | |
echo "READING DATA" | |
let read = await socket.sock.readData(true) | |
echo "read: " & $read | |
asyncCheck reader() | |
proc updateGame(clock: Timer) = | |
discard | |
proc drawGame(clock: var Timer) = | |
clock.tick() | |
beginDrawing() | |
clearBackground(ZENGRAY) | |
discard | |
endDrawing() | |
swapBuffers() | |
proc disposeGame() = | |
discard | |
proc main() {.async.} = | |
zengine.init(ScreenWidth, ScreenHeight, "RTS") | |
await initGame() | |
var | |
# Window control | |
evt = sdl2.defaultEvent | |
running = true | |
var clock = Timer() | |
clock.start() | |
while running: | |
while sdl2.pollEvent(evt): | |
case evt.kind: | |
# Shutdown if X button clicked | |
of QuitEvent: | |
running = false | |
of KeyUp: | |
let keyEvent = cast[KeyboardEventPtr](addr evt) | |
# Shutdown if ESC pressed | |
if keyEvent.keysym.sym == K_ESCAPE: | |
running = false | |
else: | |
discard | |
updateGame(clock) | |
drawGame(clock) | |
limitFrameRate(clock) | |
disposeGame() | |
zengine.core.shutdown() | |
asyncCheck main() | |
runForever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment