Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --monster sad for ludum dare 33
- --by logan buchanan
- --username:ninebears
- up = true
- down = true
- left = true
- right = true
- max_life = 100
- --0 = start
- --1 = game
- --2 = life loss
- --3 = happy loss
- --4 = victory
- state = 0
- --happiness value of objects
- house_worth = 5
- fence_worth = 1
- plant_worth = 2
- tree_worth = 2
- statue_worth= 20
- timer = 0
- function make_object(x, y)
- local o = {}
- o.x = x
- o.y = y
- o.dx = 0
- o.dy = 0
- o.w = 8
- o.h = 8
- o.sprite = 0
- o.type = null
- o.cleared = false
- return o
- end
- function make_player(x, y)
- local pl = make_object(x, y)
- pl.life = 100
- pl.happy = 50
- pl.sprite = 0
- pl.type = 2
- pl.dx = 1.5
- pl.dy = 1.5
- return pl
- end
- function make_enemy(x, y, type)
- local en = make_object(x, y)
- en.type = type
- --peasant
- if(type == 0) then
- en.sprite = 64
- end
- --knight
- if(type == 1) then
- en.sprite = 80
- end
- return en
- end
- function make_king(x, y, type)
- local k = make_enemy(x, y, type)
- k.sprite = 67
- add(enemies,k)
- end
- function make_blood(x, y, type)
- local bl = make_object(x, y)
- bl.type = type
- bl.maxframes = 6
- if(bl.type == 0) then
- bl.init = 96
- bl.frame =96
- end
- if(bl.type == 1) then
- bl.init = 112
- bl.frame =112
- end
- return bl
- end
- function draw_blood(bl)
- if(timer%3 == 0) bl.frame +=1
- if(bl.frame>=bl.init+bl.maxframes) then
- del(blood, bl)
- end
- spr(bl.frame, bl.x, bl.y, 1,1)
- end
- function draw_player(pl)
- if(timer%10 == 0) pl.sprite += 1
- if(pl.sprite>= 2) pl.sprite = 0
- spr(pl.sprite, pl.x-pl.w/2, pl.y-pl.h/2, 1,1)
- end
- function draw_enemy(en)
- if(en.sprite == 67) then
- spr(en.sprite, en.x-en.w/2, en.y-en.h/2,1,1)
- return
- end
- if(en.type == 0)then
- if(timer%5 == 0) en.sprite +=1
- if(en.sprite>66) en.sprite =64
- end
- if(en.type == 1)then
- if(timer%5 == 0) en.sprite +=1
- if(en.sprite>82) en.sprite =80
- end
- spr(en.sprite, en.x-en.w/2, en.y-en.h/2,1,1)
- end
- function move_enemy(en)
- if(en.type == 0)then
- player_pos = make_object(player.x, player.y)
- en_pos = make_object(en.x, en.y)
- sub_vec(player_pos,en_pos)
- normalize(player_pos)
- mult_vec(player_pos, 0.4)
- nullify_en_movement_peasant(en,player_pos)
- sub_vec(en, player_pos)
- end
- if(en.type == 1) then
- player_pos = make_object(player.x, player.y)
- en_pos = make_object(en.x, en.y)
- sub_vec(player_pos,en_pos)
- normalize(player_pos)
- nullify_en_movement_knight(en,player_pos)
- add_vec(en, player_pos)
- end
- end
- function move_player(pl)
- if left then
- pl.x += -pl.dx
- end
- if right then
- pl.x += pl.dx
- end
- if up then
- pl.y += -pl.dy
- end
- if down then
- pl.y += pl.dy
- end
- end
- function spawn_enemy(x, y, type)
- en = make_enemy(x, y, type)
- add(enemies, en)
- end
- function fill_room()
- local x = curr_room.x
- local y = curr_room.y
- local xpos = x*128
- local ypos = y*128
- --all enemy spawns
- --don't judge me i didn't
- --have time to make a
- --better solution
- --top left corner
- if(x==0 and y==0)then
- spawn_enemy(xpos+50, ypos+50,0)
- end
- --statue room
- if(x==1 and y==0)then
- spawn_enemy(xpos+32, ypos+64,1)
- spawn_enemy(xpos+64, ypos+64,1)
- spawn_enemy(xpos+64, ypos+32,1)
- spawn_enemy(xpos+64, ypos+96,1)
- end
- --castle entrance
- if(x==2 and y==0)then
- spawn_enemy(xpos+64, ypos+32,1)
- spawn_enemy(xpos+32, ypos+64,1)
- spawn_enemy(xpos+96, ypos+64,1)
- end
- --kings hall
- if(x==3 and y==0)then
- spawn_enemy(xpos+25, ypos+40,1)
- spawn_enemy(xpos+25, ypos+88,1)
- make_king(xpos+96,ypos+64,0)
- end
- --top right
- if(x==4 and y==0)then
- spawn_enemy(xpos+86, ypos+40,0)
- end
- --left beside village
- if(x==0 and y==1)then
- spawn_enemy(xpos+45, ypos+52,0)
- end
- --left village (tree grove)
- if(x==1 and y==1)then
- spawn_enemy(xpos+40, ypos+40,0)
- spawn_enemy(xpos+40, ypos+85,0)
- spawn_enemy(xpos+55, ypos+100,0)
- spawn_enemy(xpos+100, ypos+30,0)
- spawn_enemy(xpos+90, ypos+110,0)
- end
- --central village
- if(x==2 and y==1)then
- spawn_enemy(xpos+64, ypos+64,0)
- spawn_enemy(xpos+20, ypos+50,0)
- spawn_enemy(xpos+100, ypos+50,0)
- spawn_enemy(xpos+30, ypos+90,0)
- spawn_enemy(xpos+65, ypos+90,0)
- end
- --right village
- if(x==3 and y==1)then
- spawn_enemy(xpos+45, ypos+45,0)
- spawn_enemy(xpos+90, ypos+45,0)
- spawn_enemy(xpos+64, ypos+96,0)
- end
- --solo house right village
- if(x==4 and y==1)then
- spawn_enemy(xpos+30, ypos+35,0)
- end
- --bottom left village
- if(x==0 and y==2)then
- end
- --tree house grove
- if(x==1 and y==2)then
- spawn_enemy(xpos+64, ypos+50,0)
- end
- --village entrance
- if (x == 2 and y == 2)then
- spawn_enemy(xpos+45,ypos+50, 1)
- spawn_enemy(xpos+75, ypos+50,1)
- end
- --farms outside village
- if(x==3 and y==2)then
- spawn_enemy(xpos+45, ypos+60,0)
- spawn_enemy(xpos+85, ypos+60,0)
- end
- --bottom right village
- if(x==4 and y==2)then
- end
- end
- function check_move_inputs()
- if btn(0,0) then
- left = true else left = false
- end
- if btn(1,0) then
- right = true else right = false
- end
- if btn(2,0) then
- up = true else up = false
- end
- if btn(3,0) then
- down = true else down = false
- end
- end
- function check_solids_player(pl,layer)
- --checks left
- if(fget(mget(flr((pl.x/8 - ((pl.w/2)+1)/8)), flr(pl.y/8)), layer)) then
- if(layer == 0 and pl.type == 2)left = false
- if(layer == 0 and pl.type == 0 or pl.type == 1) return 0
- if(layer == 1) then
- spr_num = mget(flr((pl.x/8 - ((pl.w/2)+1)/8)), flr(pl.y/8))
- set_sprite(pl, 0, spr_num)
- sfx(0)
- end
- end
- --checks right
- if(fget(mget(flr((pl.x/8 + ((pl.w/2))/8)), flr(pl.y/8)), layer)) then
- if(layer == 0 and pl.type == 2)right = false
- if(layer == 0 and pl.type == 0 or pl.type == 1) return 1
- if(layer == 1) then
- spr_num = mget(flr((pl.x/8 + ((pl.w/2))/8)), flr(pl.y/8))
- set_sprite(pl, 1, spr_num)
- sfx(0)
- end
- end
- --checks up
- if(fget(mget(flr(pl.x/8), flr((pl.y/8 - ((pl.h/2)+1)/8))), layer)) then
- if(layer == 0 and pl.type == 2)up = false
- if(layer == 0 and pl.type == 0 or pl.type == 1) return 2
- if(layer == 1)then
- spr_num = mget(flr(pl.x/8), flr((pl.y/8 - ((pl.h/2)+1)/8)))
- set_sprite(pl, 2, spr_num)
- sfx(0)
- end
- end
- --checks down
- if(fget(mget(flr(pl.x/8), flr((pl.y/8 + ((pl.h/2))/8))), layer)) then
- if(layer == 0 and pl.type == 2)down = false
- if(layer == 0 and pl.type == 0 or pl.type == 1) return 3
- if(layer == 1) then
- --here you can change the sprite on the map
- --live trees to dead trees!!
- --buildings o destroyed buildings!
- spr_num = mget(flr(pl.x/8), flr((pl.y/8 + ((pl.h/2))/8)))
- set_sprite(pl, 3, spr_num)
- sfx(0)
- end
- end
- end
- function set_sprite_down(pl, n)
- mset(flr(pl.x/8), flr((pl.y/8 + ((pl.h/2))/8)), n)
- end
- function set_sprite_up(pl, n)
- mset(flr(pl.x/8), flr((pl.y/8 - ((pl.h/2)+1)/8)), n)
- end
- function set_sprite_right(pl, n)
- mset(flr((pl.x/8 + ((pl.w/2))/8)), flr(pl.y/8), n)
- end
- function set_sprite_left(pl, n)
- mset(flr((pl.x/8 - ((pl.w/2)+1)/8)), flr(pl.y/8), n)
- end
- function set_sprite(pl, dir, n)
- --left
- if(dir == 0) then
- --tree top
- if(spr_num == 23) then
- set_sprite_left(pl, 24)
- player.happy += tree_worth
- end
- --tree bottom
- if(spr_num == 39) then
- set_sprite_left(pl, 40)
- player.happy += tree_worth
- end
- --houses
- if(spr_num >=70 and spr_num<=72)then
- set_sprite_left(pl,spr_num+3)
- player.happy += house_worth
- end
- --houses
- if(spr_num >=86 and spr_num<=88)then
- set_sprite_left(pl,spr_num+3)
- player.happy += house_worth
- end
- --fence
- if(spr_num == 36) set_sprite_left(pl,37) player.happy += fence_worth
- --statue
- if(spr_num ==25) set_sprite_left(pl,26) player.happy += statue_worth
- --garden
- if(spr_num ==53) set_sprite_left(pl,54) player.happy += plant_worth
- end
- --right
- if(dir == 1) then
- --tree top
- if(spr_num == 23) then
- set_sprite_right(pl, 24)
- player.happy += tree_worth
- end
- --tree bottom
- if(spr_num == 39) then
- set_sprite_right(pl, 40)
- player.happy += tree_worth
- end
- if(spr_num >=70 and spr_num<=72)then
- set_sprite_right(pl,spr_num+3)
- player.happy += house_worth
- end
- if(spr_num >=86 and spr_num<=88)then
- set_sprite_right(pl,spr_num+3)
- player.happy += house_worth
- end
- if(spr_num == 36) set_sprite_right(pl,37) player.happy += fence_worth
- --statue
- if(spr_num ==25) set_sprite_right(pl,26) player.happy += statue_worth
- --garden
- if(spr_num ==53) set_sprite_right(pl,54) player.happy += plant_worth
- end
- --up
- if(dir == 2) then
- --tree top
- if(spr_num == 23) then
- set_sprite_up(pl, 24)
- player.happy += tree_worth
- end
- --tree bottom
- if(spr_num == 39) then
- set_sprite_up(pl, 40)
- player.happy += tree_worth
- end
- if(spr_num >=70 and spr_num<=72)then
- set_sprite_up(pl,spr_num+3)
- player.happy += house_worth
- end
- if(spr_num >=86 and spr_num<=88)then
- set_sprite_up(pl,spr_num+3)
- player.happy += house_worth
- end
- if(spr_num == 36) set_sprite_up(pl,37) player.happy += fence_worth
- --statue
- if(spr_num ==25) set_sprite_up(pl,26) player.happy += statue_worth
- --garden
- if(spr_num ==53) set_sprite_up(pl,54) player.happy += plant_worth
- end
- --down
- if(dir == 3) then
- --tree top
- if(n == 23) then
- set_sprite_down(pl, 24)
- player.happy += tree_worth
- end
- --tree bottom
- if(n == 39) then
- set_sprite_down(pl, 40)
- player.happy += tree_worth
- end
- if(spr_num >=70 and spr_num<=72)then
- set_sprite_down(pl,spr_num+3)
- player.happy += house_worth
- end
- if(spr_num >=86 and spr_num<=88)then
- set_sprite_down(pl,spr_num+3)
- player.happy += house_worth
- end
- if(spr_num == 36) set_sprite_down(pl,37) player.happy += fence_worth
- --statue
- if(spr_num ==25) set_sprite_down(pl,26) player.happy += statue_worth
- --garden
- if(spr_num ==53) set_sprite_down(pl,54) player.happy += plant_worth
- end
- end
- function check_room()
- curr_room.x = flr(player.x/128)
- curr_room.y = flr(player.y/128)
- end
- function box_collide(rect1, rect2)
- if (rect1.x < rect2.x + rect2.w and
- rect1.x + rect1.w > rect2.x and
- rect1.y < rect2.y + rect2.h and
- rect1.h + rect1.y > rect2.y) then
- return true
- end
- return false
- end
- function collisions()
- for en in all(enemies) do
- if(box_collide(player, en)) then
- if(en.type == 0) then
- player.happy += 5
- sfx(2)
- end
- if(en.type == 1) then
- player.happy += 1
- player.life -= 20
- sfx(3)
- end
- if(en.sprite ==67)then
- --victory
- sfx()
- state = 4
- end
- local bl = make_blood(en.x-en.w/2, en.y-en.h/2, en.type)
- add(blood, bl)
- del(enemies, en)
- end
- end
- end
- function store_rooms()
- for x=0,4 do
- for y=0,3 do
- local room = make_object(x, y)
- add(rooms,room)
- end
- end
- end
- function clear_room()
- for rm in all(rooms) do
- if(rm.x == curr_room.x and rm.y == curr_room.y) then
- if(count(enemies)<=0)then
- rm.cleared = true
- end
- end
- end
- end
- --checks if room is cleared
- function room_cleared()
- for rm in all(rooms) do
- if(curr_room.x==rm.x and curr_room.y==rm.y)then
- if(rm.cleared) return true
- end
- end
- return false
- end
- function draw_hud()
- rectfill(curr_room.x*128+10, curr_room.y*128+110, curr_room.x*128+10+(player.life/3), curr_room.y*128+113,8)
- rectfill(curr_room.x*128+10, curr_room.y*128+115, curr_room.x*128+10+(player.happy/3), curr_room.y*128+118,10)
- --print("hp",curr_room.x*128+10, curr_room.y*128+110, 0)
- end
- function update_player_values(pl)
- if(curr_room.x == 2 and curr_room.y == 3) then
- pl.happy -= 0.2
- else
- pl.happy -= 0.1
- end
- if(mget(flr(pl.x/8), flr(pl.y/8)) ==2) then
- if(pl.life < max_life)pl.life += 0.5
- end
- end
- function check_loss(pl)
- if(pl.life <=0)then
- --lose
- sfx(7)
- state = 2
- end
- if(pl.happy <= 0) then
- --lose
- sfx(7)
- state = 3
- end
- end
- function check_z_press()
- if(btn(4,0)) then
- sfx(1)
- if(state == 0) state = 1
- end
- if(btn(5, 0)) then
- if(state == 2 or state == 3 or state == 4) then
- _init()
- end
- end
- end
- function _init()
- state = 0
- timer = 0
- enemies = {}
- blood = {}
- rooms = {}
- prev_room = make_object(0, 0)
- curr_room = make_object(2, 3)
- store_rooms()
- player = make_player(curr_room.x*128+64,curr_room.y*128+64)
- check_room()
- end
- function _update()
- if(state != 1) check_z_press()
- if(state == 1)then
- check_room()
- --check to see if rooms have changed
- if(curr_room.x != prev_room.x or curr_room.y != prev_room.y) then
- for en in all(enemies) do
- del(enemies, en)
- end
- if(not room_cleared())then
- fill_room()
- end
- end
- clear_room()
- check_move_inputs()
- check_solids_player(player, 0)
- check_solids_player(player, 1)
- move_player(player)
- update_player_values(player)
- collisions()
- check_loss(player)
- prev_room = make_object(curr_room.x, curr_room.y)
- timer += 1
- end
- end
- function _draw()
- if(state==0) draw_start()
- if(state==1)then
- cls()
- camera((curr_room.x*128), curr_room.y*128)
- map(0, 0, 0, 0, 256, 256)
- foreach(enemies, draw_enemy)
- foreach(enemies, move_enemy)
- foreach(blood, draw_blood)
- draw_player(player)
- draw_hud()
- end
- if(state == 2) draw_loss_life()
- if(state == 3) draw_loss_happy()
- if(state == 4) draw_victory()
- end
- function draw_start()
- cls()
- camera(0, 0)
- spr(102, 40, 10, 6, 2)
- print("troll is sad!", 40, 40, 7)
- print("his cave make him even sadder!",2, 50, 7)
- print("destruction make troll happy!", 4, 60, 7)
- print("human king make troll angry!", 6, 70, 7)
- print("yellow", 0, 80, 10)
- print("bar is troll happiness", 30, 80, 7)
- print("red", 0, 90, 8)
- print("bar is troll health",15, 90, 7)
- print("cave bed heals, but hurts happy", 0, 100, 7)
- print("kill the human king!",25, 110, 7)
- print("press z to start", 30, 120, 7)
- end
- function draw_victory()
- cls()
- camera(0, 0)
- spr(102, 40, 10, 6, 2)
- print("you killed the human king!", 10, 40, 7)
- print("no more stinky cave for troll!", 0, 50, 7)
- print("troll is happy once more!",12, 60, 7)
- print("press x to restart", 30, 80, 7)
- end
- function draw_loss_life()
- cls()
- camera(0, 0)
- spr(102, 40, 10, 6, 2)
- print("troll was killed!",30, 40, 7)
- print("looks like his search for", 10, 50, 7)
- print("happiness ended poorly", 15, 60, 7)
- print("the human village is safe", 10, 70, 7)
- print("but at what cost?",30, 80, 7)
- print("press x to restart", 30, 100, 7)
- end
- function draw_loss_happy()
- cls()
- camera(0, 0)
- spr(102, 40, 10, 6, 2)
- print("troll became too sad!", 20, 40, 7)
- print("troll just didn't see the point", 0, 50, 7)
- print("troll is going to live in cave", 2, 60, 7)
- print("for rest of sad life.", 20, 70, 7)
- print("press x to restart", 30, 90, 7)
- end
- function sub_vec(vec1, vec2)
- vec1.x -= vec2.x
- vec1.y -= vec2.y
- end
- function add_vec(vec1, vec2)
- vec1.x += vec2.x
- vec1.y += vec2.y
- end
- function mult_vec(vec,sca)
- vec.x = vec.x*sca
- vec.y = vec.y*sca
- end
- function normalize(vec)
- length = sqrt((vec.x*vec.x) + (vec.y*vec.y))
- vec.x = (vec.x) / length
- vec.y = (vec.y) / length
- end
- function nullify_en_movement_knight(en, vec)
- if(check_solids_player(en, 0) == 0) then
- if(vec.x < 0) null_x(vec)
- end
- if(check_solids_player(en, 0) == 1) then
- if(vec.x > 0) null_x(vec)
- end
- if(check_solids_player(en, 0) == 2) then
- if(vec.y < 0) null_y(vec)
- end
- if(check_solids_player(en, 0) == 3) then
- if(vec.y > 0) null_y(vec)
- end
- end
- function nullify_en_movement_peasant(en, vec)
- if(check_solids_player(en, 0) == 0) then
- if(vec.x > 0) null_x(vec)
- end
- if(check_solids_player(en, 0) == 1) then
- if(vec.x < 0) null_x(vec)
- end
- if(check_solids_player(en, 0) == 2) then
- if(vec.y > 0) null_y(vec)
- end
- if(check_solids_player(en, 0) == 3) then
- if(vec.y < 0) null_y(vec)
- end
- end
- function null_x(vec)
- vec.x = 0
- end
- function null_y(vec)
- vec.y = 0
- end
Advertisement
Add Comment
Please, Sign In to add comment