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 bitmapshades

I have an action RPG in development using Impact JS for the game engine with my own top down perspective control scheme like the Amiga classic - Chaos engine.

My question is where can I find code that will let the player go from walking mode to entering a vehicle and switching to driving mode like Grand Theft Auto?

I've tried lots of things, including trying to hide the player to simulate getting in the cat but the main issue stems from using this.getDistanceTo() in the player update() function to get the distance to the car when the use key is pressed. Iit is causing conflicts when the player presses the accelerate key.

1 decade ago by bitmapshades

I'm still banging my head against the wall with this. I'd love to know how to do this but I'm not there yet as you can see:

main.js
update: function() {
		//Controls
		this.controls();

		// Ambient SFX
		this.ambientSFX();

		// screen follows the player
		this.player = this.getEntityByName('player');
		if(this.player.visible) {
			this.screen.x = this.player.pos.x - ig.system.width/2;
			this.screen.y = this.player.pos.y - ig.system.height/2;
		}

		var car = this.getEntityByName('mycar');
		if(this.player.mode == 'drive'){
			this.screen.x = car.pos.x - ig.system.width/2;
			this.screen.y = car.pos.y - ig.system.height/2;
		}
//rest of code omitted

// player.js
update: function() {
      // Check game state for enabled actions
      if(ig.game.state == 'chat'){
          this.mode = 'chat';
          this.dx = 0;
          this.dy = 0;
          this.visible = false;
          //this.currentAnim = this.anims.idle;
      }
      else if(ig.game.state == 'respawn'){
          this.mode = 'chat';
          this.visible = true;
      }
      else if(ig.game.state == 'camera'){
          this.mode = 'chat';
          this.visible = false;
      }
      else if(ig.game.state == 'play'){
          if(this.mode == 'drive'){
              this.visible = false;
          }
          else{
              this.visible = true;
              if(ig.input.state('stealth') ) {
                  this.mode = 'stealth';
                  this.controls();
              }
              else if(ig.input.state('shoot') ){
                  this.mode = 'fire';

                  // Reload message
                  if(ig.game.stats.ammo <= 0){
                      this.combatText(1);
                  }
              }
              else {
                  this.mode = 'move';
                  this.controls();
              }
          }
      }
//bla blah code omitted

this.parent();
},

// car.js
    check: function( other ) {
		// If player is next to the car
		if (other instanceof EntityPlayer) {
			var player = ig.game.getEntityByName('player');
			player.mode = 'drive';
		}
	},

    // Accelerate
    speedup: function() {
		if(this.speed >= this.MAX_SPEED){
			this.speed = this.MAX_SPEED;
		}
		else{
			this.speed += this.ACCEL;
		}
    },

    // Brake
    brake: function() {
        if(this.speed > -10 ){
            this.speed -= this.BRAKE;
        }
    },

    // Steering
    steerLeft: function(amount) {
		if(this.steering > -this.MAX_STEER){
			this.steering += this.STEER_LEFT;
		}
		this.angle -= this.steering;
    },

    steerRight: function(amount) {
		if(this.steering < this.MAX_STEER){
			this.steering += this.STEER_RIGHT;
		}
		this.angle += this.steering;
    },

    update: function() {
		var player = ig.game.player;
		if(player.mode == 'drive'){
			if(ig.input.state('up') ){
				this.speedup();
				//this.currentAnim = this.anims.Accelerate;
			}
			else if(ig.input.state('down') ){
				this.brake();
				//this.currentAnim = this.anims.brake;
			}
			else {
				// Speed decay
				if(this.speed > 0){
					this.speed -= 10;
				}
				else{
					this.speed = 0;
				}
			}
			if(ig.input.state('left')){
				this.steerLeft();
			}
			else if(ig.input.state('right') ){
				this.steerRight();
			}

			// apply velocity by angle and speed
			this.vel.x = Math.cos(this.angle) * this.speed;
			this.vel.y = Math.sin(this.angle) * this.speed;
			this.currentAnim = this.anims.idle;
			this.currentAnim.angle = this.angle;
		}

        // Update display
        this.parent();
    }

1 decade ago by mimik

can't you just kill the player entity once its "in the car" and then spawn a new player when you get out?

1 decade ago by bitmapshades

Using kill() on the player entity will run the code I have in the kill() function to remove a life from player stats and handle respawns moving the player to the latest checkpoint.

1 decade ago by mimik

thats not good.
rename your custom kill function to i dont know Micke!
and use the regular kill.

or do a if statment in the kill function handeling the stuff differently.
or just set the camera on the car. then move the player out off screen.

1 decade ago by bitmapshades

Ok Mimik I followed your tip and renamed separated the kill code in the player entity to function die(). The problem is when I use player.kill() on a collision event it breaks several dependencies.

If I move the player off the screen the camera tracking will follow it and go off the map.

//car.js
check: function(other){
		if (other instanceof EntityPlayer) {
			ig.game.state = 'driving';
			var player = ig.game.getEntityByName('player');
			player.kill();
		}
	},

//Console errors
Uncaught TypeError: Cannot read property 'pos' of undefined main.js:263
ig.Game.extend.update main.js:263
(anonymous function) impact.js:448
ig.Game.ig.Class.extend.run game.js:170
ig.System.ig.Class.extend.run system.js:101
ig.System.inject.run menu.js:14
(anonymous function) impact.js:419
animate

1 decade ago by bitmapshades

I tried to switch between player movement states (using ig.input.released) but that didn't work. So I decided to bind right mouse for driving mode so that when the player releases the button they exit the car and go back to normal walking mode. For a mobile game I obviously need to find a solution to toggle different states.

// player.js update()
var car = ig.game.getEntityByName('car');
if(ig.input.state('drive') && this.distanceTo(car) < this.speed ){
    this.mode = 'driving';
    car.debug();
}
if(ig.input.released('drive')){
    this.pos.x = car.pos.x + this.speed;
    this.pos.y = car.pos.y - this.speed/2;
}

if(this.mode == 'driving'){
    this.visible = false;
    if(ig.input.state('up') ){
        // get movement vector
        car.speed += car.accel;
    }
    else if(ig.input.state('down') ){
        car.speed -= car.decel;
    }
    // apply velocity
    car.vel.x = car.dx * car.speed;
    car.vel.y = car.dy * car.speed;
    car.currentAnim.angle = car.angle;
    }
else {
    this.visible = true;
    this.mode = 'move';
    this.controls();
}

1 decade ago by mimik

hm I see.
How about resolving with some basic Boolean triggers?

if player is walking. set walking to true.
then you can resolve you different states within the same function.
: Cannot read property 'pos' of undefined

something is looking for iguess player pos and when player is dead you get an error.

something like:

if(ig.input.state('drive') && this.distanceTo(car) < this.speed && this.walking ){
   this.walking = false;
    // Wrooooom

}

1 decade ago by bitmapshades

That is why I have the player mode property so I can see in my debug panel what mode the player is in. It' s the same as saying:

if(ig.input.state('use') && this.distanceTo(car) < this.speed && this.mode == 'walking' ){
   this.mode = 'driving';
    // Wrooooom
}

if(ig.input.state('use') && this.distanceTo(car) < this.speed && this.mode == 'driving' ){
   this.mode = 'walking';
    // Walk
}

1 decade ago by bitmapshades

Except in both cases the code constantly switches the player between the two states. Hence the reason I had to force the player to hold another key down just to stay in the car! Not ideal.

1 decade ago by bitmapshades

Alright I did it! Here's some screen grabs..

https://docs.google.com/file/d/0B3wdI8qgt5cIQWNNWEh0VGEtNXM/edit
https://docs.google.com/file/d/0B3wdI8qgt5cIUUdOcTFNYzEyT00/edit

1 decade ago by mimik

ah great!
if it constantly switches it sounds like the update funtion update on same keypress.
ig.input.state('use')

so whenever you have the key down. its going to fire the event.

you could change to ig.input.released

1 decade ago by bitmapshades

That's exactly what I have done with a few improvements. so the player entity moves with the car entity solving the problem of getting kicked out anytime the car hit top speed.
Page 1 of 1
« first « previous next › last »