Pirate Hell v2

Nothing fancy this week, too, so I’ll twaddle a little bit about the mechanics of the game.



I’m using dependency injection! (woo, what a word ^^ )



This is one of the most brilliant concepts I’ve ever learned. What does it do? It wires all your components together.



That means:

Let’s pretend you have a component which creates and configures scene-objects. We’ll call that one “ModelFactory.java”.

Now you have a component which creates your whole ingame-stuff including objects. We’ll call that one “SceneryComposer.java”



Now as you see the sceneryComposer could take advantage of the modelFactory. So somewhere in your code of the sceneryComposer you have to set it.



[java]

class SceneryComposer

{

private ModelFactory _modFact;







public void setModelFactory(ModelFactory factory)

{

_modFact = factory;

}





}

[/java]



Now imagine you have a huge game with hundreds of components and everyone needs dependencies to other components. These “component wiring” will be quite a task.



With dependency injection you will get rid of this problem once and for all.

the theory is this:

  1. There’s a method which will find all components of your game.
  2. There’s a method which will iterate through all these components and look at the dependencies. If it finds a component for that dependency it will set it.


  3. Tadaa, you have a fully wired list of components.



    There are some good solutions for this task. I would recommend the spring framework, because it’s easy to handle and pretty nifty. For pirateHell I wrote my own one because the spring-solution needs round about 6mb of sources to work. My solutions has some disadvantages and is not that flexible, but the basics are the same like in spring.



    My solutions works like this:

    Every component has a annotation called ‘@component’.



    [java]

    @component

    class ModelFactory{…}

    [/java]



    Now I have a method which is able to scan a specific class-path and it’s sub-paths (so it hasn’t to scan the whole java-vm).

    For every class the scanner finds it will create an object with the standard constructor and put it in a list.



    Now we have a list with all components of the application.



    Every dependency is annotated with ‘@needed



    [java]

    @component

    class SceneryComposer

    {

    @needed

    public ModelFactory _modFact;



    }

    [/java]



    Now I have a method which iterates through the list of components. Every component will be scanned for properties with the @needed-annotation. If it finds one it will search the component-list for this attribute and sets it directly (that’s why the attribute is ‘public’)



    And that’s it. Your SceneComposer now has the reference to the ModelFactory. And also every other component in your game will have every reference it needs. There is no more need to set any of it.



    Hope that wasn’t to freaky. Next time the fancy stuff will be back.^^



    greetz



    ceiphren





    @erlend_sh: Good idea. I’ll do that. But it will take a little bit to create some interesting material.



    @getsculptor: Alpha? uhm… I don’t know.^^ Eventually in early summer.
2 Likes

@ceiphren did you implement dependency injection yourself?

That’s one of the major features of Spring framework http://www.springsource.org/

I comes with a lot of other things that you may not need though. Also i don’t really know about the performance, i only use it for web apps.

But it can worth a look.

@nehon: yes, I wrote a classpath-scanner to find the annotated classes and a dependency injector to wire them together. The scanner has some bugs but basically it works (classpath-scanning is damn hard work.).



The core is a List which holds all components. In my version you can fill that list in 3 ways:

  • via classpath-scanning
  • via a config-file where you name all component-classes with a full qualified class-name
  • or adding instances of the classes per hand (which is handy for test-cases. You can create the component-context by hand and use only the components you need for this particular case)



    Also you can combine these methods.



    Yeah spring is not very fast but I think that’s not a problem, cause the dependency injection should run only once after the game-start.



    edit: to be honest it’s not really a classpath-scanner. It’s more a package-scanner who knows where to find .class.files.
@ceiphren said:
(classpath-scanning is damn hard work.).


Actually, it's provably impossible in the general case since class loaders are not required to dish up .class files as resources. Technically, class loaders are not even required to have .class files at all.

Still, it can work in specific use-cases like yours.

How does the scene composer know which model factory it gets? I mean, how does the application configuration potentially wire in different dependencies if it wants to? Because otherwise, dependency injection loses most of its benefits and you're really just doing good interface->implementation design practices.

Dependency injection and IOC (inversion of control) frameworks/containers allow wiring up the dependencies at runtime, ie: injecting them. Your scene composer thing could have one ModelFactory while some other service gets a different one based on the configuration.
@pspeed said:
Actually, it's provably impossible in the general case since class loaders are not required to dish up .class files as resources


Yeah, you're right. That's what I meant with bugs. It works only in some particular cases and it's the reason why I recommend spring. So it's not really a true classpath-scanner. I will correct that in the previous post.

Dependency injection and IOC (inversion of control) frameworks/containers allow wiring up the dependencies at runtime, ie: injecting them. Your scene composer thing could have one ModelFactory while some other service gets a different one based on the configuration.


Inversion of control wasn't what I wanted to achieve. I just wanted to get rid of setting the dependencies by hand. Currently it's possible that the SceneComposer awaits an interface ModelFactory and get's a specific implementation. But if you have different implementations the scene-composer should ask explicitly for the one it needs... in my case.

For greater projects I wouldn't use my solution.

I’m curious to know why you are doing that rather than using an “AppContext” solution which seems simpler?



i.e. AppContext {



public static ModelLoader getModelLoader();



}



Then you can just do AppContext.getModelLoader anywhere you need a model loader.



Unit testing etc can still be done, just set the model loader in the AppContext to the unit test one when starting up the unit tests.







The game looks good, one question I did have was whether you had considered wind? In sailing ships things like wind direction has a massive effect on tactics as it constrains movements. Ideally you’d want animations in the model for whichever direction you are sailing and transitions between them as well (i.e. raising and lowering sails etc).

@zarch: Because I’m damn lazy ^^ . It’s easier just to annotate the class as a component and write @needed where you need it.



Hm, wind would give the game a realistic touch. Currently it’s fully arcade-focused. I’ll keep that in mind.



Sail animations are definitly on display. Currently I don’t know how but they will come.

Just curious, did you consider Google Guice?

You can get the guice library without the AOP (method interception) for 470kb, 865kb with AOP.

Of course you now have your own framework, so perhaps it’s a moot suggestion, but for anyone else looking for dependency injection. :slight_smile:

@Tumaini: Nope, never heard of it. But it looks promissing.

On deck, pirates and monkeys!



After a hilarious saturday (started coding at 11 am, now it’s midnight… okay, I had a 3-hours break ^^ ) I just wanted to show you some improvements



http://youtu.be/iMXnt9Lq6Yw



Now some not-really-important-babbling:

Current framerate is at 200 at 1920*1080 and 310 on 1280*1024 (if I disable vsync). With all this stuff in the scenerie I must say: Wow. jme just rocks.

Also I want to say: quaternions are awesome. Making life waaay easier.

so long

ceiphren
5 Likes

Yay, haha great man! Looks like so much fun! :smiley:

Very nice!

This is one of my favorite games to keep tabs on. I always look forward to the updates… I have a real soft spot for tall ships.



I think if you ever give up on this game then a band of angry monkeys will show up at your house with torches and pitchforks. :slight_smile: It simply must be completed someday.

1 Like

Looking great!



You need to start having some fun with pirate music though, it’ll make your videos 2x better.

http://www.newgrounds.com/audio/search/title/pirate



I don’t know exactly what you’re going for yet, so here’s a selection of different themes:

http://www.newgrounds.com/audio/listen/374471

http://www.newgrounds.com/audio/listen/363967

http://www.newgrounds.com/audio/listen/1795

Thanks mates. this is the fuel that keeps the engine running :slight_smile:



@pspeed: Don’t bother. As I’m the captain of this ship, I will bring it to port or go down with it.



@erlend_sh: Thanks for this. Unfortunately this is not what I have in mind.



Currently I need at least 5 Tracks. One for the menu (something chilled with a touch of adventure-mood), 3 mission tracks which keeps you in a mood for conquest and glory and one hilarious epic boss-battle track. I found some good stuff on jamendo.org but it doesn’t fit together very well 'cause every song as another style and quality.



Something like this would be cool:

http://www.youtube.com/watch?v=qGyPuey-1Jw



also this sounds cool:

http://www.youtube.com/watch?v=GRgzQvIKynM



and this:

http://www.youtube.com/watch?v=8WWdOjxoQro&feature=results_video&playnext=1&list=PLA4156F2A10B65207



And of course the soundtrack of pirates of the caribbean





if anybody knows some cool music-source I would be gratefull.



btw. Newgrounds! NewgroundsNewgroundsNewgroundsNewgroundsNewgrounds! (yep, this is hidden advertisment XD )

Hm, I think you’ll have issues finding a completely orchestrated song or one with a complete choir without spending some money… Especially the Zimmer-style Muppet music, I don’t think you’ll find this in the “free” realm… I do these kinds of orchestrations for composers sometimes, for plays or as a demo before they record it with an orchestra. Its a lot of work making it sound like an orchestra using samplers etc, not even speaking about the composition (arrangement etc) itself…



On another note… be sure games like these is what fuels us :wink:

@normen: Yeah, I thought that would be a problem. Well I have a little budget for the music. Wouldn’t be enough for an orchester, that’s clear. But maybe enough for someone who can arrange it via software and samples. I have no idea what that would cost. Chorals and voices are not needed and it doesn’t has to be the quality of hans zimmer.



edit: wrote you a pm.

@ceiphren said:
Currently I need at least 5 Tracks. One for the menu (something chilled with a touch of adventure-mood), 3 mission tracks which keeps you in a mood for conquest and glory and one hilarious epic boss-battle track. I found some good stuff on jamendo.org but it doesn't fit together very well 'cause every song as another style and quality.

Actually, from my experience, composers are the easiest pro-bono collaborators to come by. A lot of talented composers are trying to break into the game industry, and they're offering their expertise for free to get a foot in the door. On all the hobbyist/student projects I've done, the one thing I've never had problem finding is a composer, and most of those projects have been much less impressive than what you have going on already.

1) Go on GameDev.net's "Help Wanted" and track down the "Offered" posts by composers. Contact them.

2) Post your project on GameDev.net's Classifieds section, looking for a composer

3) Set up a profile on IndieDB.org and do #1 and #2 for indiedb's recruiting board as well. Seriously, that's where I found my last composer, Tom Stoffel, and he's fantastic. Actually, if you like some of his samples I could ask him if he'd be interested in doing some compositions for you.
1 Like

Finally on IndieDB: http://www.indiedb.com/games/piratehell



Damn, what a weekend…

3 Likes

Yay, another awesome project linked up with jME on indiedb. Best of luck over there mate :wink: The fact that they don’t let you sync up their news section with an RSS feed is quite annoying, but boy is that profile worth keeping updated.