I edited Radnens shooting script so that the boundaries are limited by a variable bullet_distance
var bullets = [];
var bullet_num = 0; // how many bullets where shot
var bullet_distance = 64;
var bd_north,bd_east,bd_south,bd_west;
//Bullet is an Object with arguments
function Bullet(name, start_x, start_y, vel_x, vel_y)
{
this.name = name;
this.xVelocity = vel_x;
this.yVelocity = vel_y;
this.speed = 1; //general speed of bullet
CreatePerson(name, "bullet.rss", true); // name of this bullet;
SetPersonX(name, start_x);
SetPersonY(name, start_y);
}
//creates and positions bullet according to player direction
function CreateBullet(player)
{
var x = 0, y = 0;
switch(Direction) {
case "north": y = -1;bd_north = py - bullet_distance;break; //north -y
case "south": y = 1;bd_south = py + bullet_distance;break; //south +y
case "west": x = -1;bd_west = px - bullet_distance;break; //west -x
case "east": x = 1;bd_east = px + bullet_distance;break; //east x
}
bullet_num++;
//creat a new instance for Bullet(name, start_x, start_y, vel_x, vel_y) and store inside bullets array
bullets.push(new Bullet("bullet: " + bullet_num, GetPersonX(player) + x * 32, GetPersonY(player) + y * 32, x, y));
}
Bullet.prototype.update = function() { //whenever the game updates this method is run for Bullet object prototype
var x = GetPersonX(this.name) + this.xVelocity * this.speed;
var y = GetPersonY(this.name) + this.yVelocity * this.speed;
SetPersonX(this.name, x);
SetPersonY(this.name, y);
if ( y <= bd_north ||y >= bd_south ||x <= bd_west ||x >= bd_east) {
this.dead = true;
var obstruction = GetObstructingPerson(this.name, x, y);
if (obstruction != "") {
// obstruction = some entity by name
}
}
}
function UpdateBullets()
{
for (var i = 0; i < bullets.length; ++i) {
bullets[i].update();
if (bullets[i].dead) { DestroyPerson(bullets[i].name); bullets.splice(i, 1); i--; }
}
}
It works perfect and there are no problems UNTILL the player walks in the blue zone. The shooting system then glitches, the bullet creates itself and then imedtiatly destroys itself untill the player moves out of the blue zone and shots in another direction.
Here is the update script, I moved the UpdateBullets(); up but the glitch is still there.
var moving = false;
var px = GetPersonX(Player);
var py = GetPersonY(Player);
var Direction;
function Update()
{
//Update Global Variables
Direction = GetPersonDirection(Player);
moving = false;
px = GetPersonX(Player); //update and get player x position
py = GetPersonY(Player); //update and get player y position
//Movement
IsPersonObstructed(Player,px,py)
if (IsKeyPressed(KEY_UP)) {
QueuePersonCommand(Player, COMMAND_FACE_NORTH, true);
QueuePersonCommand(Player,COMMAND_MOVE_NORTH,false);
moving = true;
}
else if (IsKeyPressed(KEY_DOWN )) {
QueuePersonCommand(Player, COMMAND_FACE_SOUTH, true);
QueuePersonCommand(Player, COMMAND_MOVE_SOUTH, false);
moving = true;
}
else if (IsKeyPressed(KEY_LEFT)){
QueuePersonCommand(Player, COMMAND_FACE_WEST , true);
QueuePersonCommand(Player, COMMAND_MOVE_WEST , false);
moving = true;
}
else if (IsKeyPressed(KEY_RIGHT)) {
QueuePersonCommand(Player, COMMAND_FACE_EAST , true);
QueuePersonCommand(Player, COMMAND_MOVE_EAST , false);
moving = true;
}
//Bullet
UpdateBullets();
if (!IsKeyPressed(KEY_SPACE)) key_hit = false;
if (IsKeyPressed(KEY_SPACE) && key_hit == false)
{
key_hit = true;
CreateBullet(Player);
}
}
Is it possible to have a shooting system work in a wrapping map? I had a feeling that some co-ordinates could get mixed up while the player wraps around the map. The glitch if left in would help the player know when they are wrapping over a map but I would rather this not be the case so the player gets absolutely lost. I'm making a survival horror game (mwahahaha). Thanks for your support once again.