.O.
..O Deniz's personal pages
OOO
/home /about

Epic4x: Devlog #10

March 9, 2017

Posted on March 9, 2017 by dendiz Today I got around to actually implementing the command system. I had to dig deep into spring to get the kind of class/object interaction that I was talking about. The commands are beans and hibernate entities at the same time. The issued command is persisted in the database after validation based on the components that the commands affected ship has. So if you want to issue a move command the ship must have an engine. At each turn the commands are read from the database and their execute method is called which actually does the operations like updating a ships x/y coordinates. Hibernate can infer the type of the object after reading the database entity and call the correct execute method. Each command extends from a base abstract command object which contains all the parameters that each command will use. This is a bit off as an “attack” command will not need a destination planet field but will inherit one anyway, but the execute method will know which parameters to use anyway.

The most challenging part was figuring out how to instantiate a spring bean with constructor parameters. A move command is a bean but needs the parameters “destination planet” and “ship” at construction. The trick was using something like this:

factory = applicationContext.getAutowireCapableBeanFactory();
Iterable<Command> commands = commandRepository.findAll();
for (Command command : commands) {
   factory.autowireBean(command);
   factory.initializeBean(command, command.getClass().getSimpleName());
   command.execute();
}

commands are created with constructor parameters and saved in the database and augmented to a spring bean with this code.

Tags: #gamedev #epic4x #devlog