• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Development] Earth Defend (web game)

Status
Not open for further replies.
Level 11
Joined
Jul 15, 2004
Messages
333
Hello there,

in winter this year I started this small web-based game in javascript. Without much of beforehand idea it turned out to this. It's about an one man, known as Astronaut, defending whole Earth with various weapons against asteroids and various alien lifeforms. Main core of it took me whole two weeks but in some freetime I eventually was able to form it closer to my vision. It's still far from being labeled as finished project but it is playable at this point.



You will need the newest Chrome, best played with full hd resolution (use fullscreen). It's capped on 60 fps.

Play the game here: http://ed.baranok.eu/



If you want to test all weapons tap key "T". Or just check console.

Enjoy and don't hesitate to leave a comment.



*
 
Last edited:
Level 29
Joined
Jul 29, 2007
Messages
5,174
Looks decent for a prototype, I only find it ridiculous that you don't properly listen to simple events such as mouse clicks in a proper standard way, such that it will work in all decent browsers, though the source is kind of jumbled, so I can't even find where the event is registered.

For starters, use EventTarget.addEventListener instead of the 10 years obsolete function properties, and don't get information from w3schools...(hint: it's a terrible site)

I don't quite understand why you are using timeouts all over the place. Especially in places that are called pretty much right away (e.g. Main, what's even the point of the timeout there, just have it run in global scope, or let it be a self-executing function).

Beyond that, the code is a bit too messy, and depends on too libraries, to really look around.
 
Last edited:
Level 11
Joined
Jul 15, 2004
Messages
333
That explains why it didn't listened mouse with Apple products. I was learning on the run that explains the mess I guess. Never really wanted to refactor the code until I would be convinced it's good enough.

When I tried to run too many funcs at once it kinda never got to the end, I guessed there is some limiter like in WC3 and timeouts fixed it.

Anyway I never really guessed somebody would look at the code (tho' I would do the same). I appreciate the new perspective you gave me upon this. Also thanks for feedback.

Have a Good day.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
There are no limiters of any kind, and there isn't really a point in having main-flow code run with timeouts.
Now if you want to organize code better and not have everything in global scope, self-executing functions kind of achieve the job.
If it hang, then there is an issue with the code.

As to refactoring, that's perfectly sensible, I am all for making bad but working code, and later on rewriting it (ala code iterations), especially when learning a new language.
One of the more noticeable things to me is the lack of prototypes. E.g. for the different AI types, where you can have a generic attack method for every type, instead of a large switch, and just having functions inside their scopes, for example - createAstronaut could as well be the constructor of your player, and the rest of the functions be its methods.
JavaScript:
function Player(...) {
    // initialize all the stuff you put in the Player variable
    // then do the stuff in createAstronaut
}

Player.prototype = {
    destroy: function () {
        // destroyAstronaut
    },

    animate: function () {
        // animateAstronaut
    },

    load: function () {
        // loadPlayer
    },

    ... // and so on
};

// Example of usage
var myPlayer = new Player();
myPlayer.animate();
Right now your code is very similar to procedural code, and having prototypes and methods tends to result in neater and more readable code (I am tempted to write OOP, however I scorn that overused and overemphasized term).

Generally speaking though, try to stick to standard ECMAScript 5 features, instead of mixing all sorts of really old and obsolete things. In this regard, w3schools is a terrible site. MDN, and searching for stuff on stackoverflow, is much more preferable.
 
Status
Not open for further replies.
Top