Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  mixins for my new Ruby-like language  (Read 837 times)
0 Members and 1 Guest are viewing this topic.
Offline JL235

JGO Coder


Medals: 10



« Posted 2011-01-13 07:21:44 »

I'm currently writing a Ruby-like language (it's less dynamic and offers some static analysis for better errors) for game development, and so currently I'm thinking about future features. Dispite what I have said in the past on this subject, in principal I like the idea of componetizing parts of your game objects into indevidual parts rather then through inheritance. An obvious thing to then add is Ruby mixins.

However I'd like to go further then this with more features to help with building component oriented systems. Namely the idea of being able to hook multiple pieces of behaviour together, like events, onto a game object to compose it. Here is the example I current have worked out. Lets say you have a Sprite class that is the base for everything in your game:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
class Sprite
    def new( level, img )
        @level = level
        @img = img
    end
   
    def update( delta )
        // do nothing
   end
   
    def draw()
        drawImage( @img, getX(), getY() )
    end
   
    // getX(), getY(), move(), setLocation(), getLevel(), etc
end

It holds onto the level, has a location and has an image drawn at it's location.

Now you want to create two new game enemies: a missle and an asteroid. Both hurt the player, one moves towards the player and the other wonders randomly around the screen. These could then be implemented as seperate modules.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
module RandomMove
    after update( delta )
        this.move( rand(-speed, speed)*delta, rand(-speed, speed)*delta )
    end
end

module HurtPlayer
    after update( delta )
        this.getLevel().isCollision( :player, this ) do |player|
            player.damage();
            this.remove();
        end
    end
end

module TowardPlayer
    after update( delta )
        this.moveTowards( this.getLevel().get( :player ), delta )
    end
end

Modules define methods, but can chose to be appended to existing ones using the after keyword (or before or around if they chose). To use them they are included to a class, just like in Ruby.

So creating a new Asteroid and Missile is now simple, you extend the Sprite class and add the appropriate components:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
ASTEROID_IMG = new Image( "asteroid.png" )
MISSILE_IMG  = new Image( "missile.png" )

class Asteroid < Sprite
    include RandomMove, HurtPlayer
    def new()
        super( ASTEROID_IMG )
    end
end

class Missile < Sprite
    include TowardPlayer, HurtPlayer
    def new()
        super( MISSILE_IMG )
    end
end


Obviously modules could also be used like normal Ruby mixing, like for drawing that image:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
module Drawable
    def setImage( img )
        @img = img
    end
   
    def draw()
        drawImage( @img, this.getX(), this.getY() )
    end
end

class Missile < Sprite
    include TowardPlayer, HurtPlayer, Drawable
   
    def new()
        setImage( MISSILE_IMG )
    end
end

However with my modules you could also append another draw object to draw a shadow before draw is called:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
module DrawShadow
    before draw()
        // draw shadow
   end
end

class Missile < Sprite
    include TowardPlayer, HurtPlayer, Drawable, DrawShadow
   
    def new()
        setImage( MISSILE_IMG )
    end
end


It essentially boils down to Ruby mixins + after, before and around. But do people think this would be a bit useful for a game oriented language, or very useful?

An alternative would be to implement something totally different as my language is Ruby-like, not Ruby (feel free to offer your suggestions). A while back I had originally settled on full aspect orientation, but now I'm wondering if that is just too wild and crazy (I never liked how they just magically join onto bits of code elsewhere in your program).

Please just discuss, I'm really looking for as much input as I can find to help me make the right decisions before implementing anything like this.

Edit: something else I've been thinking about is being able to create objects purely from modules, removing the need to even have a MissleSprite (or even a Sprite). But I have no idea what this would look like.

Offline JL235

JGO Coder


Medals: 10



« Reply #1 - Posted 2011-01-14 11:20:49 »

There are a lot of members here who are in favour of componetised game architectures, so I'm bumping this because I'd love to get some input on what people would like to see in a language to help make building componetised systems easier. Even if it's not directly in regards to my idea above.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (26 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (13 views)
2014-07-29 10:59:34

E.R. Fleming (34 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (44 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!