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

9 years ago by therootbeerking

Hello, I'm very new to programming(learning as I go), I've dabbled in the past with Impact years ago, but now I'm finally taking a serious crack and making and finishing a game. The game I'm making is a puzzle game along the lines of Puyo Puyo. So far I've managed to hack together an entity that spawns 6 different random colored creatures that fall down the board in a horizontal line using:

update: function() {
            
            if (this.canFall === true) {
                    if (this.isFalling.delta() > 0) {
                        this.pos.y += 42;
                        console.log(this.pos.y);
                        this.isFalling.reset();
                    };

                     if (this.pos.y >= 508) {
                        this.canFall = false;
                        };
                };

            this.parent(); 
        }

Currently this works as I want it, but only to a point. The creature entities will stop moving once they reach the end of the board(at pos.y 508) however, I've hit a wall knowledge wise as to how to get the entities to also stop when an entity is already in that position. In my head I imagine I could do something like: if there is already an entity at pos.y 508, then stop moving current entity before it gets to said position... However, I have no idea how to go about doing this, or if that is even the best way to go about doing this.

So that's why I'm here, if someone could please help me out with this problem I'd be forever grateful! I've been searching for the answer all day to no avail.

9 years ago by Joncom

If you move entities by setting their velocity, and give your entities a collides type of ig.Entity.COLLIDES.ACTIVE, this should prevent them from overlapping. I think that's what you want?

9 years ago by therootbeerking

Hey Joncom, thanks for the reply but sorry, that's not what I want, but I appreciate your trying to help. The reason I'm using pos.y = 42 is because I want the creatures to move in a grid like in Tetris(and other falling block type puzzle games). Basically I'm trying to come up with a system that says if there is an entity already in position on the board(map) then stop moving the current entity down. Because eventually I'll need to also figure out how to make it know what colored creature entity is in that same position, for matching color combos.

So to reiterate my question, I'm wondering how would I go about checking to see if there is an entity in a falling(moving) entity's path?

That way I could then switch off the "this.canFall" variable and stop the entity at the position just above the other entity. Preferably, I'm looking for away to detect other entities on the board so that I can also do things like, if the entity that was blocking the path is removed the entity(s) above the one that was removed all move down one row on the board.

Sorry once again if my question was confusing.

Cheers!

EDIT: I figured out part of the solution using check.

 check: function( other ) {
            this.canFall = false;
            this.pos.y -= 2;
            other.pos.y += 2;
            this.lastPosY = this.pos.y;
            this.parent();
        },

        update: function() {
            // This method is called for every frame on each entity.
            // React to input, or compute the entity's AI here.
            this.vel.y += 1;
            this.vel.y -= 1;

            if (this.canFall === true) {
                    if (this.isFalling.delta() > 0) {
                        this.pos.y += 42;
                        console.log(this.pos.y);
                        this.isFalling.reset();
                    };

                     if (this.pos.y >= 508) {
                        this.canFall = false;
                        this.lastPosY = 508;
                        };
                } else {
                    this.pos.y = this.lastPosY;
                };

            // Call the parent update() method to move the entity
            // according to its physics
            this.parent(); 
        }

However, I can't figure out how to go about turning the falling back on if the entity below an entity is removed. So I think I still need to know how to tell if an entity exists at a given position. If someone could help me with that ^_^

9 years ago by Joncom

update: function() {
    var canFall = true;
    var creatures = ig.game.getEntitiesByType(EntityCreature);
    // Loop over all the creatures that might be below
    for(var i=0; i<creatures.length; i++) {
        var creature = creatures[i];
        // Is there already something in the spot below?
        if(creature.pos.y === this.pos.y + 42) {
            // Yes, so prevent falling this frame
            canFall = false;
            // We can break out of this loop and stop checking now...
            break;
        }
    }
    ...
    this.parent();
}

Maybe?

9 years ago by therootbeerking

Thanks again Joncom, I think that would work, but I can't get it to play nice with my current code... this is what I've got:
                check: function( other ) {
            this.canFall = false;
            this.pos.y -= 4;
            other.pos.y += 4;
            this.lastPosY = this.pos.y;
            if (this.doOnce === false) {
                    ig.global.Spawned++;
                    this.doOnce = true;
                    };
            this.parent();
        },

        update: function() {
            // This method is called for every frame on each entity.
            // React to input, or compute the entity's AI here.
//change sprite sheet based on what creature of spawned.
            if (this.isColor === 'blue') {
                this.currentAnim = this.anims.blueCreatureIdle;
            } else if (this.isColor === 'green') {
                this.currentAnim = this.anims.greenCreatureIdle;
            }else if (this.isColor === 'red') {
                this.currentAnim = this.anims.redCreatureIdle;
            }else if (this.isColor === 'yellow') {
                this.currentAnim = this.anims.yellowCreatureIdle;
            }


            if (this.canFall === true) {
                    if (this.isFalling.delta() > 0) {
                        this.pos.y += 42;
                        this.isFalling.reset();
                    };
            } else {
                    this.pos.y = this.lastPosY;
                    this.vel.y += 1;
                    this.vel.y -= 1;
                };

            if (ig.input.pressed('leftButton') && this.inFocus()) {
                this.kill() 

                console.log(this.pos.x);
                console.log('removed');
            };

            // Call the parent update() method to move the entity
            // according to its physics
            this.parent(); 
        },

Now instead of falling and staying put like they should they just randomly disappear or fall out of order and never stay put.

Any ideas what I might be doing wrong??

9 years ago by Joncom

Maybe remove that check function. Do you need it for something?

And remember, you don't need to "reset" the entity to its last position, because it only moves if there is room for it.

update: function() {

    this.canFall = true;
    var creatures = ig.game.getEntitiesByType(EntityShapecreature);
    // Loop over all the creatures that might be below
    for (var i = 0; i < creatures.length; i++) {
        var creature = creatures[i];
        // Is there already something in the spot below?
        if (creature.pos.y === this.pos.y + 42) {
            // Yes, so prevent falling this frame
            this.canFall = false;
            // We can break out of this loop and stop checking now...
            break;
        }
    }

    // Cancel "canFall" if we are at the bottom
    if (this.pos.y >= 508) {
        this.canFall = false;
    }

    if (this.canFall && this.isFalling.delta() > 0) {
        this.pos.y += 42;
        this.isFalling.reset();
    }

    if (ig.input.pressed('leftButton') && this.inFocus()) {
        this.kill()
        console.log('removed');
    }

    this.parent();
},

9 years ago by therootbeerking

Hey Joncom thanks again for all your help, however, that doesn't quite work either...

Currently, the only way I could get the Tetris-like grid movement, and still use impact's collision system was to go with dynamic collision, so the ground is an entity. With that in mind the check function is used for a couple of things.
1. Turns off Falling when a creature hits an other entity(either a ground entity, or another creature)
2. bounces the y positions(by 4 pixels) of the creature and the entity it collided with so that the entity doesn't fall through the entry it collided with.
3. Sets the current position to a variable called "this.lastPosY" that insures the spot that the entity collided at is kept(so it stays in the position on the grid)
4. Lastly I'm using it to tell the spawner that all 6 of the creatures it spawned have landed on something, as the spawner is set up to only spawn more creatures after the creatures it spawned have stopped moving once.

check: function( other ) {
            this.canFall = false;
            this.pos.y -= 4;
            other.pos.y += 4;
            this.lastPosY = this.pos.y;
            if (this.doOnce === false) {
                    ig.global.Spawned++;
                    this.doOnce = true;
                    };
            this.parent();
        }

I tried taking it out, and just used the code you provided, however I'm running into a lot of issues, issue one, each creature will move down at different speeds, so instead of a nice horizontal line of 6 creatures, we now have a diagonal line of 6 creatures. Next the creatures are not staying in the grid and will overlap and cause other weird collision errors like falling through the ground and other creatures. Lastly when a creature is removed from under an other one the creature will still stay in place until all creatures from the previous line are removed...

9 years ago by Joncom

If you're teleporting things around like Tetris blocks, well, that really isn't well suited to physics (ie. collision).

I would turn off gravity if you have it on. As well as collision of every kind.

It would be much simpler if you just use timers and move things to the correct position when the time is right (and if there is room below).

For starters though, just focus on getting them to fall at a nice pace, moving the correct distance each time, and then stopping at the "bottom".

Once you have that working, THEN consider the check that prevents them from falling through each other.

9 years ago by therootbeerking

Haha, yeah like I said, I'm learning this whole coding biz as I go along, so I'm probably programming this in the most complicated way possible. I think I figured out a good system, I just need to know if it's possible to check if an entity is NOT colliding with an entity, if that is possible then I think I might have solved the puzzle.

Basically my idea is an invisible entity that detects when an entity is overlapping with it's self, and when that happens it kicks out a number into an array that represents the board, this works great however I don't know how to go about it changing the number back to 0 if the entity overlapping the detector is no longer overlapping.

I thought maybe something like
        update: function() {
            if (this.check() === false) {
                rows[0] = 0;
            };         

            this.parent(); 
        }

would work, but no luck.

EDIT:

I think I'm on the right track with this new detector code however the else code is still running even when this.touches is active. What might be the solution here?

        update: function() {
            // This method is called for every frame on each entity.
            // React to input, or compute the entity's AI here.
             
            var creatures = ig.game.getEntitiesByType(EntityShapecreature);
            // Loop over all the creatures that might be below
            for(var i=0; i<creatures.length; i++) {
                    var creature = creatures[i];
                    if(this.touches(creature)) {
                    //detect what color creature is touching this space on the board and tell the array.
                        if(creature.isColor === 'blue') {
                            rows[yToRow][0] = 4;
                        console.log('Blue detected!'+rows[yToRow]);
                        };
                        if (creature.isColor === 'green') {
                            rows[yToRow][0] = 3;
                            console.log('Green detected!'+rows[yToRow]);
                        }; 
                        if (creature.isColor === 'red') {
                            rows[yToRow][0] = 2;
                            console.log('Red detected!'+rows[yToRow]);
                        }; 
                        if (creature.isColor === 'yellow') {
                            rows[yToRow][0] = 1;
                            console.log('Yellow detected!'+rows[yToRow]);
                        };
                    } else {
                        //space on board is empty
                        rows[yToRow][0] = 0; 
                        console.log(rows[yToRow][0]);
                    };
            };

9 years ago by Joncom

If you have more than one detector entity spawned, then the logic from one detector update might be overriding the rows data set by another detector update?

Edit:

Still not sure if a "detector" if necessary.

If an entity is at position x=0, y=0, then you know it's at the top left corner.

If an entity is at position x=42, y=0, then you know it's 1 unit right of the above.

If an entity is at position x=42, y=84, then you know it's 2 units below the above.

You can find an entity "board position" by dividing entity.pos.x/y by 42.

Therefore, you can check if you reached the bottom before falling too far.

You can also use this to check if there is an entity directly below (has the same pos.x, but a pos.y that is 42 greater).

9 years ago by therootbeerking

Thanks for all the help Joncom! I think I've figured it all out now.

You can also use this to check if there is an entity directly below (has the same pos.x, but a pos.y that is 42 greater).


I think this is the last piece of the puzzle. Thanks again, I appreciate you taking the time out of your day to help me sort this out. Thanks to you I finally understand "for loops" too, which is nice, haha.

9 years ago by Joncom

Happy to help.
Page 1 of 1
« first « previous next › last »