Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6250235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:23:10+00:00 2026-05-24T13:23:10+00:00

When dealing with multiple classes in a Javascript/NodeJS game, I’m having trouble figuring out

  • 0

When dealing with multiple classes in a Javascript/NodeJS game, I’m having trouble figuring out which class should emit events and which classes should listen for the. I am following this guide to creating event-driven games: http://pragprog.com/magazines/2011-08/decouple-your-apps-with-eventdriven-coffeescript

I’m writing a small game and have split my classes into the following controllers:

world – creates the game world and progresses through a number of ‘turns’ to determine some simple game logic (i.e. a character should move, a tower should shoot).

tower – a tower sits on a 10×10 grid and has a range. When an object comes into range, it can shoot.

mobs (enemies) – a mob spawns on the 10×10 grid and moves every 3 seconds. At some point, it wanders in range of a tower.

I have been reading about EventEmitters all day but can’t seem to figure out the right way to architect my events. Should the mobs fire an event when they move, and the tower listen for a ‘move’ event? Or should the world control all of the events and the tower/mobs listen to the world?

See below for example code.

Background: I’ve been working on a simple TowerD game for NodeJS and decided to implement the server first. I’m storing all entities in MongoDB and using geospatial calculation to determine if objects are in range to shoot. Currently i’m using a rudimentary 3 second loop to ‘tick’ the game and progress logic but I’d like to move to a truly event-driven model and am struggling.

World:

exports.World = class World extends EventEmitter

  constructor: ->
    ### Initial config ###
    @gameTime = 3000  # every 3000ms, the game progresses

    ### Start the game!! ###
    @game = setInterval ->
      world.gameLoop()
    , @gameTime

    ### Load the map ###
    # First level: Hidden Valley
    @maps = []
    @maps.push new map 'hiddenvalley'

### Load the mobs ###
    # First map has one mob: Warrior
    @mobs = []

    # Let's create two of them  
    @mobs.push new mob @maps[0].mobs[0]
    @mobs.push new mob @maps[0].mobs[0]

(see full world.coffee: https://github.com/bdickason/node-towerd/blob/master/controllers/world.coffee)

Tower:

exports.Tower = class Tower
  constructor: (name) ->
    name = name.toLowerCase() # In case someone throws in some weird name

  # Check for anything within range
  checkTargets: (callback) ->
    mobModel.find { loc : { $near : @loc , $maxDistance : @range } }, (err, hits) -> 
      if err
        console.log 'Error: ' + err
      else
        callback hits

(see full towers.coffee: https://github.com/bdickason/node-towerd/blob/master/controllers/towers.coffee)

Mobs:

exports.Mob = class Mob extends EventEmitter

  move: (X, Y, callback) ->
    @loc = [@loc[0] + X, @loc[1] + Y]
    newloc = @loc
    mobModel.find { uid: @uid }, (err, mob) ->
      if(err)
        console.log 'Error finding mob: {@uid} ' + err
      else
        mob[0].loc = newloc
        mob[0].save (err) ->
          if (err)
            console.log 'Error saving mob: {@uid} ' + err
    console.log 'MOB ' + @uid + ' [' + @id + '] moved to (' + @loc[0] + ',' + @loc[1] + ')'

(see full source of mobs.coffee: https://github.com/bdickason/node-towerd/blob/master/controllers/mobs.coffee)

Full source of project: https://github.com/bdickason/node-towerd

Any event help would be appreciated. I’ve poured over about 15 nodejs games on github and haven’t found anyone using this pattern yet 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:23:11+00:00Added an answer on May 24, 2026 at 1:23 pm

    I’d use a custom EventEmitter that supports “parents”, e.g. bubbling event propagation. Then I would do it the following way:

    • When a mob moves, it fires an “move” event on itself with a parameter like {mob: @,x: x, y: y} (the event then bubbles up to the world).
    • The world listens for “move” events. When it receives one, it checks in the database to look up the towers that need to be notified and emits the event on them.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am dealing with a large codebase that has a lot of classes and
I'm dealing with game dialogue files (conversation between player and non-playable-characters) where dialogue choices
I'm trying to figure out the best way to deal with writing multiple GUI
Is it safe to call errno multiple times when dealing with the same error.
I'm trying to extend ScriptManager to simplify dealing with resources that have multiple resource
I'm dealing with a null pointer exception for which I have a stack trace.
I am currently in the process of developing a Web Service which should expose
I was wondering how people handle dealing with references across multiple users across multiple
Dealing with an employee that went over my head
When dealing with small projects, what do you feel is the break even point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.