package org.jslaughter.component.weapon;
import org.jslaughter.component.action.WeaponAction;
import org.jslaughter.component.physics.motion.BulletMotion;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Vector2f;
import org.nvframe.camera.Camera;
import org.nvframe.component.state.Position;
import org.nvframe.entity.Entity;
import org.nvframe.event.EventService;
import org.nvframe.event.eventtype.SoundEvent;
import org.nvframe.event.eventtype.UpdateEvent;
import org.nvframe.exception.NVFrameException;
import org.nvframe.factory.EntityFactory;
import org.nvframe.manager.ConfigManager;
import org.nvframe.manager.InputManager;
import org.nvframe.util.settings.SettingsObj;
/**
*
* @author Nik Van Looy
*/
public class PistolAction extends WeaponAction {
public PistolAction(String id, Entity owner, SettingsObj settings) throws NVFrameException {
super(id, owner, settings);
}
@Override
public void onUpdate(UpdateEvent event) {
GameContainer gc = event.getGc();
int delta = event.getDelta();
Input input = gc.getInput();
shotTimerMs += delta;
if(input.isKeyPressed(InputManager.getInstance().getInput("player_interaction_reload"))) {
reloading = true;
currReloadTimeMs = 0;
// play reload sound
EventService.getInstance().fireEvent(new SoundEvent(reloadSound));
return;
}
if(reloading) {
currReloadTimeMs += delta;
if(currReloadTimeMs > reloadTimeMs) {
remainingCap = magazineCap;
reloading = false;
currReloadTimeMs = 0;
}
return; // can't shoot while reloading the pistol
}
if(input.isMousePressed(InputManager.getInstance().getInput("player_interaction_primaryfire"))) {
if(shotTimerMs < shotDelayMs || remainingCap <= 0)
return;
Vector2f targetCoords = new Vector2f(Camera.getInstance().getMouseX(input), Camera.getInstance().getMouseY(input));
try {
// create the bullet entity
Entity bullet = EntityFactory.getInstance().getEntityFromPrototype(bulletId);
bullet.setOwner(owner);
Position position = (Position) owner.getComponent(Position.class);
BulletMotion entityMotionComp = (BulletMotion) bullet.getComponent(BulletMotion.class);
entityMotionComp.fire(position.getXY().copy(), targetCoords.copy());
remainingCap--;
if(ConfigManager.getInstance().getActive("debugMode"))
System.out.println("remainingCap: " + remainingCap);
// play pistol gunshot sound
if(shotSound != null)
EventService.getInstance().fireEvent(new SoundEvent(shotSound));
}
catch(NVFrameException e) {
e.printStackTrace();
}
if(remainingCap == 0) {
reloading = true;
currReloadTimeMs = 0;
// play reload sound
if(reloadSound != null)
EventService.getInstance().fireEvent(new SoundEvent(reloadSound));
}
else
shotTimerMs = 0;
}
}
}
|