Click here to Skip to main content
15,886,519 members
Articles / Web Development / HTML5

Editor for Mario5

Rate me:
Please Sign up or sign in to vote.
4.98/5 (62 votes)
2 Aug 2012CPOL22 min read 106.6K   3K   90  
Adding some spice to the Mario game by providing a Level editor with a social platform.
var Animation = Level.extend({
    init: function (id) {
        this.world = $('#' + id);
		this.setPosition(0, 0);
        this.input = [];
        this.speeches = [];
        this.currentSpeeches = [];
        this.animations = [];
        this.currentAnimations = [];
        this.cycles = 0;
        this.maxCycles = 0;
		this.reset();
    },
    load: function(level) {
        this._super(level);
        this.onend = level.onend || function() {};
        this.maxCycles = Math.ceil(level.duration / constants.interval);

        for(var i = 0; i < level.characters.length; i++) {
            var character = level.characters[i];
            var figure = new (reflection[character.name])(character.x, character.y, this);

            for(var j = 0; j < character.speeches.length; j++) {
                var speech = character.speeches[j];
                this.speeches.push({
                    figure: figure,
                    start: Math.floor(speech.start / constants.interval),
                    end: Math.floor(speech.end / constants.interval),
                    text: speech.text
                });
            }

            for(var j = 0; j < character.animations.length; j++) {
                var animation = character.animations[j];
                var obj = {
                    figure: figure,
                    start: Math.floor(animation.start / constants.interval),
                    end: Math.floor(animation.end / constants.interval)
                };

                for(var key in animation) {
                    if(obj[key] === undefined)
                        obj[key] = animation[key];
                }

                this.animations.push(obj);
            }
        }

        this.speeches.sort(function(a, b) {
            return b.start - a.start;
        });

        this.animations.sort(function(a, b) {
            return b.start - a.start;
        });
    },
    createSpeech: function(s) {
		var pos = s.figure.view.position();
        s.element = $(DIV).addClass('speech-bubble').appendTo(this.world).text(s.text).css({
			left: pos.left - 90,
			top: pos.top - s.figure.view.height() - 40
		});
    },
    removeSpeech: function(index) {
        var s = this.currentSpeeches[index];
        s.element.remove();
        this.currentSpeeches.splice(index, 1);
    },
    createAnimation: function(a) {
        if(a.x !== undefined) {
            var dx = (a.x - a.figure.x) / (a.end - a.start);
            var dy = a.figure.vy;
            a.figure.setVelocity(dx, dy);
        }

        if(a.background !== undefined) {
            a.figure.setImage(a.background.image, a.background.x, a.background.y);
        }
    },
    removeAnimation: function(index) {
        var a = this.currentAnimations[index];

        if(a.x !== undefined) {
            a.figure.setVelocity(0, a.figure.vy)
        }

        this.currentAnimations.splice(index, 1);
    },
    tick: function () {
        var i = 0, figure;

        if(this.cycles === this.maxCycles) {
            this.onend();
            this.pause();
            return;
        }

        for(i = this.currentSpeeches.length; i--; ) {
            if(this.currentSpeeches[i].end === this.cycles)
                this.removeSpeech(i);
			else if(this.currentSpeeches[i].figure.vx !== 0) {
				this.currentSpeeches[i].element.css({
					left: '+=' + this.currentSpeeches[i].figure.vx
				});
			}
        }

        for(i = this.currentAnimations.length; i--; ) {
            if(this.currentAnimations[i].end === this.cycles)
                this.removeAnimation(i);
        }

        while(this.speeches.length && this.speeches[this.speeches.length - 1].start === this.cycles) {
            var speech = this.speeches.pop();
            this.createSpeech(speech);
            this.currentSpeeches.push(speech);
        }

        while(this.animations.length && this.animations[this.animations.length - 1].start === this.cycles) {
            var animation = this.animations.pop();
            this.createAnimation(animation);
            this.currentAnimations.push(animation);
        }
		
		for(i = this.figures.length; i--; ) {
			figure = this.figures[i];
			figure.move();
			figure.playFrame();
		}
		
		for(i = this.items.length; i--; ) {
			this.items[i].playFrame();
        }

        this.cycles = this.cycles + 1;
    },
});

var endingLevel = {
    data: [
        ['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'planted_soil_left'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'planted_soil_middle'],
		['', '', '', '', '', '', '', '', '', '', '', 'bush_left', 'grass_top', 'soil', 'planted_soil_right'],
		['', '', '', '', '', '', '', '', '', '', '', 'bush_middle_left', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', 'bush_middle', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', 'bush_middle_right', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', 'bush_right', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'soil', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'planted_soil_left', 'soil'],
		['', '', '', '', '', '', '', '', '', '', '', '', 'grass_top', 'planted_soil_middle', 'soil']
    ],
    width: 20,
    height: 15,
    id: 15,
    background: 4,
    onend: function() { },
    duration: 16000,
    characters: [
        {
            name: 'bigmario',
            x: -30,
            y: 96,
            speeches: [
                {
                    start: 7500,
                    end: 10500,
                    text: 'Oh Daisy!'
                }
            ],
            animations: [
                {
                    start: 2500,
                    end: 4100,
                    x: 100
                },
                {
                    start: 7900,
                    end: 9900,
                    x: 260
                },
                {
                    start: 14000,
                    end: 14000,
                    background: {
                        x: 384,
                        y: 88,
                        image: images.sprites
                    }
                }
            ]
        },
        {
            name: 'peach',
            x: 520,
            y: 96,
            speeches: [
                {
                    start: 0,
                    end: 3000,
                    text: 'Mario!'
                },
                {
                    start: 4000,
                    end: 7000,
                    text: 'Thank you, Mario!'
                },
                {
                    start: 10000,
                    end: 13000,
                    text: 'I love you, Mario!'
                }
            ],
            animations: [
                {
                    start: 7000,
                    end: 10000,
                    x: 320
                },
                {
                    start: 14000,
                    end: 14000,
                    background: {
                        x: 38,
                        y: 2,
                        image: images.peach
                    }
                }
            ]
        },
    ],
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Germany Germany
Florian lives in Munich, Germany. He started his programming career with Perl. After programming C/C++ for some years he discovered his favorite programming language C#. He did work at Siemens as a programmer until he decided to study Physics.

During his studies he worked as an IT consultant for various companies. After graduating with a PhD in theoretical particle Physics he is working as a senior technical consultant in the field of home automation and IoT.

Florian has been giving lectures in C#, HTML5 with CSS3 and JavaScript, software design, and other topics. He is regularly giving talks at user groups, conferences, and companies. He is actively contributing to open-source projects. Florian is the maintainer of AngleSharp, a completely managed browser engine.

Comments and Discussions