6d639d8f343e0c80b14f504199296cc424ec492e
[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 /// <summary>
21 /// Construct a trigger entity.
22 /// </summary>
23 /// <param name="identifier">The entity identifier.</param>
24 /// <param name="position">The position.</param>
25 /// <param name="info">The key-value pairs.</param>
26 /// <param name="game">The game reference.</param>
27 public Trigger(char identifier, Point position, Dictionary<string, string> info, Game game)
28 {
29 mGame = game;
30 mPrevCondition = false;
31 mCoordinates = position;
32
33 string condition;
34 if (info.TryGetValue("condition", out condition))
35 {
36 mCondition = new Script(condition, game);
37 }
38 else throw new Exception("Triggers must define a condition script.");
39
40 string eventt;
41 if (info.TryGetValue("event", out eventt))
42 {
43 mEvent = new Script(eventt, game);
44 }
45 else throw new Exception("Triggers must define an event script.");
46 }
47
48 /// <summary>
49 /// Check the trigger condition and execute the event if the
50 /// condition evaluates to true.
51 /// </summary>
52 public void Call()
53 {
54 Player player = mGame.GetPlayerAtCoordinates(mCoordinates);
55 if (player != null)
56 {
57 bool condition = mCondition.Run(player);
58 if (condition && condition != mPrevCondition)
59 {
60 mEvent.Run(player);
61 }
62 mPrevCondition = condition;
63 }
64 else
65 {
66 mPrevCondition = false;
67 }
68 }
69
70
71 /// <summary>
72 /// Calls the trigger.
73 /// </summary>
74 /// <param name="timeSpan">Unused.</param>
75 public virtual void Update(TimeSpan timeSpan)
76 {
77 Call();
78 }
79
80 public virtual void LoadContent(ContentManager contentManager)
81 {
82 // No implementation needed.
83 }
84
85 public virtual void Draw(SpriteBatch spriteBatch)
86 {
87 // No implementation needed.
88 }
89
90 public Vector2 Position
91 {
92 get { return Vector2.Zero; }
93 }
94
95 public Point Coordinates
96 {
97 get { return new Point(-1, -1); }
98 }
99
100 #endregion
101
102
103 #region Private Variables
104
105 Script mCondition;
106 Script mEvent;
107 Game mGame;
108 bool mPrevCondition;
109 Point mCoordinates;
110
111 #endregion
112 }
113 }
This page took 0.04454 seconds and 3 git commands to generate.