Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enemies know gets knockbacked which looks really nice

  • Loading branch information...
commit 4c192be0d541745f93ea05e0ab16daed10f592f5 1 parent b2806d5
@WASDi authored
View
2  src/mygame/controls/PlayerControl.java
@@ -208,7 +208,7 @@ private void meleeAttack(){
Npc enemy = it.next();
if(pos.distance(enemy.getPosition())<5f){
Vector3f dir = enemy.getPosition().subtract(pos).normalizeLocal();
- enemy.onAttack(5, dir);
+ enemy.onAttack(5, dir, this);
}
}
//TODO do hit animation.
View
4 src/mygame/npc/Npc.java
@@ -3,6 +3,7 @@
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
+import mygame.controls.PlayerControl;
import mygame.quest.Quest;
/**
@@ -60,7 +61,8 @@
* Called by Player when he does an attack on the Npc
* @param dmg How much health the Npc lost
* @param direction Which direction the hit came from to calculate knockback
+ * @param player The player which did the attack
*/
- public void onAttack(int dmg, Vector3f direction);
+ public void onAttack(int dmg, Vector3f direction, PlayerControl player);
}
View
85 src/mygame/npc/PhysicNpc.java
@@ -3,10 +3,10 @@
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.math.FastMath;
-import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
+import mygame.controls.PlayerControl;
import mygame.quest.Quest;
/**
@@ -18,7 +18,7 @@
private Node node;
private Vector3f arrowTranslation;
private float spawnx, spawnz; //the x and y coordinate where the Npc is spawned
- private Vector2f walkTo;
+ private Vector3f walkTo;
private Vector3f walkDir = new Vector3f();
private final float maxWalkDistance = 20f;
private long nextWalk;
@@ -28,6 +28,7 @@
private Vector3f knockback = null;
private float knockTime = 0f;
+ private PlayerControl attacker;
public PhysicNpc(float sizex, float sizey, Node node, int maxHp) {
super(new CapsuleCollisionShape(sizex, sizey), .1f);
@@ -38,9 +39,16 @@ public PhysicNpc(float sizex, float sizey, Node node, int maxHp) {
}
public String talk() {
- //Enemies get angry when you talk to them
- attackPlayer();
- return "Die!";
+ if(attacker != null){
+ //You have attacked this enemy before and he gets angry when you talk to him
+ enrage(attacker, false);
+ return "Die!";
+ }
+ else{
+ //Enemy doesn't know about you
+ return "Hmm?";
+ }
+
}
public Vector3f getPosition() {
@@ -89,33 +97,37 @@ public void update(float tpf) {
//The default behaviour is that they should walk toward a nearby point and stop for a while
//Then pick a new random point while making sure not to walk too far away from the start location
- if(enraged){
+ if(enraged && attacker!=null){
if(knockback!=null){
- knockTime+=tpf;
+ knockTime+=tpf*1.5f;
if(knockTime>1){
knockback = null;
- knockTime = 0f;
}
else{
float percent = 1-knockTime;
- percent*=.3f;
+ percent*=.4f;
setWalkDirection(knockback.mult(percent));
}
}
else{
//walk towards player
- //TODO IMPORTANT set walkTo to player positioon
+ walkTo = attacker.getPhysicsLocation();
}
+ //TODO stop being enraged if moving too far from spawn
+ //TODO possibly call nearby enemies to join the attack against the player
}
- else if(walkTo!=null){
+
+ if(walkTo!=null){
//walk towards walkTo
walkDir.set(walkTo.x-getPhysicsLocation().x, 0,
- walkTo.y-getPhysicsLocation().z).normalizeLocal().multLocal(.1f);
+ walkTo.z-getPhysicsLocation().z).normalizeLocal().multLocal(.1f);
setWalkDirection(walkDir);
- setViewDirection(walkDir);
+ if(knockback==null)
+ setViewDirection(walkDir);
- if(FastMath.abs(getPhysicsLocation().x-walkTo.x)<1f && FastMath.abs(getPhysicsLocation().z-walkTo.y)<1f){
- //finished walking
+ if(!enraged && FastMath.abs(getPhysicsLocation().x-walkTo.x)<1f &&
+ FastMath.abs(getPhysicsLocation().z-walkTo.z)<1f){
+ //finished walking, ignores y-axis
prepareNextWalk();
}
}
@@ -126,11 +138,11 @@ else if (System.currentTimeMillis() > nextWalk){
super.update(tpf);
}
- private Vector2f getPointNearSpawn(){
- Vector2f point = new Vector2f(FastMath.rand.nextFloat(), FastMath.rand.nextFloat());
+ private Vector3f getPointNearSpawn(){
+ Vector3f point = new Vector3f(FastMath.rand.nextFloat(), 0, FastMath.rand.nextFloat());
point.normalizeLocal();
- point.set(spawnx+point.x*maxWalkDistance*FastMath.rand.nextFloat(),
- spawnz+point.y*maxWalkDistance*FastMath.rand.nextFloat());
+ point.set(spawnx+point.x*maxWalkDistance*FastMath.rand.nextFloat(), 0,
+ spawnz+point.z*maxWalkDistance*FastMath.rand.nextFloat());
return point;
}
@@ -139,11 +151,10 @@ private Vector2f getPointNearSpawn(){
* at least 'maxWalkDistance/2' from where the player currently is
* @return A Vector2f with the x,z coordinates
*/
- private Vector2f getWalkPoint(){
- Vector2f pos = new Vector2f(getPhysicsLocation().x, getPhysicsLocation().z);
+ private Vector3f getWalkPoint(){
while(true){
- Vector2f point = getPointNearSpawn();
- if(point.distance(pos)>maxWalkDistance/2)
+ Vector3f point = getPointNearSpawn();
+ if(getPhysicsLocation().distance(point)>maxWalkDistance/2)
return point;
}
}
@@ -156,17 +167,29 @@ private void prepareNextWalk(){
setWalkDirection(walkDir);
}
- public void onAttack(int dmg, Vector3f direction) {
- //TODO lose health
- attackPlayer();
- jump();
+ public void onAttack(int dmg, Vector3f direction, PlayerControl player) {
+ hp-=dmg;
+ if(hp<=0){
+ //TODO die
+ }
knockback = direction;
+ knockTime = 0f;
+ enrage(player, true);
+ jump();
}
-
- private void attackPlayer() {
+
+ /**
+ * Makes the enemy angry
+ * @param player The attacker
+ * @param hadKnockback If the player got knockbacked it will cancel current movement
+ */
+ private void enrage(PlayerControl player, boolean hadKnockback){
+ attacker = player;
enraged = true;
- //TODO move towards player and do animations and stuff.
- //TODO possibly call nearby enemies to also attack.
+ if(hadKnockback){
+ //walkTo gets calculated later in the loop
+ walkTo = null;
+ }
}
}
View
3  src/mygame/npc/StaticNpc.java
@@ -9,6 +9,7 @@
import com.jme3.scene.control.Control;
import java.util.ArrayList;
import java.util.List;
+import mygame.controls.PlayerControl;
import mygame.quest.Quest;
/**
@@ -126,7 +127,7 @@ public void setSay(String say) {
this.say=say;
}
- public void onAttack(int dmg, Vector3f direction) {
+ public void onAttack(int dmg, Vector3f direction, PlayerControl player) {
//do nothing, StaticNpc can't be hit for now.
}

0 comments on commit 4c192be

Please sign in to comment.
Something went wrong with that request. Please try again.