//for detecting enemies public Character(Sprite sprite, Body body, World world, CharacterDef charDef) : base(sprite, body, world) { _width = charDef.Width; _height = charDef.Height; _moveSpeed = charDef.MoveSpeed; //create surface sensor float xtmp = _width*CHAR_SENSOR_WIDTH_FACTOR*0.5f; float x1 = World.B2Value(-xtmp); float x2 = World.B2Value(xtmp); float ytmp = -_height*0.5f - 2.0f; float y1 = World.B2Value(ytmp - 1.0f); float y2 = World.B2Value(ytmp); var vertices = new Vector2[4]; vertices[0].X = x2; vertices[0].Y = y2; vertices[1].X = x1; vertices[1].Y = y2; vertices[2].X = x1; vertices[2].Y = y1; vertices[3].X = x2; vertices[3].Y = y1; _surfaceSense = AttachPolygonSensor(vertices); _surfaceSense.SensorName = Constants.SURFACE_SENSOR; //create visual sensor _visualSense = AttachCircleSensor(Vector2.Zero, _width*VISUAL_RANGE); _visualSense.SensorName = Constants.VISUAL_SENSOR; HP = charDef.HP; MaxHP = charDef.MaxHP; //Load animation if (charDef.FrameDefs != null) { for (int i = 0; i < charDef.FrameDefs.Length; i++) { FrameDef frameDef = charDef.FrameDefs[i]; var frame = new Frame(frameDef); frame.Target = sprite; _animations.Add(frame.Name, frame); } } //Load actions if (charDef.Actions != null) { for (int i = 0; i < charDef.Actions.Length; i++) { AttachAction(Activator.CreateInstance(Type.GetType(charDef.Actions[i])) as IAction); } } //Load sound effects if (charDef.SoundEffects != null) { foreach (var i in charDef.SoundEffects) { _soundEffects.Add(i.Key, AuditionManager.Instance.SoundEffects[i.Value]); } } //Load AI if (!string.IsNullOrEmpty(charDef.AI)) { _ai = new AI.AI(charDef.AI, this); } }
public Character CreateCharacter(CharacterDef charDef) { var bodydef = new BodyDef(); bodydef.type = charDef.BodyType; bodydef.angle = charDef.RotateZ; bodydef.fixedRotation = charDef.FixedRotation; bodydef.position.X = B2Value(charDef.X); bodydef.position.Y = B2Value(charDef.Y); Body body = _world.CreateBody(bodydef); var shape = new PolygonShape(); shape.SetAsBox(B2Value(charDef.Width*0.5f), B2Value(charDef.Height*0.5f)); var fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = charDef.Density; fixtureDef.friction = charDef.Fraction; fixtureDef.restitution = charDef.Restitution; body.CreateFixture(fixtureDef); var tex = XNADevicesManager.Instance.ContentManager.Load<Texture2D>(charDef.TexName); var sprite = new Sprite(tex); sprite.Alpha = charDef.Alpha; sprite.ScaleX = charDef.ScaleX; sprite.ScaleY = charDef.ScaleY; sprite.RotateX = charDef.RotateX; sprite.RotateY = charDef.RotateY; sprite.RotateZ = charDef.RotateZ; sprite.R = charDef.R; sprite.G = charDef.G; sprite.B = charDef.B; sprite.X = charDef.X; sprite.Y = charDef.Y; sprite.Z = charDef.Z; sprite.DrawRectangle = charDef.DrawRectangle; sprite.TransformOrigin = charDef.TransformOrigin; sprite.ZWriteEnable = charDef.ZWriteEnable; var character = new Character(sprite, body, this, charDef); character.Group = charDef.Group; character.Damage = charDef.Damage; character.HP = charDef.HP; character.MaxHP = charDef.MaxHP; body.SetUserData(character); return character; }