Esempio n. 1
0
        private void spawnCop()
        {
            int randNum = Game1.random.Next(map.nodes.Count);
            Node randNode = map.nodes.ElementAt(randNum);
            while (Camera.X - Camera.Width / 2 < randNode.Position.X &&
                   Camera.X + Camera.Width / 2 > randNode.Position.X &&
                   Camera.Y - Camera.Height / 2 < randNode.Position.Y &&
                   Camera.Y + Camera.Height / 2 > randNode.Position.Y)
            {
                randNum = Game1.random.Next(map.nodes.Count);
                randNode = map.nodes.ElementAt(randNum);
            }
            double randFloat = Game1.random.NextDouble();
            randNum = Game1.random.Next(2);
            
            NPC newNpc;

            if (randFloat <= dumbCopThreshold)
            {
                Animation animation;
                DumbCopState dcState;
                if (randNum == 1)
                {
                    dcState = DumbCopState.STATIC;
                    animation = SpriteDatabase.GetAnimation("cop_static");

                }
                else
                {
                    dcState = DumbCopState.WANDER;
                    animation = SpriteDatabase.GetAnimation("cop_walk");
                }
                newNpc = new DumbCop(
                    new PhysicsComponent2D(new Vector2(randNode.Position.X, randNode.Position.Y), 0, new Vector2(20, 20), Speeds.DumbCop_Run, 750, Speeds.DumbCop_Walk, 1000, 8, 50, 0.25f, true),
                    new MovementAIComponent2D(),
                    new DrawComponent(animation, Color.White, Vector2.Zero, new Vector2(.4f, .4f), .5f), dcState);
            }
            else if (randFloat <= smartCopThreshold+dumbCopThreshold)
            {
                Animation animation;
                SmartCopState scState;
                if (randNum == 1)
                {
                    scState = SmartCopState.STATIC;
                    animation = SpriteDatabase.GetAnimation("smartCop_static");

                }
                else
                {
                    scState = SmartCopState.WANDER;
                    animation = SpriteDatabase.GetAnimation("smartCop_walk");
                }
                newNpc = new SmartCop(
                                new PhysicsComponent2D(new Vector2(randNode.Position.X, randNode.Position.Y), 0, new Vector2(20, 20),
                                    Speeds.SmartCop_Run, 750, Speeds.SmartCop_Walk, 1000, 8, 50, 0.25f, true),
                                new MovementAIComponent2D(3, 2, MathHelper.ToRadians(45), 0.5f, 50, 25, Vector2.Zero, Vector2.Zero, 0.1f),
                                new DrawComponent(animation, Color.White, Vector2.Zero,
                                    new Vector2(.4f, .4f), .5f), scState);
            }
            else
            {
                newNpc = new RoboCop(
                            new PhysicsComponent2D(new Vector2(randNode.Position.X, randNode.Position.Y), 0, new Vector2(20, 20), Speeds.RoboCop_Run, 750, 75, 1000, 8, 50, 0.25f, true),
                            new MovementAIComponent2D(),
                            new DrawComponent(SpriteDatabase.GetAnimation("roboCop_static"), Color.White,
                                              Vector2.Zero, new Vector2(.4f, .4f), .5f));
            }
            
            npcs.Add(newNpc);
            moveableObjectsX.Add(newNpc);
            moveableObjectsY.Add(newNpc);
            moveableObjectsX = moveableObjectsX.OrderBy(o => o.ComponentPhysics.Position.X).ToList();
            moveableObjectsY = moveableObjectsY.OrderBy(o => o.ComponentPhysics.Position.Y).ToList();
            if (newNpc is SmartCop)
            {
                smartFlock.Members.Add(newNpc);
                newNpc.flock = smartFlock;
            }
            if (newNpc is DumbCop)
            {
                dumbFlock.Members.Add(newNpc);
                newNpc.flock = dumbFlock;
            }
            spawnCount++;
            updateSpawnData();
        }
Esempio n. 2
0
        public void Load(string filename)
        {
            walls.Clear();
            nodes.Clear();
            startingNPCs.Clear();
            consumables.Clear();
            playerStart = new Vector2();

            StreamReader reader = new StreamReader(filename);
            try
            {
                string line = reader.ReadLine();
                if (line.StartsWith("RECTANGLES"))
                    line = reader.ReadLine();
                do
                {
                    int x, y, w, h;
                    bool seeThrough;
                    int spacei = line.IndexOf(' ');
                    x = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    y = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    w = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    h = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    seeThrough = bool.Parse(line);
                    if (w > 0 && h > 0)
                        walls.Add(new Wall(new Vector2(x,y), new BoundingRectangle(x, y, w, h), seeThrough));
                    line = reader.ReadLine();
                } while (reader.Peek() != -1
                    && !line.StartsWith("NODES")
                    && !line.StartsWith("EDGES")
                    && !line.StartsWith("NPCS")
                    && !line.StartsWith("TRIGGER")
                    && !line.StartsWith("PLAYER")
                    );

                if (line.StartsWith("NODES"))
                    line = reader.ReadLine();
                do
                {
                    int id, x, y;
                    bool key;
                    int spacei = line.IndexOf(' ');
                    id = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    x = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    y = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    key = bool.Parse(line);
                    nodes.Add(new Node(x, y, key, id));
                    line = reader.ReadLine();
                } while (reader.Peek() != -1
                    && !line.StartsWith("EDGES")
                    && !line.StartsWith("NPCS")
                    && !line.StartsWith("TRIGGER")
                    && !line.StartsWith("PLAYER")
                    );

                if (line.StartsWith("EDGES"))
                    line = reader.ReadLine();
                do
                {
                    int sid, eid;
                    int spacei = line.IndexOf(' ');
                    sid = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    eid = int.Parse(line);
                    Node n1 = nodes.Find(n => n.ID == sid);
                    Node n2 = nodes.Find(n => n.ID == eid);
                    n1.Edges.Add(new Edge(n1, n2));
                    n2.Edges.Add(new Edge(n2, n1));
                    line = reader.ReadLine();
                } while (reader.Peek() != -1
                    && !line.StartsWith("NPCS")
                    && !line.StartsWith("TRIGGER")
                    && !line.StartsWith("PLAYER")
                    );

                if (line.StartsWith("NPCS"))
                    line = reader.ReadLine();
                do
                {
                    int x, y, sid, eid;
                    string type, mode;
                    int spacei = line.IndexOf(' ');
                    x = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    y = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    type = line.Substring(0, spacei);
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    mode = line.Substring(0, spacei);
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    sid = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    eid = int.Parse(line);
                    Node pStart = null, pEnd = null;
                    if (sid > 0 && eid > 0)
                    {
                        pStart = nodes.Find(n => n.ID == sid);
                        pEnd = nodes.Find(n => n.ID == sid);
                    }
                    NPC npc = null;
                    Animation animation;
                    if (type.StartsWith("Civilian"))
                    {
                        int pnum = Game1.random.Next(1, 4);
                        string pedAnim = "student" + pnum + "_static";
                        //Animation a = SpriteDatabase.GetAnimation("cop_static");
                        PedestrianState pstate = PedestrianState.WANDER;
                        if (mode.StartsWith("Static"))
                        {
                            pstate = PedestrianState.STATIC;
                            animation = SpriteDatabase.GetAnimation("student" + pnum + "_static");
                        }
                        else
                        {
                            animation = SpriteDatabase.GetAnimation("student" + pnum + "_walk");
                        }
                        npc = new Pedestrian(
                            new PhysicsComponent2D(new Vector2(x, y), 0, new Vector2(20, 20), 115, 750, 75, 1000, 8, 40, 0.25f, true),
                            new MovementAIComponent2D(),
                            new DrawComponent(animation, Color.White, Vector2.Zero, new Vector2(.4f, .4f), .5f),
                            pstate
                            );
                    }
                    // The cops are not in, so they are ignored for now

                    if (type.StartsWith("DumbCop"))
                    {
                        DumbCopState dcState = DumbCopState.WANDER;
                        if (mode.StartsWith("Static"))
                        {
                            dcState = DumbCopState.STATIC;
                            animation = SpriteDatabase.GetAnimation("cop_static");
                            
                        }
                        else if (mode.StartsWith("Wander"))
                        {
                            dcState = DumbCopState.WANDER;
                            animation = SpriteDatabase.GetAnimation("cop_walk");
                        }
                        else
                        {
                            //TODO
                            dcState = DumbCopState.WANDER;
                            animation = SpriteDatabase.GetAnimation("cop_walk");
                        }
                        npc = new DumbCop(
                            new PhysicsComponent2D(new Vector2(x, y), 0, new Vector2(20, 20), World.Speeds.DumbCop_Run, 750, World.Speeds.DumbCop_Walk, 1000, 8, 50, 0.25f, true),
                            new MovementAIComponent2D(),
                            new DrawComponent(animation, Color.White, Vector2.Zero, new Vector2(.4f, .4f), .5f), dcState);

                    }
                    if (type.StartsWith("SmartCop"))
                    {
                        SmartCopState dcState = SmartCopState.WANDER;
                        if (mode.StartsWith("Static"))
                        {
                            dcState = SmartCopState.STATIC;
                            animation = SpriteDatabase.GetAnimation("smartCop_static");

                        }
                        else
                        {
                            dcState = SmartCopState.WANDER;
                            animation = SpriteDatabase.GetAnimation("smartCop_walk");
                        }

                        npc = new SmartCop(
                            new PhysicsComponent2D(new Vector2(x, y), 0, new Vector2(20, 20), World.Speeds.SmartCop_Run, 750, World.Speeds.SmartCop_Walk, 1000, 8, 50, 0.25f, true),
                            new MovementAIComponent2D(3, 2, MathHelper.ToRadians(45), 0.5f, 50, 25, Vector2.Zero, Vector2.Zero,0.1f),
                            new DrawComponent(animation, Color.White, Vector2.Zero, new Vector2(.4f, .4f), .5f), dcState);
                    }
                    if (type.StartsWith("RoboCop"))
                    {
                        npc = new RoboCop(
                            new PhysicsComponent2D(new Vector2(x, y), 0, new Vector2(20, 20), World.Speeds.RoboCop_Run, 750, 75, 1000, 8, 50, 0.25f, true),
                            new MovementAIComponent2D(),
                            new DrawComponent(SpriteDatabase.GetAnimation("roboCop_static"), Color.White, 
                                              Vector2.Zero, new Vector2(.4f, .4f), .5f));
                    }
                    if (npc != null)
                    {
                        if (pStart != null && pEnd != null)
                        {
                            npc.patrolStart = pStart;
                            npc.patrolEnd = pEnd;
                        }
                        startingNPCs.Add(npc);
                    }
                    line = reader.ReadLine();
                }
                while (reader.Peek() != -1
                    && !line.StartsWith("TRIGGER")
                    && !line.StartsWith("PLAYER")
                    );

                if (line.StartsWith("TRIGGER"))
                    line = reader.ReadLine();
                do
                {
                    int x, y, w, h, id;
                    int spacei = line.IndexOf(' ');
                    x = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    y = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    w = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    h = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    id = int.Parse(line);
                    triggers.Add(new Trigger(x, y, w, h, id));
                    line = reader.ReadLine();
                } while (reader.Peek() != -1
                    && !line.StartsWith("NODES")
                    && !line.StartsWith("EDGES")
                    && !line.StartsWith("NPCS")
                    && !line.StartsWith("PLAYER")
                    && !line.StartsWith("CONSUMABLE"));

                if (line.StartsWith("CONSUMABLE"))
                    line = reader.ReadLine();
                do
                {
                    int x, y;
                    ConsumableType t;
                    int spacei = line.IndexOf(' ');
                    x = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    y = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    t = (ConsumableType)Enum.Parse(typeof(ConsumableType), line);
                    consumables.Add(new ConsumableSpawnpoint(new Vector2(x, y), t));
                    line = reader.ReadLine();
                } while (reader.Peek() != -1
                    && !line.StartsWith("NODES")
                    && !line.StartsWith("EDGES")
                    && !line.StartsWith("NPCS")
                    && !line.StartsWith("TRIGGER")
                    && !line.StartsWith("PLAYER"));

                if (line.StartsWith("PLAYER"))
                    line = reader.ReadLine();
                do
                {
                    int x, y;
                    int spacei = line.IndexOf(' ');
                    x = int.Parse(line.Substring(0, spacei));
                    line = line.Substring(spacei + 1);
                    spacei = line.IndexOf(' ');
                    y = int.Parse(line);
                    playerStart = new Vector2(x, y);
                    line = reader.ReadLine();
                } while (reader.Peek() != -1);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }