Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
Alfred Melch | 7d67e62689 | 5 years ago |
Alfred Melch | 704e0a62b1 | 5 years ago |
Alfred Melch | 1e5b5e9d4d | 5 years ago |
Alfred Melch | c648fefa7e | 5 years ago |
Alfred Melch | 3ad7b61b7d | 5 years ago |
@ -1,5 +1 @@
|
|||||||
.vscode/
|
.vscode/
|
||||||
build/
|
|
||||||
node_modules/
|
|
||||||
venv/
|
|
||||||
*.pyc
|
|
@ -1,11 +0,0 @@
|
|||||||
serve:
|
|
||||||
./node_modules/.bin/webpack-dev-server --mode development
|
|
||||||
|
|
||||||
build:
|
|
||||||
./node_modules/.bin/webpack --mode production
|
|
||||||
|
|
||||||
lint:
|
|
||||||
./node_modules/.bin/eslint src
|
|
||||||
|
|
||||||
format:
|
|
||||||
./node_modules/.bin/prettier --write src/**/*.js
|
|
@ -1,8 +1 @@
|
|||||||
#Hello there
|
# EbermergenTD
|
||||||
|
|
||||||
## Available scripts
|
|
||||||
|
|
||||||
- `npm start`: starts a development server (`make` or `make serve`)
|
|
||||||
- `npm run build`: make a production build (`make build`)
|
|
||||||
- `npm run lint`: lint js files using eslint (`make lint`)
|
|
||||||
- `npm run format`: format js files using prettier (`make format`)
|
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
venv
|
||||||
|
*.pyc
|
@ -0,0 +1,12 @@
|
|||||||
|
# Backend for EbermergenTD
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Requirements: python3, python3-venv and pip
|
||||||
|
|
||||||
|
## Available scripts
|
||||||
|
|
||||||
|
- `make venv`: creates a python virtual environment
|
||||||
|
- `make install`: installs python dependencies
|
||||||
|
- `make run`: runs the development server
|
||||||
|
- `make test`: runs unittests
|
@ -0,0 +1,50 @@
|
|||||||
|
import atexit
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import queue
|
||||||
|
|
||||||
|
from .gameloop import GameLoop
|
||||||
|
|
||||||
|
|
||||||
|
def tick(dt, first, second):
|
||||||
|
print('%.2f' % dt, first, second)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def worker(state, action_queue):
|
||||||
|
state['counter'] = 0
|
||||||
|
state['actions'] = []
|
||||||
|
loop = GameLoop(tick=tick, fps=1, fixed_dt=True, args=(1, 2))
|
||||||
|
while True:
|
||||||
|
item = action_queue.get()
|
||||||
|
if item['action'] == 'STOP':
|
||||||
|
loop.stop()
|
||||||
|
if item['action'] == 'START':
|
||||||
|
loop.start()
|
||||||
|
state['counter'] += 1
|
||||||
|
state['actions'].append(item['action'])
|
||||||
|
|
||||||
|
|
||||||
|
class EbermergenGame:
|
||||||
|
"""Runs a single game instance
|
||||||
|
|
||||||
|
Holds the serialized state.
|
||||||
|
Provides methods to perform actions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.state = {}
|
||||||
|
self.actions = queue.Queue()
|
||||||
|
self.thread = threading.Thread(
|
||||||
|
target=worker, args=(self.state, self.actions))
|
||||||
|
|
||||||
|
def start_game(self):
|
||||||
|
self.thread.start()
|
||||||
|
atexit.register(self.interupt)
|
||||||
|
|
||||||
|
def interupt(self):
|
||||||
|
# handle gracefull shutdown
|
||||||
|
pass
|
||||||
|
|
||||||
|
def perform_action(self, action, payload={}):
|
||||||
|
self.actions.put({'action': action, 'payload': payload})
|
@ -0,0 +1,46 @@
|
|||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
class GameLoop():
|
||||||
|
"""Implements a loop that calls tick every 1/fps
|
||||||
|
|
||||||
|
tick gets called with the time delta to the last call as parameter
|
||||||
|
if fixed_dt is True the tick function gets called with 1/fps
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, fps=1, tick=None, fixed_dt=False, args=None):
|
||||||
|
self.fps = fps
|
||||||
|
self.tick = tick if tick is not None else self.default_tick
|
||||||
|
self.args = args or tuple()
|
||||||
|
self.fixed_dt = fixed_dt
|
||||||
|
self.running = False
|
||||||
|
self.last_frame_time = time.time()
|
||||||
|
self.thread = threading.Thread(target=self.loop)
|
||||||
|
|
||||||
|
def loop(self):
|
||||||
|
while self.running:
|
||||||
|
current_time = time.time()
|
||||||
|
dt = current_time - self.last_frame_time
|
||||||
|
self.last_frame_time = current_time
|
||||||
|
|
||||||
|
if self.fixed_dt:
|
||||||
|
self.tick(1 / self.fps, *self.args)
|
||||||
|
else:
|
||||||
|
self.tick(dt, *self.args)
|
||||||
|
|
||||||
|
sleep_time = (1 / self.fps) - (time.time() - self.last_frame_time)
|
||||||
|
if sleep_time > 0:
|
||||||
|
time.sleep(sleep_time)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.running = True
|
||||||
|
self.thread.start()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.running = False
|
||||||
|
|
||||||
|
def default_tick(self, dt):
|
||||||
|
print('ticked. Last tick was %.2f seconds ago' % dt)
|
||||||
|
time.sleep(random.choice([0.4, 0.5, 0.6, 1.4, 2.1]) * 1/self.fps)
|
@ -0,0 +1,2 @@
|
|||||||
|
node_modules/
|
||||||
|
build/
|
@ -0,0 +1,14 @@
|
|||||||
|
# Hello there
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Requirements: node and npm
|
||||||
|
|
||||||
|
- `npm install`: installs dependencies in node_modules
|
||||||
|
|
||||||
|
## Available scripts
|
||||||
|
|
||||||
|
- `npm start`: starts a development server
|
||||||
|
- `npm run build`: make a production build
|
||||||
|
- `npm run lint`: lint js files using eslint
|
||||||
|
- `npm run format`: format js files using prettier
|
Loading…
Reference in New Issue