Recent posts by Drakim on Kongregate

Subscribe to Recent posts by Drakim on Kongregate

avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Tile Based Game Idea

This site has some neat ideas you could look at.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Kong API with MySQL?

SMB is also plagued by the ingame physics being dependent on the game running at a certain rate, so if you lag too much everything starts glitching out. He seems like a pretty bad programmer, to be honest.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Kong API with MySQL?

You are problaby gonna need some server side scripting like PHP to help communicate between Unity3D and MySQL. If Unity3D was to communicate directly with MySQL, it would require the MySQL password to be embedded in the Unity3D app that gets given out to all your players, which is a very very bad idea.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

I pretty much only use blitting, so I have little experience. Couldn’t you possibly make some sort of MovieClipHolder component?

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Thanks for the kind words.

My hidden secret is that I have both :3

Though clever use of Haxe macros and abstracts, my entire .swf is almost nothing but one big Bytearray instance, powered by the specical Alchemy opcodes for read/write, and inlined function calls that disappear at compile time. I’ve implemented close to everything in this, classes, graphics, sound, the component system, etc. The only thing I cheat with is a single Bitmap to act like the screen, since Flash requires it.

If you were to decompile the .swf, you would find nothing but Ints being pushed around a few million times, with no function calls and no class instances being made. It runs like a dream. And due to the power of Haxe, my actual code still retains the visual appearance of regular classes and functions, so it’s easy to manage and edit.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

Then way I do it (and you don’t necessarily have to mimic me) is by separating it into 3 components, Position, Velocity and Player.

Position is just .x and .y

Velocity has .xSpeed and .ySpeed and listens for ‘update’ on the entity. In the callback it adds it’s two speed properties to the position component’s x and y.

The Player component also listens for ‘update’ on the entity, and checks for keypresses. When a button is pressed, it adjusts the Velocity’s speed values to give movement in the direction that is being pressed.

Obviously this on it’s own will not do anything since nothing is triggering the ‘update’ event on the entity. You need a world class of some sort (regular class or entity, either is fine) that can hold all ingame object entities in a list. The player entity needs to be added to this list, and every frame the world class should loop over the list and call ‘update’ on each entity.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

Hmmm, I don’t think I would personally have a separate event for every frame that your player is moving. Rather I would recommend you have a ‘update’ component on your player entity that gets invoked every frame, that checks for these keypresses.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Most remarkably, everything can be regarded as being globally accessible

This is considered an extremely bad thing. The restrictions you have from accessing anything isn’t there because of computer limitations. It’s an artificial fake limitation imposed upon you by the programming language because accessing everything globally is bad in 99.99% of all cases (including games).

Likewise, you’ll never have to make a function call simply to get a value.

If you are worried about performance in regards to things like this, you should check out Haxe, which can inline it’s function calls (meaning the function is never actually called, the code runs directly).

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

Depends on the game, I guess. If you use components to apply and remove buffs and debuffs, being able to remove them is vital (unless the components are gonna stay “sleeping” on the target forever)

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Well, it’s not strikingly wrong in terms of being impossible to use, but I imagine it becomes harder and harder to maintain as your game grows larger.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

Yeah, sorry for the unspecific method name.

While that works with an int index, it has to iterate and compare until it finds the correct event.

With a reference to a cell of a LinkedList, the removal function can be extremely lightweight at 2-3 lines, just switching some references around.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

oh, I see, it removes all events associated with a keyword/eventCode. Careful though, that could potentially break unrelated code that doesn’t expect to have it’s event attachments removed without warning.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

That will work, but with the downside that will make removing events more expensive, as you either leave holes in your array, or you have to call the expensive splice function. If you have a game that removes and adds events frequently it might be a net loss.

One could perhaps merge the EventCBD instance and the LinkedList’s Cell instance into one and the same to reduce the number of objects. Each event object would point to the next one like a list.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

The only annoying thing is that getComponent() returns IComponent rather than the component class, so you have to cast components every time you retrieve them. But I think that’s unavoidable in AS3. With Haxe you can make the function return the type that you asked for.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Obviously stuff like this depends on the game, but I had a lot of success using a World Entity that represents the physical world. It has a component that lists all the ingame object entities (which gets ‘update’ and ‘draw’ called by it regularly, unless they are too far from the player focus).

One advantage of this is that the World Entity would listen for an event called ‘invokeEvent’ where it would dispatchEvent on all the ingame entities that fits a certain criteria. It is perfect for explosions and such.


WorldEntity.dispatchEvent('invokeEvent',
  {
  range: new Rectangle(x,y,w,h),
  event: 'Explode',
  eventData: {damage:5, push:30}
  }
);

Using some Haxe macros and abstracts I could eliminate most of the performance loss associated with this level of abstraction. People on AS3 can possibly use Apparat for similar optimizations.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

I would instead recommend that you make a component that is capable of holding entities. That way your master entity uses this component to hold all the child entities you plan to add to it.

If you hardcode “holding entities” into the entity class itself, you can’t change the method of storing the entities when another situation comes up. Maybe you used an Array and now need a List, or maybe you need a spacial indexing technique to lessen overhead.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Nothing should technically prevent you from having one component hold another component. Although I don’t think there is much to gain from that, the second component could just be a regular class, since you aren’t using any component behavior.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Logical OR help

You are right, typo on my part

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Logical OR help

In plain English that code pretty much states:

if xPosition is less than zero, or xPosition is greater than 100, then run this code.

so -50 would be good, and 150 would be good, but 75 would not fit in. It seems to be doing the opposite of what you would want, so your confusion is understandable.

Btw, from what I see it is not inclusive. If xPosition is 0, the code would run like this:

if( 0 < 0 || 0 > 100 ){
//code
}

Which wouldn’t work. 0 is clearly not less than 0, and 0 is clearly not greater than 100.

If you wanted them to be inclusive, you have to use =< and => instead of just < and >. In plain English they basically mean “equal or lesser than” and “equal or greater than”.

So, guessing what you actually want, it would be something like this:

if( xPosition => 0 && xPosition =< 100 ){
//this code executes if one of either xPosition is between 0 and 100, inclusive
}

Note that I flipped the > marks and used AND (&&) instead of OR (||)

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

When I was first developing my Component framework, I had an additional name parameter to the addComponent function, so that you could add more than one Component of the same class under different names. You would specify this name with getComponent to retrieve the component back out.

I decided against this though, because it is another hardcoded string that all the code in your project needs to agree upon, is the blue weapon cooldown stored under ‘laser_cooldown_1’, or under ‘laser_cooldown_2’?

Instead, I opted for the per-Class basis, because it forces me to think smart about how I design the components.

I happened to run into the exact same problem that you had in regards to cooldown and repeating actions when I first started out. Since I could only have one component per Class, I couldn’t have a generic CooldownComponent. I solved it by having a CooldownManager instead, that would handle an arbitrary amount of cooldowns on an entity (and I suspect that a similar solution for Repeating things could work).

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Yes of course. Components is not supposed to be a grand philosophical construction pattern that you need to follow air-tightly, it’s a practical construct for practical things, and it blends well with other systems. It’s just a standardized way of doing hooks and events for your composition so different classes can play together nicely.

I suspect a lot of people would want to include the basic ingame object properties on the Entity class (such as .x and .y) because abstracting it out wouldn’t gain them that much. You’d lose some modularity the more you add though.

As for your specific problem, maybe instead of going the route of sending ‘draw’ events to the entities, maybe the graphically related components would manage themselves behind the scenes. I’ve been looking into that myself because I want to handle all the drawing on a second thread (to utilize more CPU cores).

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Are the two balls separate entities? I would recommend implementing your own z-ordered list of entities to be drawn, rather than relying on the priority system, which is a bit fixed and hard to bend.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 4): Examples and advanced techniques

Deeper topic – Components and Events

Part 1

Part 3

Part 4

Examples and advanced techniques

Now that we have a basic idea about what components are and how they work together with events, let’s talk about some examples and more advanced techniques. For this post, I will be posting more plain English description of the components rather than actual component code. Just ask if you are unsure of how something would be implemented.

Let’s add some more functionality to our Entity class:

class Entity {
  public function attachComponent(component:Component):Bool
  public function getComponent(type:Class):Component
  public function dispatchEvent(keyword:String, data:Object = null):Object
  public function attachEvent(keyword:String, callback:Function, priority:Int = 0):Event

  public function breakEvent():Void
  public function removeComponent(type:Class):Bool
  public function detachEvent(e:Event):Bool
}

removeComponent() and detachEvent() should be fairly self-explanatory. The only gotcha here is that our attachEvent() function has been appended to return an Event instance, soley used for the purpose of identification so that we can detach it later if we want to.

breakEvent() is a fairly simple function with a lot of power, and you can invoke it at any time inside your components as they are reacting to a dispatchEvent. Calling it prevents any other components that were listening for this event from ever getting their turn. Of course, it only works for components with specified a lower priority value, as the ones with a higher priority value have already executed.

In short, with breakEvent() a component can stop other components from hearing an event. It only stops the single current ongoing event as breakEvent() is called, if another event is dispatched afterwards it will work normally.

This can be used when we wish to invoke special rules that should override the regular flow of things. A prime example is the Invincibility Star that Mario can acquire. When he is under it’s affect he should not die when an enemy inflicts harm upon him.

A star power component could be designed like this:

Starpower Component
new Starpower(target) – add component to target, and listen for all events mentioned below.

Events
‘damage’: Priority 100, breakEvent() to prevent the damage from ever being picked up by any other components on Mario.

‘collide’: Priority 10, dispatchEvent() with ‘death’ to whatever we are colliding with, because nothing survives Mario with starpower

‘update’: Spawn some glittery star effects around Mario every frame, and check if it’s time to end the startpower buff. When it is time, dispatchEvent ‘starpowerExpire’

‘starpowerExpire’: removeComponent() the StarPower component, and detachEvent() all the events, so that there is no trace that Mario ever had this component.

This is a well designed component, because it is portable and can be entirely contained to it’s own file. Nothing in our Mario code would imply the existance of this powerup, which shows how easy it would be in the future to create more powerups, without having to do any retroactive adjustments on Mario.

Now, let’s make something deadly. A poison debuff that inflicts damage every frame to an enemy, and also spreads to other enemies that touches the poisoned victim.

SpreadingPoison Component
new SpreadingPoison(target) – add component to target, and listen for all the events mentioned below.

Events
‘update’: inflict damage, and check if it is time for the poison to expire.

‘collide’: check if the target we are colliding with is an ally, and if so, apply the poison to it too.

‘draw’: Priority -1, draw some green poison bubbles on top of the target to show that it is poisoned.

As you can see, we can even use events for graphical stuff. By using a priority of -1, we are telling the entity that we want the green poison bubbles drawn after (and thus on top) the enemy sprite, which has a priority of 0 for the ‘draw’ keyword.

Now let’s implement the famous Protoss shield from Starcraft

ProtossShield Component
new ProtossShield(target, 500) – you know the drill by now

Events
‘damage’: Priority 5, subtract the damage amount from the shield. If the shield is greater than the damage amount, subtract the damage amount from the shield and call breakEvent(). If the damage amount is greater than the shield, subtract the shield from the damage amount, set shield to 0, and let the ‘damage’ event resume.

‘update’: Regenerate 1 shield point

If the unit has so much shield that the damage is entirely absorbed, the ‘damage’ event is stopped entirely. This means that if you have a component for splashing some blood, or playing a “ugh!” sound, it’s not gonna trigger unless the shield runs out.

But enough gameplay components, let’s look at some general purpose components also.

Repeater Component
new Repeater(keyword,frequency) – This component dispatches the specified event to it’s parent entity as often as the frequency given.

new Repeater(‘jump’,100) would trigger the ‘jump’ event on the enemy every 100th frame. As long as there is some other movement component listening for this ‘jump’ event and acting upon it, you would have the enemy jump in intervals.

Reaction Component
new Reaction(oldKeyword, newKeyword) – This component dispatches newKeyword when it hears the oldKeyword.

new Reaction(‘damage’,‘trigger’) would cause any damage to trigger the a bomb’s countdown mechanism, making it behave in a volatile manner. The Reaction mechanism is great for bridging two pieces of functionality without having to write a completely new component.

Delay Component
new Delay(keyword, time)

Fairly self-explanatory. Works almost like a task scheduler, helping us quickly set up actions to happen in the future. For instance, we could simply have our Starpower component with a new Delay(‘starpowerExpire’,5000) so that we don’t need to keep track of the time ourselves in the Starpower component.

These general purpose components can help us with rapid prototyping, and just saving keystrokes in general, so that we can create more content faster. A lot of them can even outperform customized components at scale with some clever mass optimizations.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 3): Events to the rescue!

The thing is that entities are not ingame objects 1:1. While you often have one entity per ingame object, this is just the norm, not the rule.

For instance, if I were to build Pacman I would make a single Entity to represent all the dots. It would need a specialized Position component that could hold the position values of all the dots (probably using a vector). And then a specialized Image component that would draw all the dots instead of just one. Same for a bullet hell game’s bullet.

Just like how in a tile engine you don’t treat each tile as an individual unique construct that needs it’s own instance.

 
avatar for Drakim Drakim 1135 posts
Flag Post

Topic: Game Programming / Deeper Topic (part 2): Composition over inheritance

That could definitely work, and it would also allow you to have circular references among the components, which is otherwise pretty tricky to set up.

Blocking the addition of any later components is not a good idea, often external causes might want to inflict a component temporarily on an entity in the middle of the gameplay. It shouldn’t be too tricky to allow though, there just needs to be some separation between components that have already been initialized and not.

If performance becomes a concern for something like a bullet hell game, I would advice having a single entity be all the bullets at once. It would need a specialized Position component that allows for an arbitrary amount of .x and .y values, and so on.