-
Notifications
You must be signed in to change notification settings - Fork 1
/
tanks
executable file
·115 lines (89 loc) · 3.88 KB
/
tanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# Copyright (C) 2014 Marvin Poul <[email protected]>
import argparse
import sys
import logging
import math
from types import SimpleNamespace
import ctypes
import ecs
import sdl2 as sdl
import copanzers
from copanzers.systems import *
from copanzers.make import Maker
from copanzers.sdl import SDLState
def init_demo (maker, w, h, args):
maker.make_level(args.level if args.level is not None
else "presets/levels/demo.json")
def init_match (maker, w, h, args):
n = len (args.scripts)
offset = 100
for i in range (n):
maker ["ScriptedTank"] (
args.scripts [i],
pos = (offset + i * (w - 2 * offset) / (n - 1 if n > 1 else 1), h * .5)
)
def main (fps, width, height):
parser = argparse.ArgumentParser (prog = "tanks")
parser.add_argument ("--verbose", "-v", action = "count", default = 0,
help = "Can be given multiple times to increase verbosity.")
parser.add_argument ("--quiet", "-q", action = "count", default = 0,
help = "Can be given multiple times to increase quietness.")
parser.add_argument ("--script", "-l", action = "store_true",
help = "Only print logs from the script routines.")
parser.add_argument ("--speed", "-s", default = 1, type = float,
help = "Factor how fast the game should play (default: 1).")
parser.set_defaults (init = lambda *_: parser.print_help() or sys.exit(1))
subs = parser.add_subparsers ()
demo_parser = subs.add_parser ("demo")
demo_parser.set_defaults (init = init_demo)
demo_parser.add_argument("-l", "--level", default = None,
help = "Path to the level to load.")
match_parser = subs.add_parser ("match")
match_parser.add_argument ("scripts", nargs = "+",
help = "Paths to script files that should battle each other.")
match_parser.set_defaults (init = init_match)
args = parser.parse_args ()
root_logger = logging.getLogger ()
root_handler = logging.StreamHandler (sys.stderr)
root_handler.setFormatter (
logging.Formatter ("%(levelname)s %(name)s\n\t%(message)s\n"))
root_logger.addHandler (root_handler)
root_logger.setLevel (
logging.WARNING + 10 * (args.quiet - args.verbose))
if args.script:
root_filter = logging.Filter ("copanzers.scripts")
root_handler.addFilter (root_filter)
entity_manager = ecs.managers.EntityManager ()
system_manager = ecs.managers.SystemManager (entity_manager)
# small helper class to give scripts easy access to some misc info
# we could use a dictionary, but game.time is shorter than game ["time"]
# and we only need one instance anyway
class GameInfo(SimpleNamespace):
pass
game = GameInfo ()
game.size = (width, height)
game.time = 0
dt = args.speed / fps
with SDLState( resolution = (width, height) ) as gfx:
maker = Maker (entity_manager, game, gfx, "presets/entities")
system_manager.add_system (MovementSystem (width, height))
system_manager.add_system (HealthSystem ())
system_manager.add_system (WeaponSystem (maker))
system_manager.add_system (MountSystem ())
system_manager.add_system (CollisionSystem ())
system_manager.add_system (VisionSystem ())
system_manager.add_system (ScriptSystem ())
system_manager.add_system (RenderSystem (gfx = gfx))
system_manager.add_system (HealthRenderSystem (gfx = gfx))
system_manager.add_system (KillSystem ())
args.init (maker, width, height, args)
while gfx.running:
sdl.SDL_RenderClear(gfx.renderer)
system_manager.update (dt)
sdl.SDL_RenderPresent(gfx.renderer)
sdl.SDL_Delay(1000//fps)
game.time += dt
gfx.poll()
if __name__ == "__main__":
main (30, 640, 340)