Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gadig
four
Commits
4130c051
Commit
4130c051
authored
Nov 19, 2016
by
Luke Smith
Browse files
Map Tool Added
parent
664b79ef
Changes
15
Hide whitespace changes
Inline
Side-by-side
Assets/Scripts/MapTool/MapLoad.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
UnityEngine.UI
;
using
System.Collections
;
public
class
MapLoad
:
MonoBehaviour
{
public
GameObject
button
;
private
MapMaker
mm
;
void
Start
()
{
mm
=
GameObject
.
Find
(
"Create"
).
GetComponent
<
MapMaker
>
();
}
public
void
CreateMapButtons
()
{
string
[]
maps
=
System
.
IO
.
Directory
.
GetFiles
(
"Assets\\Resources\\Maps\\"
,
"*.txt"
);
for
(
int
i
=
0
;
i
<
maps
.
Length
;
i
++){
string
s
=
maps
[
i
];
RectTransform
rt
=
(
RectTransform
)(((
GameObject
)
Instantiate
(
button
)).
transform
);
rt
.
SetParent
(
GameObject
.
Find
(
"Canvas"
).
transform
);
rt
.
anchoredPosition
=
new
Vector3
(
0
,
-
i
*
30
+
75
);
rt
.
localScale
=
new
Vector3
(
1
,
1
,
1
);
s
=
s
.
Substring
(
s
.
LastIndexOf
(
"\\"
)
+
1
,
s
.
Length
-
5
-
s
.
LastIndexOf
(
"\\"
));
rt
.
FindChild
(
"Text"
).
GetComponent
<
Text
>
().
text
=
s
;
rt
.
GetComponent
<
Button
>().
onClick
.
AddListener
(()
=>
{
LoadMap
(
s
);
});
}
}
void
LoadMap
(
string
file
)
{
GameObject
[]
buttons
=
GameObject
.
FindGameObjectsWithTag
(
"Button"
);
for
(
int
i
=
0
;
i
<
buttons
.
Length
;
i
++)
{
Destroy
(
buttons
[
i
]);
}
string
text
=
System
.
IO
.
File
.
ReadAllText
(
"Assets/Resources/Maps/"
+
file
+
".txt"
);
string
[]
delim
=
{
"\r\n"
};
string
[]
lines
=
text
.
Split
(
delim
,
System
.
StringSplitOptions
.
None
);
int
x
=
int
.
Parse
(
lines
[
0
]
[
0
].
ToString
());
int
y
=
int
.
Parse
(
lines
[
0
]
[
2
].
ToString
());
MapMakerTile
[,]
map
=
mm
.
GenerateEmptyTiles
(
x
,
y
);
for
(
int
i
=
1
;
i
<
lines
.
Length
-
1
;
i
++)
{
string
[]
args
=
lines
[
i
].
Split
(
','
);
x
=
int
.
Parse
(
args
[
0
]);
y
=
int
.
Parse
(
args
[
1
]);
map
[
x
,
y
].
walkable
=
bool
.
Parse
(
args
[
2
]);
map
[
x
,
y
].
weight
=
float
.
Parse
(
args
[
3
]);
map
[
x
,
y
].
GetComponent
<
SpriteRenderer
>
().
sprite
=
Resources
.
Load
<
Sprite
>
(
"Tiles/"
+
args
[
4
]);
for
(
int
j
=
0
;
j
<
args
.
Length
;
j
++)
{
//for later use (extra tile shit)
}
}
}
}
Assets/Scripts/MapTool/MapLoad.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 92d5e911fb5523e43bac0c60148eec87
timeCreated: 1477702685
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/MapTool/MapLoader.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
System.Collections
;
public
class
MapLoader
:
MonoBehaviour
{
public
string
fileName
;
public
GameObject
emptyTile
;
void
Start
()
{
string
text
=
System
.
IO
.
File
.
ReadAllText
(
"Assets\\Resources\\Maps\\"
+
fileName
+
".txt"
);
string
[]
delim
=
{
"\r\n"
};
string
[]
lines
=
text
.
Split
(
delim
,
System
.
StringSplitOptions
.
None
);
string
[]
coords
=
lines
[
0
].
Split
(
','
);
int
x
=
int
.
Parse
(
coords
[
0
].
ToString
());
int
y
=
int
.
Parse
(
coords
[
1
].
ToString
());
Tile
[,]
map
=
GenerateTiles
(
x
,
y
);
for
(
int
i
=
1
;
i
<
lines
.
Length
-
1
;
i
++)
{
string
[]
args
=
lines
[
i
].
Split
(
','
);
x
=
int
.
Parse
(
args
[
0
]);
y
=
int
.
Parse
(
args
[
1
]);
map
[
x
,
y
].
walkable
=
bool
.
Parse
(
args
[
2
]);
if
(
map
[
x
,
y
].
walkable
){
Destroy
(
map
[
x
,
y
].
GetComponent
<
BoxCollider2D
>
());
}
map
[
x
,
y
].
weight
=
float
.
Parse
(
args
[
3
]);
map
[
x
,
y
].
GetComponent
<
SpriteRenderer
>
().
sprite
=
Resources
.
Load
<
Sprite
>
(
"Tiles/"
+
args
[
4
]);
}
Map
.
map
=
map
;
}
Tile
[,]
GenerateTiles
(
int
rows
,
int
cols
)
{
Tile
[,]
map
=
new
Tile
[
cols
,
rows
];
float
startx
=
-(
float
)
cols
/
2f
*
emptyTile
.
transform
.
lossyScale
.
x
;
float
starty
=
(
float
)
rows
/
2f
*
emptyTile
.
transform
.
lossyScale
.
y
;
for
(
int
y
=
0
;
y
<
rows
;
y
++)
{
for
(
int
x
=
0
;
x
<
cols
;
x
++)
{
map
[
x
,
y
]
=
((
GameObject
)
Instantiate
(
emptyTile
,
new
Vector2
(
startx
+
x
*
emptyTile
.
GetComponent
<
BoxCollider2D
>().
size
.
x
,
starty
-
y
*
emptyTile
.
GetComponent
<
BoxCollider2D
>().
size
.
x
),
Quaternion
.
identity
)).
GetComponent
<
Tile
>
();
map
[
x
,
y
].
x
=
x
;
map
[
x
,
y
].
y
=
y
;
}
}
return
map
;
}
}
Assets/Scripts/MapTool/MapLoader.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 00b82cb959770da48addbe5d69996c09
timeCreated: 1479593362
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/MapTool/MapMakerMode.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
UnityEngine.UI
;
using
System.Collections
;
public
class
MapMakerMode
:
MonoBehaviour
{
public
static
bool
imageMode
=
true
;
private
InputField
ipf
;
private
Toggle
walkable
;
private
Text
text
;
private
MapMakerTile
selected
;
void
Start
()
{
text
=
transform
.
FindChild
(
"Text"
).
GetComponent
<
Text
>();
ipf
=
GameObject
.
Find
(
"Weight"
).
GetComponent
<
InputField
>
();
walkable
=
GameObject
.
Find
(
"Walkable"
).
GetComponent
<
Toggle
>
();
HideUI
();
}
void
Update
()
{
if
(
Input
.
GetMouseButtonDown
(
0
))
{
RaycastHit2D
hit
=
Physics2D
.
Raycast
(
Camera
.
main
.
ScreenToWorldPoint
(
Input
.
mousePosition
),
Vector2
.
zero
);
if
(
hit
)
{
UpdateUIInfo
(
hit
.
collider
.
gameObject
.
GetComponent
<
MapMakerTile
>
());
}
}
if
(
Input
.
GetKeyUp
(
"b"
)
&&
!
imageMode
)
{
selected
.
walkable
=
!
selected
.
walkable
;
UpdateUIInfo
(
selected
);
}
}
void
UpdateUIInfo
(
MapMakerTile
tile
)
{
if
(
selected
!=
null
){
selected
.
selected
=
false
;
}
selected
=
tile
;
tile
.
selected
=
true
;
ipf
.
text
=
tile
.
weight
+
""
;
walkable
.
isOn
=
tile
.
walkable
;
}
public
void
ChangeStats
()
{
float
w
=
0
;
float
.
TryParse
(
ipf
.
text
,
out
w
);
selected
.
weight
=
w
;
selected
.
walkable
=
walkable
.
isOn
;
}
public
void
SwitchMode
()
{
if
(
imageMode
)
{
text
.
text
=
"Image"
;
imageMode
=
false
;
ShowUI
();
}
else
{
text
.
text
=
"Passable and Weight"
;
imageMode
=
true
;
HideUI
();
}
}
void
ShowUI
()
{
ipf
.
gameObject
.
SetActive
(
true
);
walkable
.
gameObject
.
SetActive
(
true
);
}
void
HideUI
()
{
ipf
.
gameObject
.
SetActive
(
false
);
walkable
.
gameObject
.
SetActive
(
false
);
}
}
Assets/Scripts/MapTool/MapMakerMode.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 1894a374ace13c34485b8aa77f51a64c
timeCreated: 1477709596
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/MapTool/MapMakerTile.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
System.Collections
;
using
System.Collections.Generic
;
public
class
MapMakerTile
:
MonoBehaviour
{
public
int
x
,
y
;
public
bool
walkable
=
true
;
public
float
weight
=
1
;
public
bool
selected
;
private
SpriteRenderer
sr
;
private
SpriteBar
sb
;
void
Start
()
{
sr
=
GetComponent
<
SpriteRenderer
>
();
sb
=
GameObject
.
Find
(
"Scrollbar"
).
GetComponent
<
SpriteBar
>
();
}
void
Update
()
{
if
(
MapMakerMode
.
imageMode
)
{
sr
.
color
=
Color
.
white
;
}
else
{
if
(
selected
)
{
sr
.
color
=
Color
.
yellow
;
}
else
if
(
walkable
)
{
sr
.
color
=
Color
.
blue
;
}
else
{
sr
.
color
=
Color
.
red
;
}
}
}
void
OnMouseDown
()
{
if
(
MapMakerMode
.
imageMode
)
{
sr
.
sprite
=
sb
.
GetHighlitedSprite
();
}
}
public
override
string
ToString
()
{
return
x
+
","
+
y
+
","
+
walkable
+
","
+
weight
+
","
+
sr
.
sprite
.
name
;
}
}
Assets/Scripts/MapTool/MapMakerTile.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 87ade81d31590624e926ba555ca73162
timeCreated: 1477700238
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/MapTool/MapSave.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
UnityEngine.UI
;
using
System.Collections
;
public
class
MapSave
:
MonoBehaviour
{
private
Text
text
;
void
Start
()
{
text
=
GameObject
.
Find
(
"Name"
).
transform
.
FindChild
(
"Text"
).
GetComponent
<
Text
>
();
}
public
void
SaveMap
()
{
string
map
=
""
;
int
cols
=
0
;
int
rows
=
0
;
GameObject
.
Find
(
"Create"
).
GetComponent
<
MapMaker
>
().
GetMapDimensions
(
out
cols
,
out
rows
);
map
+=
rows
+
","
+
cols
+
"\r\n"
;
GameObject
[]
tiles
=
GameObject
.
FindGameObjectsWithTag
(
"Empty Tile"
);
for
(
int
i
=
0
;
i
<
tiles
.
Length
;
i
++)
{
MapMakerTile
tile
=
tiles
[
i
].
GetComponent
<
MapMakerTile
>
();
map
+=
tile
.
ToString
();
map
+=
"\r\n"
;
}
System
.
IO
.
File
.
WriteAllText
(
"Assets/Resources/Maps/"
+
text
.
text
+
".txt"
,
map
);
}
}
Assets/Scripts/MapTool/MapSave.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 2e6e692a569e7da46a8976bfe3764952
timeCreated: 1477687805
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/MapTool/SpriteBar.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
UnityEngine.UI
;
using
System.Collections
;
using
System.Collections.Generic
;
public
class
SpriteBar
:
MonoBehaviour
{
public
GameObject
emptySprite
;
public
RawImage
[]
spriteBar
;
private
GameObject
highliter
;
private
int
highlited
;
private
Scrollbar
scroll
;
private
int
startIndex
;
private
Sprite
[]
sprites
;
void
Start
()
{
highliter
=
GameObject
.
Find
(
"Highlite"
);
sprites
=
Resources
.
LoadAll
<
Sprite
>
(
"Tiles/"
);
scroll
=
GetComponent
<
Scrollbar
>
();
UpdateSpriteBar
();
}
public
void
UpdateSpriteBar
()
{
startIndex
=
(
int
)(
scroll
.
value
*
(
float
)
sprites
.
Length
);
startIndex
-=
startIndex
%
8
;
for
(
int
i
=
0
;
i
<
spriteBar
.
Length
;
i
++)
{
spriteBar
[
i
].
texture
=
null
;
}
for
(
int
i
=
0
;
i
<
spriteBar
.
Length
&&
i
+
startIndex
<
sprites
.
Length
;
i
++)
{
spriteBar
[
i
].
texture
=
sprites
[
startIndex
+
i
].
texture
;
}
}
public
void
SpriteClicked
(
int
i
)
{
highlited
=
i
;
highliter
.
transform
.
position
=
spriteBar
[
i
].
transform
.
position
;
}
public
Sprite
GetHighlitedSprite
()
{
if
(
highlited
+
startIndex
<
sprites
.
Length
)
{
return
sprites
[
highlited
+
startIndex
];
}
return
null
;
}
}
Assets/Scripts/MapTool/SpriteBar.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 24722660838f46d45a3e4ed7e96c1029
timeCreated: 1477615623
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/MapTool/TileDragger.cs
0 → 100644
View file @
4130c051
using
UnityEngine
;
using
System.Collections
;
public
class
TileDragger
:
MonoBehaviour
{
private
Vector2
previousMousePos
;
void
Start
()
{
previousMousePos
=
Camera
.
main
.
ScreenToWorldPoint
(
Input
.
mousePosition
);
}
void
Update
()
{
if
(
Input
.
GetMouseButtonUp
(
0
))
{
RaycastHit2D
hit
=
Physics2D
.
Raycast
(
Camera
.
main
.
ScreenToWorldPoint
(
Input
.
mousePosition
),
Vector3
.
zero
);
if
(
hit
.
collider
!=
null
&&
hit
.
collider
.
gameObject
!=
null
)
{
hit
.
collider
.
gameObject
.
GetComponent
<
SpriteRenderer
>
().
sprite
=
GetComponent
<
SpriteRenderer
>
().
sprite
;
}
Destroy
(
gameObject
);
}
Vector2
mouseDelta
=
(
Vector2
)(
Camera
.
main
.
ScreenToWorldPoint
(
Input
.
mousePosition
))
-
previousMousePos
;
transform
.
position
=
new
Vector3
(
transform
.
position
.
x
+
mouseDelta
.
x
,
transform
.
position
.
y
+
mouseDelta
.
y
,
0
);
previousMousePos
=
Camera
.
main
.
ScreenToWorldPoint
(
Input
.
mousePosition
);
}
}
Assets/Scripts/MapTool/TileDragger.cs.meta
0 → 100644
View file @
4130c051
fileFormatVersion: 2
guid: 653b030dabb60e3488106133a46d35a0
timeCreated: 1477619076
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Assets/Scripts/World/Tile.cs
View file @
4130c051
...
...
@@ -7,14 +7,11 @@ public class Tile : MonoBehaviour, IComparable<Tile>
{
public
bool
walkable
=
true
;
public
float
weight
=
1
;
// The cost for moving through this tile
// Set negative for unwalkable
public
int
x
;
public
int
y
;
public
Color
canPas
=
new
Color
(
255f
,
255f
,
255f
);
public
Color
cantPas
=
new
Color
(
0f
,
0f
,
0f
);
private
SpriteRenderer
sprite
;
private
SpriteRenderer
sr
;
public
Tile
prev
;
...
...
@@ -25,18 +22,7 @@ public class Tile : MonoBehaviour, IComparable<Tile>
public
bool
closed
;
void
Start
()
{
sprite
=
GetComponent
<
SpriteRenderer
>
();
}
void
OnMouseDown
()
{
walkable
=
!
walkable
;
if
(
walkable
)
{
sprite
.
color
=
canPas
;
}
else
{
sprite
.
color
=
cantPas
;
}
sr
=
GetComponent
<
SpriteRenderer
>
();
}
public
int
CompareTo
(
Tile
tile
)
{
...
...
@@ -87,8 +73,11 @@ public class Tile : MonoBehaviour, IComparable<Tile>
return
neighbors
;
}
public
String
toString
()
{
public
void
SetSprite
(
Sprite
sprite
)
{
sr
.
sprite
=
sprite
;
}
public
String
toString
()
{
String
str
=
""
;
str
+=
"("
+
x
+
", "
+
y
+
") "
+
" "
+
weight
;
return
str
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment