Page 6 of 10

Re: PÄSSION: object-oriented LÖVE

Posted: Mon Feb 15, 2010 9:00 am
by kikito
Last night I implemented the first version of stackable states on MiddleClass.

I'm sure it's buggy - haven't done proper testing yet. But if anyone feels adventurous... the new code is on the SVN.

I'll try to do some testing during this week.

I haven't given up on implementing some sort of observation pattern - just putted it on the backburner, the stackable states sounded more fun to program :)

By the way , I accept suggestions on this matter. I'm specially interested on the "registration" interface. This is what I have in mind right now. Please let me know what you guys think - function names, structure, etc.

Code: Select all

Player = passion.actor.subclass('Player')
function Player:startListening()
  self:listen('keypress', 'a', self.goLeft)
  self:listen('keypress', 'd', self.goRight)
  self:listen('mousepress', 'l', self.setTarget)
end
function Player:stopListening()
  self:ignore('keypress', 'a')
  self:ignore('keypress', 'd')
  self:ignore('mousepress', 'l')
end
function Player:goLeft()
  print('left')
end
function Player:goRight()
  print('right')
end
function Player:setTarget(x,y)
  print('My target is :' .. x .. ', ' .. y)
end
Oh and I forgot to answer to this one the other day:
By the way, have you thought about saving&loading, and sending over networks? You want this too to be simple and by storing userdata in the tables, this might not be possible.
I have thought a bit about object serialization, and networking (I consider them two different problems, by the way). But PÄSSION 1.0 will not have them, but they are not discarded from future versions. If they do, serialization will come first, since it seems an order of magnitude easier to implement than networking.

Re: PÄSSION: object-oriented LÖVE

Posted: Mon Feb 15, 2010 4:26 pm
by bartbes
kikito wrote: since it seems an order of magnitude easier to implement than networking.
<subliminal>LUBE</subliminal>

Re: PÄSSION: object-oriented LÖVE

Posted: Mon Feb 15, 2010 7:56 pm
by kikito
bartbes wrote:<subliminal>LUBE</subliminal>
:D Even with LUBE, it's still going to be an elephant, at least if I'm to implement it properly (preemtion, "local" and "remote" functions and states...)

What about the listen() and ignore() stuff? Do you think it could be simplified more than that, and still be used the same way?

Re: PÄSSION: object-oriented LÖVE

Posted: Tue Feb 16, 2010 1:09 am
by blenderer
Hi Kikito, and thanks for writing an amazing/helpful library. I having a problem with middleclass and I am not understanding why it is happening.

here is my code:

Code: Select all

require 'MiddleClass.lua'

function love.load()
	npc = class("npc")
	function npc:initialize(name)
		self.name = name
	end
	
	boss = class('boss', npc)
	function boss:initialize(name, hp)
		super.initialize(self, name)
		self.hp = hp
	end
	
	bowser = boss:new('Bowser', 799)
	deckard = npc:new('Deckard Cain')

end
 
function love.update(dt)

end
 
function love.draw()
	love.graphics.print("Boss name = " .. bowser.name, 400, 300)
	love.graphics.print("Boss level = " .. bowser.level, 400, 320)
	love.graphics.print("npc name = " .. deckard.name, 400, 340)
end
This is creating an error for me on:

Code: Select all

super.initialize(self, name)
Its probably something very elementary, so thanks in advance for the help.

Re: PÄSSION: object-oriented LÖVE

Posted: Tue Feb 16, 2010 8:10 am
by kikito
Hi blenderer,

Just so you know, this forum is for PÄSSION-related stuff. (PÄSSION is a different library built on top of MiddleClass). There's a different post for MiddleClass itself. It would have been "more organized" if you had posted your question there.

I've looked at your code and on a first glance it looks ok (well, you are not respecting the naming conventions ;) but it should work anyway).

Could you tell us the error message you are getting?

Re: PÄSSION: object-oriented LÖVE

Posted: Thu Feb 25, 2010 3:51 am
by Deecodeuh
Oh man.. :( Looking at this source code.. I can't make sense of any of it. I really need help learning all of this. Where do I start? What do I have to read?

Re: PÄSSION: object-oriented LÖVE

Posted: Thu Feb 25, 2010 8:15 am
by kikito
Hi Deecodeuh,

I'll tell you what you can "ignore" first.

It is quite probable that you are not interested in implementing your own way to do classes on Lua, you can safely ignore these two:
  • passion/MiddleClass.lua
  • passion/MindState.lua
It is also quite possible that you don't want to implement your own GUI (windows, buttons), so it is safe for you to ignore everything on the UI directory:
  • passion/gui/*
This will leave a bunch of stuff out of the way.

The graphics directory is where I put drawing-related functions that I find useful - for example, rounded corner rectangles and automatic drawing of shapes. I might add new stuff there today (for creating/drawing quads). For the files on these directories, you should just learn the function names and their parameters; you don't need to browse their code.
  • passion/graphics/*
This leaves you with passion/Core.lua and passion/actors/*. These are the most critical parts in PÄSSION. Again, you should be just fine if you are able to read the method names and parameters; you only need to look at the method code if you are really curious.

Warning - some of the stuff in passion.Core and in actors/* is still changing. Notably, I still haven't made "invisible" and "frozen" actor states, and I have to change the way callbacks (keypress, mousedown, etc) hare handled in passion.Core.

I hope this helps you. If you have specific questions, ask away!

Re: PÄSSION: object-oriented LÖVE

Posted: Thu Feb 25, 2010 2:59 pm
by Deecodeuh
Thank you for your help. :) Late last night, I noticed the wiki entry for MiddleClass, and that helped a lot.

Re: PÄSSION: object-oriented LÖVE

Posted: Thu Feb 25, 2010 4:29 pm
by kikito
Deecodeuh wrote:Thank you for your help. :) Late last night, I noticed the wiki entry for MiddleClass, and that helped a lot.
There's one for MiddleClass and another one for MindState.

Making a wiki entry for PÄSSION is also on my TO-DO list. But I've got to clear up some areas first, on that same list :)

Re: PÄSSION: object-oriented LÖVE

Posted: Wed Mar 24, 2010 11:55 pm
by kikito
PÄSSION 0.6.2rc1 is here!

It has been some time since I did not update this thread. In the meantime, development has continued. You can blame the lack of forum updates on work, holidays, and then this guy and this other guy.

Last time Osuf suggested two improvements: stackable states and an observer pattern. Both are done now.
  • Regarding stackable states, I've even made some documentation about them.
  • I've implemented a little messaging system called Beholder (Observer was too boring). Quick sneak peek:

Code: Select all

MyActor = class('MyActor', passion.Actor)
function MyActor:initialize()
  super.initialize(self)
  self:observe('keypressed_up',    'goUp')
  self:observe('keypressed_down', function(obj) obj.y = obj.y + 1 end)
end
function MyActor:goUp()
  self.y = self.y - 1
end
function MyActor:destroy()
  self:stopObserving('keypressed_up')
  self:stopObserving('keypressed_down')
end
Notice that it is possible to specify either a function or a method name for the callback. The callbacks admit parameters (mousepressed_l will give you the x and y coords, for example)

'keypressed_up' and 'keypressed_down' are events triggered by PÄSSION, but it is possible to trigger them from code by invoking:

Code: Select all

Beholder.trigger('keypressed_up')
It is also possible to create custom messages - for example, on a multiplayer game, one could create an event called 'player_won', passing the winning player as a parameter:

Code: Select all

Player = class('Player') -- Player is a regular class (not an actor)
Player:implements(Beholder) -- This includes "observe" and "stopObserving" on regular classes. passion.Actor includes it by default.
function Player:initialize(name)
  super.initialize(self)
  self.name = name
  self:observe('player_won', 'someoneWon')
end

function Player:someoneWon(someone)
  if(someone==self) then
    print(self.name .. ' : yahooo!')
  else
    print(self.name .. ': buaaaah!')
  end
end

frank = Player:new('frank')
peter = Player:new('peter')
ann = Player:new('ann')

Beholder.trigger('player_won', ann) -- ann will smile, peter and frank will cry
In additon to these changes, I've:
  • Removed the hasBody / hasImage stuff. There's a proper subclass now, called ActorWithBody, that handles actors with bodies. HasImage was too trivial, so I've just scrapped it.
  • I've chanded all passion:methods to passion.methods, so it looks more like love now.
  • I've added utility functions here and there. passion:dumpTable(t) will output the contents of a table on the console. There's also a function for drawing shapes (passion.graphics.drawShape) that it is used by ActorWithBody:drawShapes, a very useful debug function. And there are others, mostly on passion_util.lua.
  • I've re-organized the code in modules: audio, fonts, graphics, gui, oop and timer. Hopefully this will make the library easier to understand on a first glance.
  • By the way, timer functions are the other big addition. Check this out:

Code: Select all

MyActor = class('MyActor', passion.Actor)
function MyActor:initialize()
  super.initialize(self)
  self:after(3, function(obj) print('This will be printed 3 seconds after the object creation') end)
  self:every(4, 'shout', 'wooho!') -- prints 'Periodic call - wooho!' every 4 seconds
  self:every(5, 'shout', 'yeehoo!') -- prints 'Periodic call - yeehoo!' every 5 seconds
end
function MyActor:shout(phrase)
  print('Periodic call - ' .. phrase)
end
The 'after' and 'every' functions allow you to invoke methods (by name) or functions, with optional parameters.

Finally, I've updated all the samples on this thread to the latest version of PÄSSION. I've replaced the old versions on their respective entries, but for comfort I'm also attaching them here (except the piano one, which is too big - you might find it here).

What comes next?
  • Well, I certainly want to include more gui elements.
  • I'm also planning to make an 'interpolator', based on the timer implementation that I've got now. This will allow the creation of "fade in/fade out" effects (like the ones Jasoko and others alked about not long ago) easily, not only on images, but on every lua object (i.e. sounds). I'll be "inspiring myself" on jquery's animate function.
  • I'll try to create a passion.audio.play method that handles the duplication the source automatically so we can hear the same sound more than once.
  • I still have to delucidate a way to handle the new camera-like functions of love properly.
  • And, documentation :). I keep delaying this one, but sooner or later I will have to start writing tutorials about PÄSSION.
  • I'm making a space game that uses all the new stuff. I've got a ship that shoots stuff, with different cannons. I'll publish it once I get it done to the "Asteroids" level.
Sorry for the long post. Comments / Questions are welcomed - and encouraged!