Page 2 of 2
Posted: 08 Jan 2011, 23:45
by GRIM2594
Robbilie wrote:shit i knew that i forgot something....
Tomorrow ill post my code k?
U dont need npcs in anyway the just make it more complicated than it is possible...
Thanks Robbilie. I would love to see if your coding technique would be a workable example that I could study.
Posted: 09 Jan 2011, 13:03
by Robbilie
Mob.class
package dk.fullcontrol.fps.simulation;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
// Item class describes an item on the scene
// In this example project it represents either ammo crate or health pack
public class Mob {
public static final double touchDistance = 5; // How close must the player approach to the item to take it
public int id; // The unique id of the item
private Transform transform; // The position and rotation of the item in the world
public MobType mobType; // The type of the item (ammo or healthpack)
public int health = 100;
public int maxHealth = 100;
public int moblvl;
public int giveexp;
public boolean isAttacking = false;
public Transform playerTransform;
public Mob(int id, Transform transform, MobType mobType, int maxHealth, int health, int moblvl, int giveexp) {
this.id = id;
this.transform = transform;
this.mobType = mobType;
this.maxHealth = maxHealth;
this.health = health;
this.moblvl = moblvl;
this.giveexp = giveexp;
}
public int getId() {
return id;
}
public MobType getMobType() {
return mobType;
}
public int getHealth() {
return health;
}
public int getMoblvl() {
return moblvl;
}
public int getMobexp() {
return giveexp;
}
public int getMaxHealth() {
return maxHealth;
}
public Transform getTransform() {
return transform;
}
public void removeHealth(int count) {
health -= count;
}
public boolean isDead() {
return health <= 0;
}
// Put the item info to JSON object to send it to client
public void toSFSObject(ISFSObject data) {
ISFSObject mobData = new SFSObject();
mobData.putInt("id", id);
mobData.putUtfString("type", mobType.toString());
this.transform.toSFSObject(mobData);
mobData.putInt("maxHealth",maxHealth);
mobData.putInt("health",health);
mobData.putInt("moblvl",moblvl);
mobData.putInt("mobgivesexp",giveexp);
data.putSFSObject("mob", mobData);
}
public boolean isClose(Transform newTransform) {
double d = newTransform.distanceTo(this.transform);
return (d <= touchDistance);
}
public boolean isAttacking() {
return isAttacking;
}
public void setPlayerTransform(Transform playerTrans) {
playerTransform = playerTrans;
}
public Transform getPlayerTransform() {
return playerTransform;
}
}
Posted: 09 Jan 2011, 13:05
by Robbilie
put that into world.class
private static final int maxSpawnedMobs = 25; // Maximum mobs that can be spawned at once
private static final int maxMobsOfSingleType = 40; // Maximum items of the particular type that can be present on the scene
private int mobId = 0;
// Mobs
private List<Mob> mobs = new ArrayList<Mob>();
public List<Mob> getMobs() {
return mobs;
}
// Spawning new items
public void spawnMobs() {
int mobsCount = rnd.nextInt(maxSpawnedMobs);
extension.trace("Spawn " + mobsCount + " mobs.");
for (int i = 0; i < mobsCount; i++) {
int randomlvl = rnd.nextInt(100);
double expo = Math.pow(1.164,randomlvl);
double mobexp = 100*expo*0.3;
int monsterexp = (int) mobexp;
Mob mob = new Mob(mobId++, Transform.randomWorld(), MobType.Mob,100,100,randomlvl,monsterexp);
mobs.add(mob);
extension.clientInstantiateMob(mob);
}
}
MobType.class
package dk.fullcontrol.fps.simulation;
public enum MobType {
Mob
}
Posted: 09 Jan 2011, 13:08
by Robbilie
there are many things i missed i think....
heres the client code
networkmanager.cs
replace:
// This method handles all the responses from the server
private void OnExtensionResponse(BaseEvent evt) {
try {
string cmd = (string)evt.Params["cmd"];
ISFSObject dt = (SFSObject)evt.Params["params"];
if (cmd == "spawnPlayer") {
HandleInstantiatePlayer(dt);
}
else if (cmd == "transform") {
HandleTransform(dt);
}
else if (cmd == "mobtransform") {
HandleMobTransform(dt);
}
else if (cmd == "notransform") {
HandleNoTransform(dt);
}
else if (cmd == "killed") {
HandleKill(dt);
}
else if (cmd == "mobkilled") {
HandleMobKill(dt);
}
else if (cmd == "health") {
HandleHealthChange(dt);
}
else if (cmd == "mobhealth") {
HandleMobHealthChange(dt);
}
else if (cmd == "anim") {
HandleAnimation(dt);
}
else if (cmd == "level") {
HandleLevelChange(dt);
}
else if (cmd == "chars") {
HandleChars(dt);
}
else if (cmd == "charselected") {
HandleSelect(dt);
}
else if (cmd == "score") {
HandleScoreChange(dt);
}
else if (cmd == "ammo") {
HandleAmmoCountChange(dt);
}
else if (cmd == "spawnItem") {
HandleItem(dt);
}
else if (cmd == "spawnMob") {
HandleMob(dt);
}
else if (cmd == "removeItem") {
HandleRemoveItem(dt);
}
else if (cmd == "enemyShotFired") {
HandleShotFired(dt);
}
else if (cmd == "time") {
HandleServerTime(dt);
}
else if (cmd=="reloaded") {
HandleReload(dt);
}
}
catch (Exception e) {
Debug.Log("Exception handling response: "+e.Message+" >>> "+e.StackTrace);
}
}
add:
/// <summary>
/// Send local transform to the server
/// </summary>
/// <param name="ntransform">
/// A <see cref="NetworkTransform"/>
/// </param>
public void SendMobTransform(NetworkTransform ntransform) {
Room room = smartFox.LastJoinedRoom;
ISFSObject data = new SFSObject();
ntransform.ToSFSObject(data);
ExtensionRequest request = new ExtensionRequest("sendMobTransform", data, room, true); // True flag = UDP
smartFox.Send(request);
}
// Updating transform of the remote player from server
private void HandleMobTransform(ISFSObject dt) {
int mobId = dt.GetInt("mobid");
NetworkTransform ntransform = NetworkTransform.FromSFSObject(dt);
PlayerManager.Instance.MoveMob(mobId,dt);
}
// Handle player killed
private void HandleMobKill(ISFSObject dt) {
int userId = dt.GetInt("id");
int killerId = dt.GetInt("killerId");
PlayerManager.Instance.KillMob(userId);
if (killerId == smartFox.MySelf.Id) {
SoundManager.Instance.PlayKillEnemy(PlayerManager.Instance.GetPlayerObject().audio);
}
}
// Health of the player changed - updating GUI and playing sounds if it's damage
private void HandleMobHealthChange(ISFSObject dt) {
int userId = dt.GetInt("mobid");
int health = dt.GetInt("mobhealth");
PlayerManager.Instance.UpdateHealthForMob(userId, health);
}
// New item spawned message. Instantiating the item object.
private void HandleMob(ISFSObject dt) {
ISFSObject mob = dt.GetSFSObject("mob");
int id = mob.GetInt("id");
string mobType = mob.GetUtfString("type");
NetworkTransform ntransform = NetworkTransform.FromSFSObject(mob);
int maxHealth = mob.GetInt("maxHealth");
int health = mob.GetInt("health");
int moblvl = mob.GetInt("moblvl");
int mobexp = mob.GetInt("mobgivesexp");
Debug.Log("received mob");
PlayerManager.Instance.SpawnMob(id, ntransform, mobType, maxHealth, health, moblvl, mobexp);
}
Posted: 09 Jan 2011, 13:10
by Robbilie
playermanager.cs
add
public GameObject mobPrefab;
private Dictionary<int, GameObject> mobs = new Dictionary<int, GameObject>();
private Dictionary<int, int> mobsmaxhealth = new Dictionary<int, int>();
private Dictionary<int, int> mobshealth = new Dictionary<int, int>();
private Dictionary<int, int> mobslvl = new Dictionary<int, int>();
private Dictionary<int, int> mobsexp = new Dictionary<int, int>();
public int returnMobID(GameObject go ){
for(int id = 0; id < mobs.Count; id++){
if (mobs[id] == go) {
return id;
}
}
return 1-2;
}
public void MoveMob(int id, ISFSObject data) {
//Transform test = new Transform();
Quaternion test = new Quaternion();
ISFSObject transformData = data.GetSFSObject("transform");
float x = Convert.ToSingle(transformData.GetDouble("x"));
float y = Convert.ToSingle(transformData.GetDouble("y"));
float z = Convert.ToSingle(transformData.GetDouble("z"));
float rx = Convert.ToSingle(transformData.GetDouble("rx"));
float ry = Convert.ToSingle(transformData.GetDouble("ry"));
float rz = Convert.ToSingle(transformData.GetDouble("rz"));
mobs[id].transform.position = new Vector3(x, y, z);
test.x = rx;
test.y = ry;
test.z = rz;
mobs[id].transform.rotation = test;// new Quaternion(rx, ry, rz);
}
public void SpawnMob(int id, NetworkTransform ntransform, string mobType, int maxHealth, int health, int moblvl, int mobgivesexp) {
GameObject monsterPrefab = null;
if (mobType == "Mob") {
monsterPrefab = mobPrefab;
}
else {
}
GameObject mobObj = GameObject.Instantiate(monsterPrefab) as GameObject;
mobObj.transform.position = ntransform.Position;
mobObj.transform.localEulerAngles = ntransform.AngleRotationFPS;
String textm = mobType.ToString();
mobObj.GetComponentInChildren<TextMesh>().text = textm + " [" + moblvl + "]";
mobs[id] = mobObj;
mobsmaxhealth[id] = maxHealth;
mobshealth[id] = health;
mobslvl[id] = moblvl;
mobsexp[id] = mobgivesexp;
}
public void RemoveMob(int id) {
if (mobs.ContainsKey(id)) {
Destroy(mobs[id]);
mobs.Remove(id);
}
if (mobsmaxhealth.ContainsKey(id)) {
//Destroy(mobsmaxhealth[id]);
mobsmaxhealth.Remove(id);
}
if (mobshealth.ContainsKey(id)) {
//Destroy(mobshealth[id]);
mobshealth.Remove(id);
}
if (mobslvl.ContainsKey(id)) {
//Destroy(mobsmaxhealth[id]);
mobslvl.Remove(id);
}
if (mobsexp.ContainsKey(id)) {
//Destroy(mobshealth[id]);
mobsexp.Remove(id);
}
}
public void UpdateHealthForMob(int id, int health) {
Debug.Log(health);
mobs[id].GetComponent<Mob>().UpdateHealth(health);
BloodEffect(mobs[id].transform);
}
public void KillMob(int id) {
GameObject obj = mobs[id];
BloodEffect (obj.transform);
GameObject hero = obj.transform.FindChild("Hero").gameObject;
hero.transform.parent = null;
Destroy(obj);
hero.transform.Rotate(Vector3.right*90);
hero.animation.Stop();
Destroy(hero, 10);
mobs.Remove(id);
}
Posted: 09 Jan 2011, 13:11
by Robbilie
the mobprefab is the enemy character
siymply drag it in the slot and the healthbar will update and name too...
errors?
pls ask
PS: sry i forgot that there is a code function

Posted: 09 Jan 2011, 13:13
by Robbilie
ah shit
put that into the fpsextension.clss
// Send message to client when a player is killed
public void clientKillMob(Mob pl, CombatPlayer killerPl) {
ISFSObject data = new SFSObject();
data.putInt("id", pl.getId());
data.putInt("killerId", killerPl.getSfsUser().getId());
trace("mobkilled");
Room currentRoom = RoomHelper.getCurrentRoom(this);
List<User> userList = UserHelper.getRecipientsList(currentRoom);
this.send("mobkilled", data, userList);
}
public void clientMobTransformUpdate(Mob mob, Transform mobtrans) {
sendMobTransform(mob, mobtrans);
}
// Send the transform to all the clients
public void sendMobTransform(Mob mob, Transform mobtrans) {
ISFSObject data = new SFSObject();
// Adding server timestamp to transform here
long time = new Date().getTime();
mobtrans.setTimeStamp(time);
mobtrans.toSFSObject(data);
data.putInt("mobid", mob.getId());
Room currentRoom = RoomHelper.getCurrentRoom(this);
List<User> userList = UserHelper.getRecipientsList(currentRoom);
this.send("mobtransform", data, userList);
}
// Send message to clients when the health value of a player is updated
public void clientUpdateMobHealth(Mob pl) {
ISFSObject data = new SFSObject();
trace(pl.getHealth());
data.putInt("mobid", pl.getId());
data.putInt("mobhealth", pl.getHealth());
Room currentRoom = RoomHelper.getCurrentRoom(this);
List<User> userList = UserHelper.getRecipientsList(currentRoom);
this.send("mobhealth", data, userList);
}
Posted: 09 Jan 2011, 23:54
by GRIM2594
Thank you, Robbilie for your comments. I am currently working to impliment my own NPC class from a few thoughts that came from this forum post.
I am going to set mine up somewhat like the CombatPlayers arraylist, in the FPS tutorial, with many modifications. I haven't finished yet, but my logic theory should work.
This is a general idea on how I am setting it up, if anyone would like to know:
- Server starts up
- TaskScheduler starts
- - Task A) (run once) Server loads all NPCs from database to ArrayList.
- - Task B) - (only runs when player count > 0) check NPC's for deaths, spawn new NPCs when ready
- - Task C) - (only runs when player count > 0) send new spawn data to clients
- Player connects, then sends notification to server
- - A) - Server sends NPCs info to client.
That's a general idea of how I am setting it up. (I wont discuss combat, since that wasn't my original topic.)
Posted: 10 Jan 2011, 04:41
by Robbilie
thats what my code mainly does
The mobs hav their own health, lvl and exp
Only the movement goes through a task but it doesnt wkrk right (the mobs move up, down, right left...)
and thedes no respawn...
My mobs will b loaded from db too but not every single but the types, count, bounds of area.,.
Can u post ur code when u got the moving corectly?
Posted: 10 Jan 2011, 21:51
by GRIM2594
Robbilie wrote:Can u post ur code when u got the moving corectly?
Sure, I may be able to post something that can help you. It may take a few days though, since I haven't figured out my exact path that I will take for synchronization with clients.
Right now, I am using the Unity3D client to find target direction/location, routes, and way-points around structures; but I intend on doing it a different way soon.
I will get to it after I finish all of the finishing touches up on the other aspects of NPCs (I am almost finished, hopefully by tomorrow).
Posted: 26 Mar 2011, 17:19
by egonator
Hey Robbilie, thanks for posting your code.
I see you have some code in the World.class file that deals with moving the mob (clientMobTransformUpdate) however it is never used. The mobs dont move when I load the game.
Did you manage to work out how to control the mobs movement completely on server side? Possible patrolling in intervals?
Need to also implement enemy ai so they attack the players, again this should be done server side but im not sure how they can be triggered.
Posted: 26 Mar 2011, 23:45
by Robbilie
yes back then i removed it cause it didnt work
Long time ago...
Few weeks ago i got it working (in parts) that the mob gets aggressive to player and follows...
Didnt work correctly and the mob just moves on x and z axis... Im trying to implement some collider data read in java to get height...
Post my code if its done..
Btw: new to the forum?
Wanna join my team?