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 littlefoot

I've been working on the first game daily and have gotten to the point of level 1 being pretty much done and loading the next level when the player passes the first.

But the second level requires an entity that "swoops" from the sky toward the player until it is just above the player entity (the player will usually be moving, but can potentially be stopped as well), drops one of two entities toward the player entity (which one is dropped is random), and flies back up until the next "swoop". Each swoop interval will be triggered within between 30 seconds and 1 minute. I've been trying to get my head around this and trying everything from a delta timer to having a collision layer with sloped collision tiles creating a downward and upward arc path above the ground collision tiles, but can't seem to get this working exactly.

At this point it seems that I should be getting the entity to spawn from scratch for each 'swoop' because it has to swoop down toward the direction of the player as opposed to spawning it at the beginning of the level in Weltmeister, or maybe somehow changing the pos of the swooping entity when the timer is triggered to just above the player (but how do I then make it move down in an arc and then back up...). In other words I have no idea what the best way to go about this would be or where to properly start. Would anyone be able to point me in the right direction?

Thanks

1 decade ago by Arantor

My gut says that during the entity's init, set up a timer for the time to next swoop and record whether the entity is waiting-for-swoop or in-swoop.

When the timer expires (check in the entity's update()), switch the state over to in-swoop and do whatever that has to do, including spawning the other entity, and when swooping is done, flick the state back and then reset the timer.

As far as the 'in-swoop' goes, I guess mostly that depends on what the swoop movement should be, either following the line of the sine curve for 90 to 180 degrees, or some variation on the quadratic curve (which would likely be faster to calculate), or if it's something less directly mathematical.

1 decade ago by gxxaxx

For simplicity, and a basic test of concept, you might try a simple dive at the player.

Or, actually a small offset above the player.

Create a point -- something like:

var target = {x: ig.game.player.pos.x, y: ig.game.player.pos.y - offset};

then figure the angle

var angle = this.angleTo(target);
var x = Math.cos(angle);
var y = Math.sin(angle);

this.vel.x = something * x;
this.vel.y = something * y;

This is just general idea.

Point is to get a simple dive working to test your timers and drop creation.
Then you can add as sophisticated of a movement pattern as you like.

1 decade ago by littlefoot

First, thanks for pointing me into the right direction. I've been working on this and I think I almost have the swooping motion. Here is what I have in the flying entity:

In the init function:

this.timer = new ig.Timer(5);

(I'm using 5 seconds as a test)

In the update function:

if (this.timer.delta() > 0) {
			this.pos.x = ig.game.player.pos.x + -100;
			var angle = this.angleTo(ig.game.player);
			var x = Math.cos(angle);
			var y = Math.sin(angle);
            console.log ( 'Swooping!' );
			this.vel.x = x * this.speed;
			this.vel.y = y * this.speed;  
			this.swooping = true;
			if (this.swooping && this.pos.y > ig.game.player.pos.y - 250) {
				console.log( 'Unswooping!' ); 
				this.vel.x = x * this.speed;
				this.vel.y = -y * this.speed;   
				this.swooping = false;
				this.timer.reset(); 

			}
	}

gxxaxx, I tried using the target variable but I kept getting an error saying 'cannot get y of undefined', which is why I replaced the angleTo with 'ig.game.player'.

So the current behavior is: the entity swoops at regular intervals. The problem is with movement - the entity swoops down and reaches just above the player entity on the x axis at the point that it reaches the required height on the y axis (it can't be too low), then turns back and reverses its path. I've tried playing with its speed attribute but nothing changed. What it actually needs to do is swoop down from even farther behind the player entity than it's currently doing (I'm thinking 200-300 instead of 100), fly past the player entity in an arc and then swoop back up going forward, not backward. I hope this makes sense, apologies if not (I could illustrate in a graphic if needed).

So far I've tried changing the speed attribute like I said as well as playing around with the vel and that if statement (saying && this.pos.x as well, but the x and y positions are not corresponding at the right time. If the flying entity is in the correct position on the x axis it is now too low on the y axis). I'm obviously just being a noob here. Any advice is greatly appreciated.

Thanks again for your help, in the meantime I'll start figuring out how to make this thing drop stuff.

1 decade ago by littlefoot

Ok, so I got the flying entity to drop one entity on each swoop. It works fine when I set vel.y to a number like 100 in that I can see the entity being dropped, it's just not being dropped toward the player. I need the item to be dropped in the direction of the player entity. I tried this in the init function of the entity that's being dropped:

init: function( x, y, settings ) {
  this.parent( x - this.halfWidth, y, settings );
  
            console.log ( 'Created!' );
            var angle2 = this.angleTo(ig.game.player);
			var x2 = Math.cos(angle2);
			var y2 = Math.sin(angle2);
			this.vel.x = this.x2 * this.speed;
			this.vel.y = this.y2 * this.speed;
    		this.addAnim( 'idle', 1, [0] );
  },

I wasn't expecting this to be perfect or exactly what I need, but at least I thought it might start me off in the right direction (when I can see what it's actually doing). However while the debugger shows the entity being created (and the "Created!" text is appearing in the log) I can't actually see the entity itself. It is being spawned by the flying entity like this:

ig.game.spawnEntity( EntityDropped, this.pos.x + this.halfWidth, this.pos.y + 100 );

1 decade ago by littlefoot

(Eh sorry, quick update: I now have it randomly spawning the two different entities that I needed so that's working. They're just still not going into the right direction)

1 decade ago by littlefoot

I've changed the functionality of this a bit. Instead of spawning one drop entity at a time I've decided to make the flying entity drop a batch with each swoop. This in the update function seems to be working for the drop spawning:

if (this.timer.delta() > 0) {
			this.pos.x = ig.game.player.pos.x + -100;
			var angle = this.angleTo(ig.game.player);
			var x = Math.cos(angle);
			var y = Math.sin(angle);
            console.log ( 'Swooping!' );
			this.vel.x = x * this.speed;
			this.vel.y = y * this.speed;  
			this.swooping = true;
			if (this.swooping && this.pos.y > ig.game.player.pos.y - 250) {
				console.log( 'Unswooping!' ); 
				
				this.vel.x = x * this.speed;
				this.vel.y = y * -this.speed;   
				var batchsize=Math.floor(Math.random()*6) + 5;
				console.log( batchsize );
   			   		while (batchsize > 0) { 
   			   			var randomnumber=Math.floor(Math.random()*4);
   			   			if ( randomnumber == 0 ) {
	    					ig.game.spawnEntity( EntityRedPistachio, this.pos.x + this.halfWidth, this.pos.y + 100 );
	    				}
	    				else if ( randomnumber > 0 ) {
	    					ig.game.spawnEntity( EntityGreenPistachio, this.pos.x + this.halfWidth, this.pos.y + 100 );
	    				}
	    				batchsize = batchsize-1;
   					 }
   					 
   					
				this.swooping = false;
				this.timer.reset(); 
			}
	}	    

However there is still the problem with the arc of the flying entity being incorrect and I now need to figure out how to make the batch of drops go downward at random angles (to spread across the screen and make the player dodge the 'bad' drops and chase the 'good' drops) >.<

Edit: Corrected code above, while loop was not checking for the correct batch range.
Page 1 of 1
« first « previous next › last »