]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Trigger.cs
Fixed path finder thrown exception when finding a path to the cell you are already at.
[chaz/carfire] / CarFire / CarFire / CarFire / Trigger.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7 using Microsoft.Xna.Framework.Content;
8
9 namespace CarFire
10 {
11 /// <summary>
12 /// A trigger is an invisible entity whose only purpose is
13 /// to check a condition and run a script when the condition
14 /// is right.
15 /// </summary>
16 public class Trigger : IEntity
17 {
18 #region Public Methods
19
20 public Trigger(char identifier, Point position, Dictionary<string, string> info, Game game)
21 {
22 mGame = game;
23 mPrevCondition = false;
24 mCoordinates = position;
25
26 string condition;
27 if (info.TryGetValue("condition", out condition))
28 {
29 mCondition = new Script(condition, game);
30 }
31 else throw new Exception("Triggers must define a condition script.");
32
33 string eventt;
34 if (info.TryGetValue("event", out eventt))
35 {
36 mEvent = new Script(eventt, game);
37 }
38 else throw new Exception("Triggers must define an event script.");
39 }
40
41 public void Update(TimeSpan timeSpan)
42 {
43 Player player = mGame.GetPlayerAtCoordinates(mCoordinates);
44 if (player != null)
45 {
46 bool condition = mCondition.Run(player);
47 if (condition && condition != mPrevCondition)
48 {
49 mEvent.Run(player);
50 }
51 mPrevCondition = condition;
52 }
53 else
54 {
55 mPrevCondition = false;
56 }
57 }
58
59 public void LoadContent(ContentManager contentManager)
60 {
61 // No implementation needed.
62 }
63
64 public void Draw(SpriteBatch spriteBatch)
65 {
66 // No implementation needed.
67 }
68
69 public Vector2 Position
70 {
71 get { return Vector2.Zero; }
72 }
73
74 public Point Coordinates
75 {
76 get { return new Point(-1, -1); }
77 }
78
79 #endregion
80
81
82 #region Private Variables
83
84 Script mCondition;
85 Script mEvent;
86 Game mGame;
87 bool mPrevCondition;
88 Point mCoordinates;
89
90 #endregion
91 }
92 }
This page took 0.041404 seconds and 5 git commands to generate.