Click here to Skip to main content
15,891,607 members
Articles / Web Development / HTML

Zyan Drench, A Game for Android with Wifi Support

Rate me:
Please Sign up or sign in to vote.
4.99/5 (17 votes)
28 Feb 2016MIT12 min read 47.2K   1.3K   37  
This article describes building an Android game with networking support using C#, Xamarin.Android platform and Zyan Communication Framework.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Net;

namespace Drench
{
	[Activity(Label = "Server started...", NoHistory = true)]
	public class ServerStartedActivity : Activity
	{
		private CustomApplication App { get; set; }

		protected override async void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);

			// Set layout
			RequestWindowFeature(WindowFeatures.CustomTitle);
			SetContentView(Resource.Layout.ServerStarted);
			App = (CustomApplication)Application;

			// Set custom title
			Window.SetFeatureInt(WindowFeatures.CustomTitle, Resource.Layout.CustomTitle);
			var title = FindViewById<RelativeLayout>(Resource.Id.titleLayout);
			var parentView = title.Parent as View;
			if (parentView != null)
			{
				parentView.SetBackgroundColor(Settings.CustomTitleColor);
				title.SetBackgroundColor(Settings.CustomTitleColor);
			}

			var serverAddressTextView = FindViewById<TextView>(Resource.Id.serverAddressTextView);
			serverAddressTextView.Text = "...";
			serverAddressTextView.Text = await Task.Factory.StartNew<string>(GetAddresses);
		}

		private string GetAddresses()
		{
			var result = new StringBuilder();

			Java.Util.IEnumeration networkInterfaces = NetworkInterface.NetworkInterfaces;
			while (networkInterfaces.HasMoreElements)
			{
				Java.Net.NetworkInterface netInterface = (Java.Net.NetworkInterface)networkInterfaces.NextElement();
				if (netInterface.IsLoopback)
				{
					continue;
				}

				Java.Util.IEnumeration addresses = netInterface.InetAddresses;
				while (addresses.HasMoreElements)
				{
					var address = (Java.Net.InetAddress)addresses.NextElement();
					if (address is Java.Net.Inet4Address)
					{
						result.AppendLine(address.HostAddress); // netInterface.Name: wlan0
					}
				}
			};

			return result.ToString();
		}

		private void GameStartedStopped(object sender, EventArgs args)
		{
			StartGame();
		}

		private void StartGame()
		{
			StartActivity(typeof(DrenchBoardActivity));
		}

		protected override void OnResume()
		{
			base.OnResume();

			App.DrenchGameServer.GameStarted += GameStartedStopped;
			App.DrenchGameServer.GameStopped += GameStartedStopped;

			if (App.DrenchGameServer.IsReady)
			{
				StartGame();
			}
		}

		protected override void OnPause()
		{
			base.OnPause();

			App.DrenchGameServer.GameStarted -= GameStartedStopped;
			App.DrenchGameServer.GameStopped -= GameStartedStopped;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) ULTIMA Businessware
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions