Page 1 of 2
growing yourgamename.game.php
Posted: 13 August 2020, 10:02
by Sydnique
Hey everybody,
as I'm working on a first (example) game, I keep adding more and more code to the
"Main game logic: yourgamename.game.php" file.
Is there a recommended way to move some of that code out to another File and or Class?
There's a guys project on Github who seems to have figured it out:
https://github.com/danielholmes/battle- ... ForHill218
But so far I had no luck trying to copy that approach, and I don't want to complicate things too much.
How are you guys doing it, are you just putting everything in one class or splitting it up somehow?
Re: growing yourgamename.game.php
Posted: 13 August 2020, 11:03
by Draasill
It depends a lot on the game's complexity, IMHO.
If you like OOP, it's good to have classes representing your board, your players, your cards...
So you can do things like $card->applyEffectOnPlayer($player).
The "modules" directory exists for this.
Re: growing yourgamename.game.php
Posted: 13 August 2020, 11:13
by Sydnique
got it to work
Things to remember:
- add new class file under src/Yourgamename/YourNewClass.php
- reference your new class in yourgamename.game.php:
(put that directly below the require statement near the top of the file)
Then, to make phpunit work:
- configure autoload in composer.json
Code: Select all
"autoload": {
"psr-4": {
"Yourgamename\\": "src/Yourgamename"
}
}
- don't forget to run composer install after adding or changing the autoload config
And to get it to work in production:
- add src path to extraSrc in bgaconfig.yml
(Note: This will include the contents YourNewClass.php in the deployed version of Yourgamename.game.php)
Re: growing yourgamename.game.php
Posted: 13 August 2020, 11:16
by Sydnique
Draasill wrote: ↑13 August 2020, 11:03
It depends a lot on the game's complexity, IMHO.
If you like OOP, it's good to have classes representing your board, your players, your cards...
So you can do things like $card->applyEffectOnPlayer($player).
The "modules" directory exists for this.
Thanks for the hint, I think if one wouldn't use composer/phpunit, your approach would definetely be more elegant.
Re: growing yourgamename.game.php
Posted: 13 August 2020, 11:17
by RicardoRix
BGA may suggest you use the modules directory exclusively, and prepend all filenames with a unique identifier. For the example below, it's EU :
You can do something like this:
Code: Select all
require_once('modules/EU_DBTrains.php');
....
$this->trains = new EU_Trains($this);
You may like to extend from APP_GameClass, and pass the 'game' variable in the constructor.
Code: Select all
require_once('EU_DBTables.php');
class EU_Trains extends APP_GameClass {
function __construct($game)
{
$this->game = $game;
}
Re: growing yourgamename.game.php
Posted: 13 August 2020, 11:26
by Sydnique
@RicardoRix
Thanks, thats good to know.
Might be useful in the
BGA Studio doc 
Re: growing yourgamename.game.php
Posted: 13 August 2020, 14:36
by Draasill
Sydnique wrote: ↑13 August 2020, 11:16
Thanks for the hint, I think if one wouldn't use composer/phpunit, your approach would definetely be more elegant.
Yeah, the autoload + namespaces is better, too, I think !
Re: growing yourgamename.game.php
Posted: 13 August 2020, 16:37
by Tisaac
Draasill wrote: ↑13 August 2020, 14:36
Sydnique wrote: ↑13 August 2020, 11:16
Thanks for the hint, I think if one wouldn't use composer/phpunit, your approach would definetely be more elegant.
Yeah, the autoload + namespaces is better, too, I think !
Are you sure it works in production though ?
I already run in some troubles making sub-folder of modules/ dir working when deploying, I'm not sure if that would works flawlessly. Let us know if you have some advices on it.
I personnaly use lot of subclasses in modules with some generic ones : Utils (for thing that fits nowhere), playerManager, player, logManager, and more recently the very useful NotificationManager. I also use a lot of inheritance to handle the game's rule and make the code easier to read/maintain : one class for the Card, and then one children for each "type" of card that can overwrite the function (for instance in Santorini, almost every powers' abilities are encoded in its own class, that also stores the info about the power (name/desc/...) instead of having a huge material.inc.php file).
Re: growing yourgamename.game.php
Posted: 13 August 2020, 17:12
by Draasill
Some games are using it, yeah.
Remember there are some inspiration on
https://github.com/topics/boardgamearena !
Re: growing yourgamename.game.php
Posted: 13 August 2020, 17:29
by Sydnique
awesome - I didn't know about this list