RPG Doors / Events - c#

Heeeey, on my RPG i'm working on i have a working tile engine which you can add layers, i have three layers:
Bottom Layer
Top Layer
Solid Layer
I have collision working and also the character animation working.
But how would i go making doors, that when you walk into them and have a key, it switches to another map?
I tried to add another layer called "EventLayer" but i don't know how to format it properly to work.. And things like Events - For example a falling book, sound effect...
Could someone please help me with this?
Thanks in advance! :)

Events like this will be a lot different based on what exactly you need in your game.
If you want events to only be doors and similar interactable objects, and have NPCs and monsters etc. in their own type, then events are not that tough to make.
You need to include every property you need to have for all your events, such as the position, graphic and trigger type. In your case, you want certain events only to function when certain items are in possession. Add a requirement list, and make sure everything in that list is met before executing the corresponding event.
When programming your event objects, you might want to make them universal, so you can create most (if not all) of your events from this instances of this class. Example of a quick event class mockup:
class Event {
public Texture2D Graphic { get; set; }
public Vector2 TileLocation { get; set; }
public List<Condition> Conditions { get; set; }
public TriggerType Trigger { get; set; }
public List<Command> Commands { get; set; }
public int CommandIndex { get; set; }
public bool Running { get; set; }
public bool Erased { get; set; }
public Event() { Erased = false; }
public void Update(GameTime gameTime){
if(Erased) return;
if(Running){
// continue command execution
}
else // check for triggering
switch(Trigger){ }
}
public void Draw()[
if(Erased) return;
// drawing code
}
}
The command class and the TriggerType enum should not be an issue.
I hope this helps you get what you wanted. It's a bit hard to tell you exactly how you should do it based on such little information. Good luck.

Related

Stuck at how to proceed unit testing this business logic using tdd

I have a method that I give a list of car movements (Operation class) in a parking lot (Entries,Exits) each movement has a related cars list (OperationVehicle class). This method should result what I called cars log. It lists all cars, at what time entered what time exited and the duration for each one.
I am a bit lost on what things to test first. An answer to this question would be like a tutorial for me.
How can I proceed on unit testing this particular logic using tdd ?
public class Operation
{
public Operation()
{
OperationVehicles = new List<OperationVehicle>();
}
public int OperationId { get; set; }
public DateTime Date { get; set; }
public virtual OperationType OperationType { get; set; }
public virtual List<OperationVehicle> OperationVehicles { get; set; }
}
public class OperationVehicle
{
public int OperationVehicleId { get; set; }
public OperationType OperationType { get; set; }
public virtual Operation Operation { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class CarLog
{
public int Id { get; set; }
public DateTime Date { get; set; }
public Vehicle Vehicle { get; set; }
public int StayLength { get; set; }
}
public IEnumerable<CarLog> GenerateCarsLog(List<Operation> CarStayOperations)
{
// not implemented yet
return new List<CarLog>();
}
What you have done so far is that you have modelled your domain in code (the relationships). But you haven't written much logic, so there's not much to test.
I wouldn't let your tests interact with these classes, at least not to start with. Rather, I would probably start somewhere around the user interface. Simulate the action that the user performs (and with user I don't necessarily mean a human being, but it could be another system). Test the behaviour, and do it as close to the boundaries of your system as possible.
Exactly what tests to write are hard to answer without more knowledge about the context. But I'll give it a try.
So what should the user be able to do with your system? For instance, write a first test where a car enters the parking lot and let the test assert in some way that the car is now in the parking lot.
But how would the user (or another user) know that the car successfully entered the parking lot? Is there, for example, some kind of user interface for the Car Log? Let your test's asserts look at that.
That would, however, be quite a large test to write, both including entering cars and checking the cars log. Perhaps better to start with the car log: just verifying that it is empty. Or perhaps there is an even smaller step you can start with?
My answer is a bit tentative, because that's what it is usually like: you learn and discover as you go.
A design note: Please don't expose lists from your classes (OperationVehicles). It totally breaks encapsulation. Add a Operation.AddOperationVehicle() method instead and return an IEnumerable<OperationVehicle>.

How do multiple layers work in the MVVM pattern?

I am trying to get my head around MVVM. For a simple list->data view it's no problem. But I am struggling to understand how multiple layers work. I sort of have something working but it's very hit and miss as to which bits work and which bits don't. For example, some data updates, some doesn't. Anything in a deeper level which should affect a list at an upper level sometimes updates the list, sometimes doesn't. There must be a pattern but I have yet to spot it. Does anybody know of any tutorials with more than just a list->data type of view?
Example:
List of widgets
+- Widget name
+- Widget description
+- List of Widget parts
+- Part ID
+- Colour
In that example I could have a three-column approach - list of widgets in the left, widget information in the middle including the parts list, and then the part detail on the right.
You should have multiple ViewModels, one for each level.
Then you can provide events to let the upper levels update on change.
For example you can have a
public class WidgetListViewModel
{
public ObservableCollection<WidgetViewModel> Widgets {get; set; }
}
public class WidgetViewModel
{
public string WidgetName { get; set; }
public string WidgetDescription { get; set; }
public ObservableCollection<WidgetPartViewModel> Parts { get; set; }
}
public class WidgetPartViewModel
{
public int PartId { get; set; }
public System.Windows.Media.Color Color { get; set; }
}
Having events (including a simple pattern) is described here Events in .Net
Furthermore, I recommend watching this excellent video tutorial on MVVM:
Jason Dollinger on MVVM
The video covers some issues of Unity also! (which could be very valuable for you)
The sourcecode he developes is also available:
Lab49 Sourcecode by Jason Dollinger

How to model entities to enforce a data models constraints at compile time?

I have the below data model that constrains ItemTypes with a subset of Events. Each ItemType has a valid set of Events, this is constrained in the ItemEvent table. For example, a Video can be { played, stopped, paused }, an Image can be { resized, saved, or shared }, and so on.
What is the best way to reflect this constraint in the Entity model so that I can get compile time assurance that an Event used is valid for a particular Item? Specifically, I am refactoring the AddItemEventLog method:
public void AddItemEventLog(Item item, string ItemEvent)
{
//
}
Obviously, this is a contrived example, just trying illustrate-- it allows a developer to pass in any ItemEvent string they desire. Even if I create an enumeration based on ItemEvent resultset, there isnt anything in the entity model to prevent a developer from passing in ItemEvent.Resize with an Item of type Video.
I have Item as the base class of Video, and I have tried to override an enum but now know that is not possible. I am less interested in checking for the validity of the Event at runtime, as I already will throw an exception when the DB raises a FK violation. I want to nip it in the bud at coding time if possible :)
Currently have classes modeled like this but open to any modifications:
//enums.cs
public enum ItemType : byte
{
Video = 1,
Image = 2,
Document = 3
}
//item.cs
public class Item : BaseModel
{
public int ItemId { get; set; }
public ItemTypeLookup.ItemType ItemType { get; set; }
public string ItemName { get; set; }
}
//video.cs
public class Video : Item
{
public string Width { get; set; }
public string Height { get; set; }
public string Thumb { get; set; }
}
I think that Code Contracts may be the only way to enforce something like this at compile time. Outside of compile time checks, writing unit tests to ensure the correct functionality is the next best thing!

javascript Highcharts object as a C# class

I've been working with javascript Highcharts and I made a basic 'Chart Builder' app. One of my goals is to have the user create and modify as many options as they like and save those to the db. The main problem I'm having is trying to convert the Highcharts object to a c# class. I've been building it slowly(ie manually) with the parts I need, as I need them, but to eventually get the whole thing converted will take a long time.
Ideally, I'd like to create and setup the whole highcharts options object server side and just send it 100% complete to highcharts
Is there any easy way to do this? Has anyone already done this?
Here is the Highcharts reference page: http://www.highcharts.com/ref/
and this is what I've done so far.
public class Highchart
{
public title title { get; set; }
public plotOptions plotOptions { get; set; }
}
public class title
{
public string text { get; set; }
}
public class plotOptions
{
public series series { get; set; }
}
public class series
{
public string stacking { get; set; }
public string borderColor { get; set; }
public bool shadow { get; set; }
public int borderWidth { get; set; }
}
As you can see, I just started ^_^
Update : The Highcharts .Net library has been updated in December, and is nearly feature complete as per V2.1.9 of the Javascript library.
The .Net library currently has support for multiple axes, point objects, viewstate management after postbacks, click events for points, series etc, and a built in implementation of an AJAX datasource ;) You don't need to write a single line of JS code unless you want to handle click events; you simply code in C#, and the appropriate JS is rendered automatically for you..
Click here to view the Live Demo

List Variable in WinForm C#

I'm still learning C# .Net 4 and this is my first WinForms so please be kind.
Continuing in my project, my financial DataFeed is streaming into my application by use of 'Asynchronous Sockets?'. Anyway, the data I am getting is tick per tick data, which is basically 1 order/transaction. So now I need to build bars with this tick by tick data, in particular Range Bars.
My problem is I don't want to go to the database and grab this data, so I am looking to do this in memory, like a list variable. Eventually, this system on the main server will do all the number crunching etc... and will have clients connected via Sockets to interrogate or set their own predefined algos on the in coming data and build their own charts using different ranges and indicators.
I wouldn't want to offload this to the client because I would like to keep the indicators technology proprietary.
How would I go about implementing this?
I already have my class called Tick
class Tick
{
public double Last { get; set; }
public double Bid { get; set; }
public double Ask { get; set; }
public double BidSize { get; set; }
public double AskSize { get; set; }
public DateTime TimeStampInternal { get; set; }
public int DTNTickID { get; set; }
public int UpdateTypeID { get; set; }
}
I'm thinking of a
Static List<Tick> Ticks
but I don't think this is the way to go because
I need to be able to hold only a certain amount of ticks and as new data comes in, old data gets thrown away, FIFO to keep memory usage down.
I will only be able to hold 1 Static List and I need something dynamic, e.g. have a List for each user that connects which would be identifiable to them only.
Please help me architect this correctly with best practices for speed and efficiency.
Sounds like a circular buffer is what you're looking for.
http://circularbuffer.codeplex.com/
Or perhaps a queue.
I hope that I correctly understand what you want, so here is very pseudocode :
public class User {
private UserTickList<Tick> _userTicks = new UserTickList<Tick>();
public void AddUserTick(Tick t) {
_userTicks.Add(t);
}
/*remove, update if need*/
}
public class UserTickList {
private List<Tick> _list = new List<Tick>();
public void AddTick(Tick tick) {
if(_list.Count == 10){
/*perform what you need*/
}
else
_list.Add(tick);
}
}
I repeat this probabbly will not compile, but just to give an idea what it can look like.
Hope this helps.

Categories

Resources