Game Development Reference
In-Depth Information
How to do it...
Perform the following steps to fire over a network:
1. To start off, we create a new message, called BulletUpdateMessage to send
updates on bullet positions. It only needs two fields: a Vector3f field for posi-
tion and a Boolean field for whether it's alive or not.
2. We'll add a check in the messageReceived method of ServerMes-
sageHandler to see whether a player is firing. Any action verification we want
to do should happen prior to this:
if(message.getAction().equals("Fire") &&
message.isPressed()){
server.onFire(p);
}
3. We find out the direction the player is facing and create a new ServerBullet
instance. It's assigned the next available object ID and added to the bullets list,
as follows:
public void onFire(NetworkedPlayerControl player){
Vector3f direction =
player.getSpatial().getWorldRotation().getRotationColumn(2);
direction.setY(-player.getYaw());
ServerBullet bullet = new
ServerBullet(player.getSpatial().getWorldTranslation().add(0,
1, 0), direction);
bullet.setId(nextObjectId++);
bullets.add(bullet);
}
4. Now, we need to add another code block to the simpleUpdate method to main-
tain the bullets and send out messages, as follows:
int nrOfBullets = bullets.size();
for(int i = 0; i < nrOfBullets; i++){
ServerBullet bullet = bullets.get(i);
bullet.update(tpf);
BulletUpdateMessage update = new
BulletUpdateMessage();
Search WWH ::




Custom Search