-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nim
185 lines (149 loc) · 5.84 KB
/
main.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import std/tables
import nico
import entities/player
import data
import tiles/tile
import tiles/tile_list
import physics_engine
import devtools
var the_player = Player(
position: [0, 0],
velocity: [0, 0],
speed: 50
)
var colour_count = 0
var camera_offset = [0, 0]
proc gameInit() =
#setPalette(loadPaletteFromImage("palettes/colours.png"))
hideMouse()
# Load Sprites/Fonts
loadFont(0, "fonts/font.png")
loadSpritesheet(spritesheetID["cursor"], "cursor.png", 7, 7)
loadSpritesheet(spritesheetID["player"], "entities/player.png", 14, 14)
loadSpritesheet(spritesheetID["amogus"], "amogus.png", 8, 8)
loadSpritesheet(spritesheetID["objects"], "tiles/objects.png", 12, 12)
loadSpritesheet(spritesheetID["generic_floor"], "tiles/generic_floor.png", 12, 12)
loadSpritesheet(spritesheetID["generic_walls"], "tiles/generic_walls.png", 12, 12)
loadSpritesheet(spritesheetID["liquids"], "tiles/liquids.png", 12, 12)
loadSpritesheet(spritesheetID["surface_ground"], "tiles/surface_ground.png", 12, 12)
# Create world
# TODO make actual world generation
var amogus = [0, 0]
for num in 0..1000:
echo num
var surface_tile = grass_floor([amogus[0], amogus[1]])
surface_floor_tilemap.add(surface_tile)
if amogus[0] > 100:
amogus[0] = 0
amogus[1] += 1
else:
amogus[0] += 1
#surface_floor_tilemap[0] = wooden_floor([0, 0])
#surface_floor_tilemap[1] = wooden_floor([1, 0])
#surface_floor_tilemap[2] = wooden_floor([1, 1])
proc gameUpdate(dt: float32) =
# Process player stuff
the_player = the_player.update()
the_player.position = process_velocity(dt, the_player.position, the_player.velocity, the_player.speed)
the_player.velocity = [0, 0]
# Camera movement
if key(K_left):
camera_offset[0] -= 1
if key(K_right):
camera_offset[0] += 1
if key(K_up):
camera_offset[1] -= 1
if key(K_down):
camera_offset[1] += 1
if keyp(K_space):
camera_offset = [0, 0]
# idk testing stuff
if keyp(K_o):
colour_count += 1
if key(K_l):
surface_floor_tilemap.add(water(the_player.position))
if key(K_k):
surface_floor_tilemap.add(dirt(the_player.position))
# Dynamic tilemap loading
if the_player.is_moving:
loaded_tilemap = @[]
for tile in surface_floor_tilemap:
if
tile.coords[0] > (0 + the_player.position[0] - toInt((screenWidth / 2)) - 10) and
tile.coords[1] > (0 + the_player.position[1] - toInt((screenHeight / 2)) - 10) and
tile.coords[0] < (toInt(screenWidth / 2) + the_player.position[0] + 10) and
tile.coords[1] < (toInt(screenHeight / 2) + the_player.position[1] + 10)
:
loaded_tilemap.add(tile)
# Tile animation processing
var tile_counter = 0
for tile in surface_wall_tilemap:
if tile.animated:
surface_wall_tilemap[tile_counter].animation.time_counter += dt
if tile.animation.time_counter >= tile.animation.spf:
surface_wall_tilemap[tile_counter].animation.time_counter -= tile.animation.spf
if tile.animation.current_frame == tile.animation.frames.len() - 1:
surface_wall_tilemap[tile_counter].animation.current_frame = 0
else:
surface_wall_tilemap[tile_counter].animation.current_frame += 1
tile_counter += 1
var second_tile_counter = 0
for tile in surface_floor_tilemap:
if tile.animated:
surface_floor_tilemap[second_tile_counter].animation.time_counter += dt
if tile.animation.time_counter >= tile.animation.spf:
surface_floor_tilemap[second_tile_counter].animation.time_counter -= tile.animation.spf
if tile.animation.current_frame == tile.animation.frames.len() - 1:
surface_floor_tilemap[second_tile_counter].animation.current_frame = 0
else:
surface_floor_tilemap[second_tile_counter].animation.current_frame += 1
second_tile_counter += 1
#echo "\n------------------------------------\n\n", raft_tilemap
proc gameDraw() =
cls()
# Draw background
setCamera(0, 0)
setColor(colour_count) #12
boxfill(0, 0, screenWidth, screenHeight)
setSpritesheet(spritesheetID["amogus"])
spr(0, 0, 0)
# Draw world
setCamera(
get_world_coord(the_player.position[0], camera_offset[0], screenWidth),
get_world_coord(the_player.position[1], camera_offset[1], screenHeight)
)
for tile in surface_floor_tilemap:
if
tile.coords[0] > (0 + the_player.position[0] - toInt((screenWidth / 2)) - 10) and
tile.coords[1] > (0 + the_player.position[1] - toInt((screenHeight / 2)) - 10) and
tile.coords[0] < (toInt(screenWidth / 2) + the_player.position[0] + 10) and
tile.coords[1] < (toInt(screenHeight / 2) + the_player.position[1] + 10)
:
setSpritesheet(spritesheetID[tile.spritesheet])
if tile.animated:
spr(tile.animation.frames[tile.animation.current_frame], tile.coords[0], tile.coords[1])
else:
spr(tile.sprite, tile.coords[0], tile.coords[1])
the_player.draw_shadow()
for tile in surface_wall_tilemap:
if
tile.coords[0] > (0 + the_player.position[0] - toInt((screenWidth / 2)) - 10) and
tile.coords[1] > (0 + the_player.position[1] - toInt((screenHeight / 2)) - 10) and
tile.coords[0] < (toInt(screenWidth / 2) + the_player.position[0] + 10) and
tile.coords[1] < (toInt(screenHeight / 2) + the_player.position[1] + 10)
:
setSpritesheet(spritesheetID[tile.spritesheet])
if tile.animated:
spr(tile.animation.frames[tile.animation.current_frame], tile.coords[0], tile.coords[1])
else:
spr(tile.sprite, tile.coords[0], tile.coords[1])
the_player.draw()
# Draw UI
setCamera(0, 0)
setSpritesheet(spritesheetID["cursor"])
spr(0, mouse()[0] - 3, mouse()[1] - 3)
nico.init("mastrio", "legend_of_terraria")
#fixedSize(true)
#integerScale(true)
nico.createWindow("The Legend of Terraria", 170, 150, 3, false)
nico.run(gameInit, gameUpdate, gameDraw)