Ejemplo n.º 1
0
        //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);
            }
        }
Ejemplo n.º 2
0
 public override Animation ToAnimation(object data)
 {
     var frame = new Frame(this);
     frame.Target = data as Sprite;
     return frame;
 }
        private void LoadEnemies()
        {
            _enemies = new Collection<Character>();

            for (int i = 0; i < 30; i++)
            {
                Character enemy;
                int rand = RandomHelper.NextInt(0, 3);
                if (rand == 1)
                {
                    float size = RandomHelper.NextFloat(1.0f, 1.5f);
                    enemy = CreateCharacter(@"Data\Dan.xml", Group.PlayerTwo, RandomHelper.NextFloat(-8000, 9500), 200,
                        100, 5, 120, 400, size, size, RandomHelper.NextFloat(0.9f, 1.3f),
                        RandomHelper.NextFloat(0.5f, 1.5f), RandomHelper.NextFloat(0.9f, 1.3f));
                    enemy.Actions["Attack"].ActionStart += sender =>
                    {
                        var timer = new Timer(500);
                        timer.EventData = sender.Character;
                        timer.OnTimer += timerSender =>
                        {
                            var dan = (Character) timerSender.EventData;
                            if (dan.Actions["Attack"].IsDoing)
                            {
                                var sprite = new Sprite(_soundTex);
                                sprite.DrawRectangle = new Rectangle(0, 0, _soundTex.Width, _soundTex.Height);
                                sprite.Position = dan.Sprite.Position;
                                sprite.ScaleX = dan.Sprite.ScaleX;
                                sprite.ScaleY = dan.Sprite.ScaleY;
                                sprite.R = dan.Sprite.R;
                                sprite.G = dan.Sprite.G;
                                sprite.B = dan.Sprite.B;
                                sprite.Z += 2.0f;
                                Root.Add(sprite);
                                var soundFrame = new Frame(_soundFrameDef);
                                soundFrame.Speed = 2.0f;
                                soundFrame.OnEnd += frameSender =>
                                {
                                    var frame = (Frame) frameSender;
                                    frame.Target.Parent.Remove(frame.Target);
                                };
                                soundFrame.Target = sprite;
                                soundFrame.Start();
                            }
                        };
                        timer.Start();
                    };
                }
                else if (rand == 2)
                {
                    float size = RandomHelper.NextFloat(1.0f, 1.5f);
                    enemy = CreateCharacter(@"Data\Dan1.xml", Group.PlayerTwo, RandomHelper.NextFloat(-8000, 9500), 200,
                        100, 5, 150, 500, size, size, RandomHelper.NextFloat(0.9f, 1.3f),
                        RandomHelper.NextFloat(0.5f, 1.5f), RandomHelper.NextFloat(0.9f, 1.3f));
                }
                else //if (rand == 3)
                {
                    float size = RandomHelper.NextFloat(1.0f, 1.5f);
                    enemy = CreateCharacter(@"Data\Hua.xml", Group.PlayerTwo, RandomHelper.NextFloat(-8000, 9500), 200,
                        100, 5, 100, 450, size, size, RandomHelper.NextFloat(-0.1f, 0.3f),
                        RandomHelper.NextFloat(0.5f, 1.5f), RandomHelper.NextFloat(0.9f, 1.3f));
                }

                enemy.AI.LoadAIScript(@"AI\Enemy.py");
                _enemies.Add(enemy);

                Root.Add(enemy.Sprite);
                EventManager.Register(enemy.AI);
            }
        }