Map System
-
Baseball435
- Posts: 548
- Joined: Sun May 30, 2010 3:49 am
Map System
Hey everyone, i am in developement of my game and i have like everything done almost. I have a fighting system, housing, buying from stores, selling, crafting, and a public exchange, but now i need the map system but im stumped on how i would go about making it. I have four buttons, north, south, east, and west, and when they click them they will redirect to the page compass.php and that will check which button is pressed and then move them north south east or west of where they are and then it will redirect back to the main page (indext.php). I really dont know how to do this, i want to use mysql and seperate each map but i just dont know how it will work. If someone could help me or guide me toward how i would do it that would be great. Thanks!
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Map System
I actually wrote a map system like that in my first PHP game I was working on (which like normal for me, had the design bloat out to an unrealistic degree, so it was abandoned). Here were some of the ways I did it back then:
1. You can turn the North, South, East and West arrows into links to the same page with querystrings (aka GET values) on the end of it telling the same page which direction they clicked. Then in the same page (your map page), the map names are made up of just a number, a comma and another number (like "3, 6"). That way you can mathematically figure out which map to show in your script by keeping an X and a Y value. If they go East for example, then X = X + 1. If they go north, Y = Y - 1. Then you just add these values into your img tag that displays the map and the correct map will come up.
2. When using $_GET values like this for movement, a pretty serious problem I had was a user refresh would cause another movement in the last direction. Maybe it wouldn't be a bad thing in your game, but it was in mine. How I solved it is that I added another value ("moveid" for the sake of conversation, but called movecode in script) onto the querystring which was a random 5-digit number. Every time the script was loaded and the movement was used, it would first check in the database to make sure that was not the same moveid value listed in the "movecode" column for that player. If it was, the movement in the $_GET string was ignored. If it was not, it made the movement, displayed the correct image, and then stored the new moveid value in the movecode column for that player to prevent further refresh movements.
Here is the script if you want to see it, or use parts of it. It is definitely not my best, since it was one of my first PHP scripts. It also over-uses $_SESSION variables most likely (probably would be better to just save a name and playerid and repull values, since this is setting the script up to miss vital changes to the values and forget to update it):
1. You can turn the North, South, East and West arrows into links to the same page with querystrings (aka GET values) on the end of it telling the same page which direction they clicked. Then in the same page (your map page), the map names are made up of just a number, a comma and another number (like "3, 6"). That way you can mathematically figure out which map to show in your script by keeping an X and a Y value. If they go East for example, then X = X + 1. If they go north, Y = Y - 1. Then you just add these values into your img tag that displays the map and the correct map will come up.
2. When using $_GET values like this for movement, a pretty serious problem I had was a user refresh would cause another movement in the last direction. Maybe it wouldn't be a bad thing in your game, but it was in mine. How I solved it is that I added another value ("moveid" for the sake of conversation, but called movecode in script) onto the querystring which was a random 5-digit number. Every time the script was loaded and the movement was used, it would first check in the database to make sure that was not the same moveid value listed in the "movecode" column for that player. If it was, the movement in the $_GET string was ignored. If it was not, it made the movement, displayed the correct image, and then stored the new moveid value in the movecode column for that player to prevent further refresh movements.
Here is the script if you want to see it, or use parts of it. It is definitely not my best, since it was one of my first PHP scripts. It also over-uses $_SESSION variables most likely (probably would be better to just save a name and playerid and repull values, since this is setting the script up to miss vital changes to the values and forget to update it):
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Tent. Title</title>
<meta name="" content="">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<?php
include 'connect.php';
session_start();
////Ensure the player has not tried to go directly to the mapscreen without logging in first
if (!(isset($_SESSION['name']))) {
echo "Please login to play Tent. Title! <br />";
echo "<a href='../login/login.php'>Login</a>";
exit;
}
////Including contents of session.php. Did not seem to work in include() for some reason.
$name = $_SESSION['name'];
$exper = $_SESSION['exper'];
$tnl = $_SESSION['tnl'];
$level = $_SESSION['level'];
$class = $_SESSION['class'];
$worldx = $_SESSION['worldx'];
$worldy = $_SESSION['worldy'];
$attack = $_SESSION['attack'];
$defense = $_SESSION['defense'];
$ran = $_SESSION['ran'];
$craft = $_SESSION['craft'];
$craftlevel = $_SESSION['craftlevel'];
$action = $_SESSION['action'];
$hpoints = $_SESSION['hpoints'];
$maxhpoints = $_SESSION['maxhpoints'];
$cpoints = $_SESSION['cpoints'];
$maxcpoints = $_SESSION['maxcpoints'];
$strength = $_SESSION['strength'];
$dexterity = $_SESSION['dexterity'];
$agility = $_SESSION['agility'];
$focus = $_SESSION['focus'];
$endurance = $_SESSION['endurance'];
$wisdom = $_SESSION['wisdom'];
$up = $_SESSION['up'];
$down = $_SESSION['down'];
$left = $_SESSION['left'];
$right = $_SESSION['right'];
$in = $_SESSION['in'];
////Get last movecode from the database so that if player tries to refresh, it will use old movement code
////which will not work.
$sqlq = "SELECT movecode FROM players WHERE name='$name'";
$result = mysqli_query($db, $sqlq) or die ("Could not get last movement");
$mResult = mysqli_fetch_assoc($result);
$lastMove = $mResult['movecode'];
////See if the player is coming back to this page after moving directions, and if they are, move them in the variable
////...and also update the session then the database
if (isset($_GET['direction'])) {
$direction = $_GET['direction'];
$currentMove = $_GET['movecode'];
if ($currentMove != $lastMove) {
if ($direction == "east" && $worldx < 12) {
//updating worldx variable here to ensure that map is updated correctly below.
$worldx = $worldx + 1;
//updating session worldx to ensure that player's x location is known for future movement and to pass on
$_SESSION['worldx'] = $worldx;
} else if ($direction == "west" && $worldx > 1) {
$worldx = $worldx - 1;
$_SESSION['worldx'] = $worldx;
} else if ($direction == "north" && $worldy > 1) {
$worldy = $worldy - 1;
$_SESSION['worldy'] = $worldy;
} else if ($direction == "south" && $worldy < 12) {
$worldy = $worldy + 1;
$_SESSION['worldy'] = $worldy;
}
////Update the player's location in the database
$name = $_SESSION['name'];
$query = "UPDATE players SET worldx='$worldx', worldy='$worldy', movecode='$currentMove' WHERE name='$name'";
mysqli_query($db, $query) or die ("Could not update player position in the world to db");
}
}
////Commented out development status
/*echo "Up: $_SESSION[up] Down: $_SESSION[down] Left: $_SESSION[left] Right: $_SESSION[right] In: $_SESSION[in] PlayerY: $_SESSION[worldy] PlayerX: $_SESSION[worldx] <br />"; */
////Turn the directional icons off if the player is at the border of the map
if ($worldx == 1) {
$_SESSION['left'] = FALSE;
}
if ($worldx > 1) {
$_SESSION['left'] = TRUE;
}
if ($worldx == 12) {
$_SESSION['right'] = FALSE;
}
if ($worldx < 12) {
$_SESSION['right'] = TRUE;
}
if ($worldy == 1) {
$_SESSION['up'] = FALSE;
}
if ($worldy > 1) {
$_SESSION['up'] = TRUE;
}
if ($worldy == 12) {
$_SESSION['down'] = FALSE;
}
if ($worldy < 12) {
$_SESSION['down'] = TRUE;
}
////Create a new movement code to ensure the player does not move on refreshes
$movecode = rand(1, 49000);
?>
<table border="1" frame="void">
<tr>
<td>
<?php
////Player stat printouts
echo "Name: $_SESSION[name] <br />";
echo "Class: ".ucfirst($_SESSION['class'])."<br />";
echo "Level: $_SESSION[level] <br />";
echo "XP: $_SESSION[exper]/$_SESSION[tnl] <br />";
echo "Health: $_SESSION[hpoints]/$_SESSION[maxhpoints] <br />";
echo "Caloric: $_SESSION[cpoints]/$_SESSION[maxcpoints] <br /><br />";
echo "<a href='stats.php'>Stats</a> <br />";
echo "<a href='fullmap.php'>Large Map</a>";
echo "<hr />";
echo "<a href='logout.php'>Logout</a>";
?>
</td>
<td>
<?php
////Get info to pull up correct world map image from worldx and worldy data
echo "<img border=\"1\" src=\"../images/[$worldx, $worldy].jpg\" />";
?>
</td>
<!-- Trying out new location for directional pad -->
<td align="right">
<table border="0">
<tr>
<td></td>
<td>
<?php
////Determine whether or not arrow icon should be clickable or not. Same for next several icons
if ($_SESSION['up']) {
echo "<a href='mapscreen.php?direction=north&movecode=$movecode'><img border=\"0\" src=\"../images/arrowup.png\" /></a>";
} else {
echo "<img border=\"0\" src=\"../images/noarrowup.png\" />";
}
?>
</td>
<td></td>
</tr>
<tr>
<td>
<?php
if ($_SESSION['left']) {
echo "<a href='mapscreen.php?direction=west&movecode=$movecode'><img border=\"0\" src=\"../images/arrowleft.png\" /></a>";
} else {
echo "<img border=\"0\" src=\"../images/noarrowleft.png\" />";
}
?>
</td>
<td>
<?php
if ($_SESSION['in']) {
echo "<a href='mapscreen.php?direction=in&movecode=$movecode'><img border=\"0\" src=\"../images/arrowin.png\" /></a>";
} else {
echo "<img border=\"0\" src=\"../images/noarrowin.png\" />";
}
?>
</td>
<td>
<?php
if ($_SESSION['right']) {
echo "<a href='mapscreen.php?direction=east&movecode=$movecode'><img border=\"0\" src=\"../images/arrowright.png\" /></a>";
} else {
echo "<img border=\"0\" src=\"../images/noarrowright.png\" />";
}
?>
</td>
</tr>
<tr>
<td></td>
<td>
<?php
if ($_SESSION['down']) {
echo "<a href='mapscreen.php?direction=south&movecode=$movecode'><img border=\"0\" src=\"../images/arrowdown.png\" /></a>";
} else {
echo "<img border=\"0\" src=\"../images/noarrowdown.png\" />";
}
?>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>The indelible lord of tl;dr
-
Baseball435
- Posts: 548
- Joined: Sun May 30, 2010 3:49 am
Re: Map System
i have an idea for this actually thanks to you!
im going to use the x and y thing and im going to have two seperate variables in the table (x and y) and that will be the location of the maps. Then i will have the main page just pull the information and assign the town to whichever x and y coordinate they are on!