Git Product home page Git Product logo

crafty's Introduction

Crafty JS Travis Build Status AppVeyor Build Status Sauce Test Status

Crafty is a JavaScript game library that can help you create games in a structured way…

Key Features:

  • Entities & Components - A clean and decoupled way to organize game elements. No inheritance needed!
  • Eventbinding - Event system for custom events that can be triggered whenever, whatever and bound just as easily.
  • No dom manipulation or custom drawing routines required.

Other Goodies:

  • Thriving community - Help is readily available in the forum.
  • Community modules - A growing collection of user-generated code you can use.
  • Pure JavaScript - No magic. Works in all major browsers and can be combined with your favorite js library.

Using Crafty

A simple game of pong:

Crafty.init(600, 300);
Crafty.background('rgb(127,127,127)');

//Paddles
Crafty.e("Paddle, 2D, DOM, Color, Multiway")
    .color('rgb(255,0,0)')
    .attr({ x: 20, y: 100, w: 10, h: 100 })
    .multiway(200, { W: -90, S: 90 });
Crafty.e("Paddle, 2D, DOM, Color, Multiway")
    .color('rgb(0,255,0)')
    .attr({ x: 580, y: 100, w: 10, h: 100 })
    .multiway(200, { UP_ARROW: -90, DOWN_ARROW: 90 });

//Ball
Crafty.e("2D, DOM, Color, Collision")
    .color('rgb(0,0,255)')
    .attr({ x: 300, y: 150, w: 10, h: 10,
            dX: Crafty.math.randomInt(2, 5),
            dY: Crafty.math.randomInt(2, 5) })
    .bind('UpdateFrame', function () {
        //hit floor or roof
        if (this.y <= 0 || this.y >= 290)
            this.dY *= -1;

        // hit left or right boundary
        if (this.x > 600) {
            this.x = 300;
            Crafty("LeftPoints").each(function () {
                this.text(++this.points + " Points") });
        }
        if (this.x < 10) {
            this.x = 300;
            Crafty("RightPoints").each(function () {
                this.text(++this.points + " Points") });
        }

        this.x += this.dX;
        this.y += this.dY;
    })
    .onHit('Paddle', function () {
        this.dX *= -1;
    });

//Score boards
Crafty.e("LeftPoints, DOM, 2D, Text")
    .attr({ x: 20, y: 20, w: 100, h: 20, points: 0 })
    .text("0 Points");
Crafty.e("RightPoints, DOM, 2D, Text")
    .attr({ x: 515, y: 20, w: 100, h: 20, points: 0 })
    .text("0 Points");

Left paddle is controlled by W & S, right paddle by UpArrow & DownArrow.
Check it out online and try to modify it yourself here.

Developing

If you want to fix a bug, please submit a pull request against the development branch. Some guides to help you can be found on the wiki

If you would like to make larger contributions please catch us in the forum and we will help you get started. Much appreciated :-)

Quick build instructions

The easiest way to build crafty is to use gruntjs, which requires node and npm. If you have grunt, node, and npm already installed, then run npm install from Crafty's root directory. (This will pull down about 30MB of node packages.) From then on, just run grunt to build.

You can also use yarn instead of npm.

(Full instructions here.)

crafty's People

Contributors

00dani avatar ahilles107 avatar airza avatar corellian avatar donaldpipowitch avatar dubrowgn avatar ericathegreat avatar grozen avatar harrislapiroff avatar heniotierra avatar hugeen avatar jamespic avatar jayvan avatar jhocking avatar jmwohl avatar kevinsimper avatar louisstow avatar matthijsgroen avatar maxbareiss avatar maxencefrenette avatar mkucko avatar mpetrovic avatar mucaho avatar nyanleek avatar pawurb avatar pengyu avatar potomak avatar sbyrnes321 avatar sorenbs avatar starwed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

crafty's Issues

Crafty.background only adds to the DOM

The function Crafty.background only adds a background to the DOM stage, it doesn't add a background to the canvas stage if you use that. Somehow Crafty should detect whether there is a canvas or not an add the background to that.

Variable Frame Rate

Make it as fast as possible. Might need to use another tick so that the speeds remain the same.

Private member variables

All component variables with an underscore in front should hide itself from the user so not directly accessible. This would also benefit from the change event firing when using attr() or other setters.

Have more Audio instances

Currently there is an instance for each audio file and this is due to browsers not handling many instances of Audio objects very well. Crafty should therefore create as many instances as needed so that the same sound can play at the same time.

HTML5 Audio issues

Pull requested.

In reference to this: http://stackoverflow.com/questions/4435009/html5-audio-plays-randomly

I know that short audio clips ( less than about 0.5 second ) have trouble playing in tags. Unfortunately, the some browsers deal with this better than others. My suggestion is this:

Always use sounds bytes that are longer than that length by padding a short clip at the end with silence. So a 0.1s shoot file, become 0.1s shoot + 0.5s of silence. It should work. I added a shoot.ogg that is a little longer than original.

Additionally, if we use only one instance of an audio clip per sound effect, we would have to rewind to the beginning of the sound and play it again if we want to press twice fast.
Triggering audio.play() while a sound is currently playing will not rewind+play automatically.

There are other approaches, but this one at least makes the elevator demo handle "shoot" better. Before my patch, the audio would not be handled well by Chrome 8,9,10 on OSX or
Linux. FF3.6 would also skip sometimes.

Demo : http://loccaro.com/elevatoraction/index.html

TODO: Investigate creating multiple sounds from one sound effect and play all simultaneously.

Detect browser ext

Detect which browser and which prefix to use (o, moz, webkit).

Rather than setting lots of unnecessary properties

Add support for Object.defineProperty()

IE9 supports Object.defineProperty(), but not defineGetter and defineSetter. Support for Object.defineProperty() should be added so that IE9 doesn't need to fall back to the enterframe event.

IE8 also supports Object.defineProperty(), but only specifically for DOM Objects, unfortunately. A try/catch around the call can provide a way to make it into the fallback case when running in IE8.

Mobile devices

Haven't been able to test Crafty on mobile devices but some components to accept mobile inputs would be handy.

Sort by Z then ID

If entities have the same Z, it arbitrarily decided which goes first. Need to sort by Z and then GUID. Needs to be quick too!

Call for direction

Since time is of the essence, how can we help?

Checked out the code. I'm studying it now. Can you map out a small goal roadmap for Crafty.
Maybe we can help with the site a bit. I can jump on a couple of the links that are #comingsoon.

Let us know. Good job so far mate.

Keyboard keys get stuck.

Crafty relies on the onkeyup call. If I switch tabs while using the arrows/whatever keys, the player object continues to move until i press&depress the same key again.

It might be better to instead attach a onkeypress to the document.documentElement (not window for crossbrowser compat)

Draw less often

Currently Crafty will draw something when ever the change event is called. This means

ent.x += 5;
ent.y += 5;

Will draw the entity twice. One solution is to just set a boolean flag to true on the change event and only draw it on enterframe if flag is true.

Inconsistent use of Math.floor() breaks canvas, mouse

Canvas drawing (at least in Firefox 3.6; it seemed to work without issue in Chrome 8) and the bounds defined by areaMap can become wildly offset from where they are expected if an object repeatedly moves to new x,y positions that are not integers. I'm doing programmatic animation over time that frequently results in sprite positions being set to non-integer values.

The cause seems to be from comparisons between the modified values returned by pos() and other values that remain unmodified.

A fix that seems to work for me is to move the Math.floor() calls into the setters:

this.defineSetter('x', function(v) { this._attr('_x', Math.floor(v)); });

That may create other issues, though. If at all possible, I think that it would be best to allow non-integer values everywhere, but I'm not experienced enough with canvas to know if this is possible cross-browser.

Better Collision

Now that rotation is implemented, simple rectangle collision isn't that great. More advanced collision is needed. Separating Axis Theorem (SAT) will be added.

Basic rectangle collision can still be used by colliding with the MBR (minimum bounding rectangle) on the object.

Make some vids

  • Introduce Entity Component
  • Native Components
  • Making components

Test for IE, silently degrade

Make sure all the DOM works in IE. If no canvas, make sure errors don't exist.

Try using filters for rotation, opacity and what-not.

Rotation

DOM can be implemented with CSS3 transform but canvas...? Maybe canvas transformations. Don't really understand how it works.

stopProppagation

Event function to stop other handlers being called for an event.

Better Scrolling

Scrolling should work by shifting the canvas element an using another canvas.

DOM Stage get's created irrespectively to the use of it.

The DOM Stage get's created irrespectively to the use of it, so if you would only use the canvas (which is preferably on any browser that fully supports it) the DOM Stage still get's created.

I would opt in for a parameter to the Crafty.init function to set the Stage, DOM or Canvas. This would allow for feature detection and delivering the Stage that is relevant for that browser.

This way you can support the older browsers without support for canvas and the newer browsers can make full utilization of Canvas and the browser's ability to Hardware accelerate it.

Window Resize

Recalculate Crafty.window and Crafty.stage/viewport (if set to fullscreen) when the window resizes.

.requires()

Useful for components that requires a set of components but should only add if not previously added.

Tween

Add a component to interpolate the values to make a small animation between 2D properties. Copy the API of jQuery.

Physics Component

Add a physics component that includes gravity and bounce and friction

Global Assets

Keep a global collection of assets used by the loader and other components can check if it's been loaded first. Also handy for the builder.

Unit Tests

At least for Core. Maybe other components.

origin is shared among objects

All objects have the same origin, when you change the origin of 1 object all the other objects also change origin.

This shouldn't happen

Better Comments

Seeing as most components were written fairly quickly, they aren't commented very well. Go through the code and add some good commenting.

Advanced Colours

Be able to use all types of color (rgb, hex, hsl, simple)

{
    aliceblue: 'f0f8ff',
    antiquewhite: 'faebd7',
    aqua: '00ffff',
    aquamarine: '7fffd4',
    azure: 'f0ffff',
    beige: 'f5f5dc',
    bisque: 'ffe4c4',
    black: '000000',
    blanchedalmond: 'ffebcd',
    blue: '0000ff',
    blueviolet: '8a2be2',
    brown: 'a52a2a',
    burlywood: 'deb887',
    cadetblue: '5f9ea0',
    chartreuse: '7fff00',
    chocolate: 'd2691e',
    coral: 'ff7f50',
    cornflowerblue: '6495ed',
    cornsilk: 'fff8dc',
    crimson: 'dc143c',
    cyan: '00ffff',
    darkblue: '00008b',
    darkcyan: '008b8b',
    darkgoldenrod: 'b8860b',
    darkgray: 'a9a9a9',
    darkgreen: '006400',
    darkkhaki: 'bdb76b',
    darkmagenta: '8b008b',
    darkolivegreen: '556b2f',
    darkorange: 'ff8c00',
    darkorchid: '9932cc',
    darkred: '8b0000',
    darksalmon: 'e9967a',
    darkseagreen: '8fbc8f',
    darkslateblue: '483d8b',
    darkslategray: '2f4f4f',
    darkturquoise: '00ced1',
    darkviolet: '9400d3',
    deeppink: 'ff1493',
    deepskyblue: '00bfff',
    dimgray: '696969',
    dodgerblue: '1e90ff',
    feldspar: 'd19275',
    firebrick: 'b22222',
    floralwhite: 'fffaf0',
    forestgreen: '228b22',
    fuchsia: 'ff00ff',
    gainsboro: 'dcdcdc',
    ghostwhite: 'f8f8ff',
    gold: 'ffd700',
    goldenrod: 'daa520',
    gray: '808080',
    green: '008000',
    greenyellow: 'adff2f',
    honeydew: 'f0fff0',
    hotpink: 'ff69b4',
    indianred: 'cd5c5c',
    indigo: '4b0082',
    ivory: 'fffff0',
    khaki: 'f0e68c',
    lavender: 'e6e6fa',
    lavenderblush: 'fff0f5',
    lawngreen: '7cfc00',
    lemonchiffon: 'fffacd',
    lightblue: 'add8e6',
    lightcoral: 'f08080',
    lightcyan: 'e0ffff',
    lightgoldenrodyellow: 'fafad2',
    lightgrey: 'd3d3d3',
    lightgreen: '90ee90',
    lightpink: 'ffb6c1',
    lightsalmon: 'ffa07a',
    lightseagreen: '20b2aa',
    lightskyblue: '87cefa',
    lightslateblue: '8470ff',
    lightslategray: '778899',
    lightsteelblue: 'b0c4de',
    lightyellow: 'ffffe0',
    lime: '00ff00',
    limegreen: '32cd32',
    linen: 'faf0e6',
    magenta: 'ff00ff',
    maroon: '800000',
    mediumaquamarine: '66cdaa',
    mediumblue: '0000cd',
    mediumorchid: 'ba55d3',
    mediumpurple: '9370d8',
    mediumseagreen: '3cb371',
    mediumslateblue: '7b68ee',
    mediumspringgreen: '00fa9a',
    mediumturquoise: '48d1cc',
    mediumvioletred: 'c71585',
    midnightblue: '191970',
    mintcream: 'f5fffa',
    mistyrose: 'ffe4e1',
    moccasin: 'ffe4b5',
    navajowhite: 'ffdead',
    navy: '000080',
    oldlace: 'fdf5e6',
    olive: '808000',
    olivedrab: '6b8e23',
    orange: 'ffa500',
    orangered: 'ff4500',
    orchid: 'da70d6',
    palegoldenrod: 'eee8aa',
    palegreen: '98fb98',
    paleturquoise: 'afeeee',
    palevioletred: 'd87093',
    papayawhip: 'ffefd5',
    peachpuff: 'ffdab9',
    peru: 'cd853f',
    pink: 'ffc0cb',
    plum: 'dda0dd',
    powderblue: 'b0e0e6',
    purple: '800080',
    red: 'ff0000',
    rosybrown: 'bc8f8f',
    royalblue: '4169e1',
    saddlebrown: '8b4513',
    salmon: 'fa8072',
    sandybrown: 'f4a460',
    seagreen: '2e8b57',
    seashell: 'fff5ee',
    sienna: 'a0522d',
    silver: 'c0c0c0',
    skyblue: '87ceeb',
    slateblue: '6a5acd',
    slategray: '708090',
    snow: 'fffafa',
    springgreen: '00ff7f',
    steelblue: '4682b4',
    tan: 'd2b48c',
    teal: '008080',
    metle: 'd8bfd8',
    tomato: 'ff6347',
    turquoise: '40e0d0',
    violet: 'ee82ee',
    violetred: 'd02090',
    wheat: 'f5deb3',
    white: 'ffffff',
    whitesmoke: 'f5f5f5',
    yellow: 'ffff00',
    yellowgreen: '9acd32'
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.