Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Utility to do some of the ECS monkey work
- # Format:
- # ./ecstool.py create component CSpr
- # ./ecstool.py create usystem UAnimSpr +CAnimSpr +CPos +CObj -CIso *CDir *CIsoDir
- # ./ecstool.py create dsystem DParticles
- # ./ecstool.py create singlesystem SCollision
- import sys
- from string import Template
- from os.path import exists
- def confirm_choice(msg):
- confirm = input(str(msg) + "[Y/N]: ")
- return confirm == 'Y' or confirm == 'y'
- # +Something *Something -Something
- def gen_aspects(args):
- aspects=""
- if args:
- asp_all=[]
- asp_one=[]
- asp_exc=[]
- for a in args:
- if a[0] == "+":
- asp_all.append("typeof(" + a[1:] + ")")
- elif a[0] == "*":
- asp_one.append("typeof(" + a[1:] + ")")
- elif a[0] == "-":
- asp_exc.append("typeof(" + a[1:] + ")")
- if len(asp_all) > 0:
- aspects+="\n\t\t.All("
- for a in asp_all:
- aspects+=a + ", "
- aspects=aspects[:-2]+")"
- if len(asp_one) > 0:
- aspects+="\n\t\t.One("
- for a in asp_one:
- aspects+=a + ", "
- aspects=aspects[:-2]+")"
- if len(asp_exc) > 0:
- aspects+="\n\t\t.Exclude("
- for a in asp_exc:
- aspects+=a + ", "
- aspects=aspects[:-2]+")"
- if len(aspects) > 0:
- aspects = "Aspect" + aspects
- return aspects
- def gen_mappers(args):
- defs=""
- inits=""
- if args:
- rel_comps = []
- for a in args:
- if a[0] == "+":
- rel_comps.append(a[1:])
- elif a[0] == "*":
- rel_comps.append(a[1:])
- if len(rel_comps) > 0:
- for c in rel_comps:
- lc=c.lower()
- # Defs
- defs += "\tprivate ComponentMapper<{tname}> {mname};\n".format(
- tname=c, mname=lc)
- # Inits
- inits += "\t\t{mname} = mapper.GetMapper<{tname}>();\n".format(
- tname=c, mname=lc)
- return defs, inits
- def create_component(name, args):
- temp = Template(
- """public class $name {
- public $name() {
- }
- }
- """)
- out = temp.safe_substitute(name=name)
- return out, "./Components/" + name + ".cs"
- def create_usystem(name, args):
- temp = Template(
- """using Microsoft.Xna.Framework;
- using MonoGame.Extended.Entities;
- using MonoGame.Extended.Entities.Systems;
- public class $name : EntityUpdateSystem{
- $mapper_defs
- public $name() : base($aspects) {
- }
- public override void Initialize(IComponentMapperService mapper) {
- $mapper_init
- }
- public override void Update(GameTime gameTime) {
- foreach (var entity in ActiveEntities) {
- }
- }
- }
- """)
- mdefs, minits = gen_mappers(args)
- out = temp.safe_substitute(name=name, aspects=gen_aspects(args),
- mapper_defs=mdefs, mapper_init=minits)
- return out, "./Systems/" + name + ".cs"
- def create_dsystem(name, args):
- temp = Template(
- """using Microsoft.Xna.Framework;
- using MonoGame.Extended.Entities;
- using MonoGame.Extended.Entities.Systems;
- public class $name : EntityDrawSystem{
- $mapper_defs
- public $name() : base($aspects) {
- }
- public override void Initialize(IComponentMapperService mapper) {
- $mapper_init
- }
- public override void Draw(GameTime gameTime) {
- foreach (var entity in ActiveEntities) {
- }
- }
- }
- """)
- mdefs, minits = gen_mappers(args)
- out = temp.safe_substitute(name=name, aspects=gen_aspects(args),
- mapper_defs=mdefs, mapper_init=minits)
- return out, "./Systems/" + name + ".cs"
- def create_singlesystem(name, args):
- pass
- factories={
- "component": create_component,
- "usystem": create_usystem,
- "dsystem": create_dsystem,
- "singlesystem": create_singlesystem
- }
- def exec_fail(reason = "Error"):
- print(str(reason) + "\nFormat:\n" +
- " ecstool.py create {component|usystem|dsystem|singlesystem} Name {aspects}")
- print(" Aspect alls: +CSpr +CPos")
- print(" ones: *CSpr *CPos")
- print(" excludes: -CSpr")
- exit(1)
- def run():
- if len(sys.argv) < 4:
- exec_fail("Incorrect Args")
- if sys.argv[1] == "create":
- print("CREATE")
- what = sys.argv[2]
- print(" " + what)
- if what in factories:
- name = sys.argv[3]
- print(" " + name)
- args=len(sys.argv)>4 and sys.argv[4:] or None
- content, where = factories[what](name, args)
- if exists(where):
- print("This " + what + " already exists.")
- choice = confirm_choice("Are you sure you want to overwrite it? ")
- if not choice:
- print("- Cancelled by user")
- exit(0)
- f = open(where, "w")
- f.write(content)
- f.close()
- print("Created: " + where)
- else:
- exec_fail("Unknown type")
- else:
- exec_fail("Unknown command")
- if __name__ == "__main__":
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement