Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Logan

Hello,

Yeah, yeah, I know I forgot my second "t" in entity's :( maybe someone could fix it?

With an extremely large amount of help from "aho" on the IRC I have a simple top down shooter game with 3 entity's: player.js, bullet.js, and enemy.js but I'm not to concerned about the enemy.js for now..

I'm trying to make a bullet move faster along a normalized vector, but with the current code my bullet doesn't follow the vector direction accurately with an increase in speed..

Live Demo: http://goo.gl/EyIHQB

FIXED: "MaxVel:{}" changed to: "maxVel" Grr... :D


bullet.js:
/**
 * Created by Logan Looker on 10/31/13.
 */
ig.module(
    'game.entities.bullet'
)
.requires(
    'impact.entity'
)
.defines(function() {

EntityBullet = ig.Entity.extend({

    size:{x:4,y:4},
    MaxVel:{x:1000, y:1000},
    checkAgainst: ig.Entity.TYPE.B,
    collides: ig.Entity.COLLIDES.PASSIVE,

    animSheet: new ig.AnimationSheet( 'media/tilesets/4x4_bullet.png', 4, 4 ),


    init: function( x, y, settings ) {
        this.parent( x, y, settings );

        this.addAnim( 'idle', 1, [0] );

        var speed = 100;

        this.vel.x = settings.directionx * speed;
        this.vel.y = settings.directiony * speed;

    },

    update: function(){

        this.parent();

    }


});

});

player.js:
/**
 * Created by Logan Looker on 10/31/13.
 */
ig.module(
    'game.entities.player'
)
    .requires(
        'impact.entity',
        'game.entities.bullet'
    )
    .defines(function(){

        EntityPlayer = ig.Entity.extend({

            //Player Properties
            size: {x:32,y:48},
            vel: {x:0,y:0},
            maxVel: {x:200,y:200},
            health: 100,
            zIndex: 999,
            name: 'Hero',

            //Collision Detection
            collides: ig.Entity.COLLIDES.PASSIVE,
            type: ig.Entity.TYPE.A,
            checkAgainst: ig.Entity.TYPE.B,

            //Animations
            animSheet: new ig.AnimationSheet( 'media/tilesets/32x32_spritesheet.png', 32, 48),

            init: function( x, y, settings ) {
                this.parent( x, y, settings );

                this.inventory = [0,1,2,3,4,5,6,7,8,9,10];
                this.xp = 0;
                this.ammo = 1000;
                this.mines = 0;
                this.grenades = 0;
                this.money = 100;
                this.weapon = '';


                this.addAnim( 'up', 1, [2]);
                this.addAnim( 'down', 1, [2]);
                this.addAnim( 'left', 1, [2]);
                this.addAnim( 'right', 1, [2]);

                //this.addAnim( 'up', 0.1, [1,2]);
                //this.addAnim( 'down', 0.1, [3,4]);
                //this.addAnim( 'left', 0.1, [5,6]);
                //this.addAnim( 'right', 0.1, [7,8]);
                //this.addAnim( 'dia_up_right', 0.1, [9,10]);
                //this.addAnim( 'dia_up_left', 0.1, [11,12]);
                //this.addAnim( 'dia_down_left', 0.1, [13,14]);
                //this.addAnim( 'dia_down_right', 0.1, [15,16]);

            },

            update: function(){

                this.vel.y = 0;
                this.vel.x = 0;

                if( ig.input.pressed('shoot') ){
                    var originX = this.pos.x + 28;
                    var originY = this.pos.y + 30;

                    var TargetX = ig.input.mouse.x;
                    var TargetY = ig.input.mouse.y;

                    var DirX = TargetX - originX;
                    var DirY = TargetY - originY;

                    var len = Math.sqrt(DirX * DirX + DirY * DirY);
                    DirX /= len;
                    DirY /= len;

                    ig.game.spawnEntity(EntityBullet, originX, originY, {directionx:DirX, directiony:DirY});
                }

                if( ig.input.state('up') ){
                    this.vel.y = -100;
                }

                if( ig.input.state('down') ){
                    this.vel.y = 100;
                }

                if( ig.input.state('left') ){
                   this.vel.x = -100;
                }

                if( ig.input.state('right') ){
                   this.vel.x = 100;
                }

                this.parent();

            }

        });

    });

main.js:
ig.module( 
	'game.main' 
)
.requires(
    'game.levels.sal_town',
    'game.entities.player',
    'game.entities.enemy',
    'game.entities.bullet',
    'impact.game',
	'impact.font',
    'impact.debug.debug'
)
.defines(function(){

//Game Variables
GameInfo = new function() {
    this.lives = 25;
    this.level = LevelSal_town;
    this.currentLevel = LevelSal_town;
};

MyGame = ig.Game.extend({
	
	// Load a font
	font: new ig.Font( 'media/04b03.font.png' ),
	
	
	init: function() {
		// Initialize your game here; bind keys etc.
        ig.input.bind( ig.KEY.MOUSE1, 'shoot');
        ig.input.bind( ig.KEY.W, 'up');
        ig.input.bind( ig.KEY.A, 'left');
        ig.input.bind( ig.KEY.S, 'down');
        ig.input.bind( ig.KEY.D, 'right');
        ig.input.bind( ig.KEY.I, 'inventory');

        this.loadLevel( LevelSal_town );
        //this.camera = 'follow';
	},
	
	update: function() {
		// Update all entities and backgroundMaps
		this.parent();

		// Add your own, additional update code here
	},
	
	draw: function() {
		// Draw all entities and backgroundMaps
		this.parent();

        //show the health and money of the user in the left upper corner
        var player = this.getEntitiesByType( EntityPlayer )[0];
        //this.myNoteManager.draw();

        this.font.draw('HEALTH: ' + player.health,10,10); //Player's Health
        this.font.draw('IN-GAME CURRENCY: '  + player.money,10,20); //Player's $$$$$$$
        this.font.draw('WEAPON: '  + player.weapon,10,30); //Player's Weapon
        this.font.draw('XP: '  + player.xp,10,40); //Player's XP
        this.font.draw('AMMO: '  + player.ammo,10,50); //Player's Ammo
        this.font.draw('Player X: '  + player.pos.x,10,60); //Player's X
        this.font.draw('Player Y: '  + player.pos.y,10,70); //Player's Y
        this.font.draw('Mouse X: ' + ig.input.mouse.x,10,80);
        this.font.draw('Mouse Y: ' + ig.input.mouse.y,10,90);
	}
});


// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', MyGame, 60, 960, 640, 1 );

});

now if you were to run that code the the game would work perfect but as soon as you change the bullet.speed in the bullet.js the bullets accuracy is greatly hindered..

but if you replace bullet.js with this:


NEW bullet.js:
    /**
     * Created by aho on 10/31/13.
     */
    ig.module(
        'game.entities.bullet'
    )
    .requires(
        'impact.entity'
    )
    .defines(function() {
     
    EntityBullet = ig.Entity.extend({
     
        size:{x:4,y:4},
        checkAgainst: ig.Entity.TYPE.B,
        collides: ig.Entity.COLLIDES.PASSIVE,
        fx: 0,
        fy: 0,
        dx: 0,
        dy: 0,
     
        animSheet: new ig.AnimationSheet( 'media/tilesets/4x4_bullet.png', 4, 4 ),
     
     
        init: function( x, y, settings ) {
            this.parent( x, y, settings );
     
            this.addAnim( 'idle', 1, [0] );
     
            var speed = 200;
     
            this.fx = x;
            this.fy = y;
     
            this.dx = settings.directionx * speed;
            this.dy = settings.directiony * speed;
     
        },
     
        update: function(){
            this.fx += dx * ig.system.tick;
            this.fy += dy * ig.system.tick;
            this.pos.x = this.fx;
            this.pos.y = this.fy;
            this.parent();
        }
     
     
    });
     
    });

The bullet will shoot accurately no matter what the speed is but there is no collision with the collision tiles....



Any Help would be amazing! sorry for all the code snippets but I didnt want anyone to be confused when they read this..

1 decade ago by Joncom

So your question is: "Why don't my bullets collide with the collision map?"

The answer is: because you are manually moving your entity, thus overriding the collision handling.

May I suggest you give your properties more meaningful names? The purpose of fx, fy, dx, and dy are not immediately obvious, and this makes offering you a solution based on what you already have more difficult than it needs to be.

1 decade ago by Logan

Not not really, my bullets were becoming less accurate with the more speed I gave them.. but when I manually moved them they stayed accurate with increased speed..

Basically I just had maxVel spelled: MaxVel which messed somethings up..
Page 1 of 1
« first « previous next › last »