Class: Ronela::Ronela

Inherits:
Object
  • Object
show all
Defined in:
lib/ronela.rb

Overview

Clase principal Ronela Un motor de novelas gráficas simple y en español

Constant Summary collapse

@@audio =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRonela

Returns a new instance of Ronela.



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/ronela.rb', line 453

def initialize
  
  @salir = false
  
  #detener lectura de guion
  @parar = false
  #la escena que se encuentra
  @escena = nil
  #los personaje creados
  @personajes = {}
  #las imagenes cargadas
  @imagenes = {}
  @musicas = {}
  @sonidos = {}
  
  
  #Llama según invocado del guion
  #Aqui es donde se contruye el lenguaje
  #Ronela
  @lenguaje = RonelaLenguaje.new {
    |accion, resto|
    
    if @personajes.has_key? accion
      
      #permite multi linea
      if resto[0] == '"' and resto[-1] != '"'
        @fguion.each_char { |c|
          if c == '"'
            resto.delete! '"'
            break
          else
            resto << c
          end
        }
      end
      
      #dibuja lo que dice el personaje
      @personajes[accion].dice $pantalla, resto, @escena
      
      #detiene la lectura del guion y espera accion del usuarix
      @parar = true
      #Se permite Comentario
    elsif accion == "Comentario"
      ; #se omite
    else
      puts "Se desconoce #{accion}"
    end
  }
  
  #Espera accion del usuarix
  #Esperar
  @lenguaje.Esperar { |variables, resto|
    @parar = true
  }

  #Registra Musica
  #Música <nombre música> <archivo=ruta..>
  @lenguaje.Música { |variables, resto|
    nombre = resto.join(" ")
    archivo = variables["archivo"]
    
    if @@audio and !archivo.nil? and File.exists? archivo
      @musicas[nombre].destroy if @musicas.has_key? nombre
      @musicas[nombre] = SDL::Mixer::Music.load(archivo)
    end
  }
  
  #Registra sonido
  #Sonido <nombre sonido> <archivo=ruta...>
  @lenguaje.Sonido { |variables, resto|
    nombre = resto.join(" ")
    archivo = variables["archivo"]
    
    if @@audio and !archivo.nil? and File.exists? archivo
      @sonidos[nombre].destroy if @sonidos.has_key? nombre
      @sonidos[nombre] = SDL::Mixer::Wave.load(archivo)
    end
  }
  
  #Reproduce sonido o música
  #Reproducir música <nombre música> [volumen=#] [efecto=entrada]
  #Reproducir sonido <nombro sonido> [volumen=#] [efecto=entrada]
  @lenguaje.Reproducir { |variables, resto|
    sl = RonelaLenguaje.new {
      |accion, resto|
      Kernel.puts "No se entiende #{accion} para Reproducir"
    }
    
    resto = resto.join(" ")
    
    vol = 100
    vol = variables["volumen"].to_i if variables.has_key? "volumen"
    efecto = variables["efecto"]
    
    sl.música {|xvariables, resto|
      nombre = resto.join(" ")
      SDL::Mixer.setVolumeMusic(vol)
      
      if @musicas.has_key? nombre
        case efecto
        when 'entrada'
          SDL::Mixer.fadeInMusic(@musicas[nombre], -1, 1000)
        else
          SDL::Mixer.playMusic(@musicas[nombre], -1)
        end
      end
    }
    
    sl.sonido {|xvariables, resto|
      nombre = resto.join(" ")
      SDL::Mixer.setVolume(-1, vol)
      
      if @sonidos.has_key? nombre
        case efecto
        when 'entrada'
          SDL::Mixer.fadeInChannel(-1, @sonidos[nombre], 0, 1000)
        else
          SDL::Mixer.playChannel(-1, @sonidos[nombre], 0)
        end
      end
      
    }
    
    sl.scan resto
  }
  
  #detiene reproducion de la musica
  @lenguaje.Detener { |variables, resto|
    sl = RonelaLenguaje.new {
      |accion, resto|
      Kernel.puts "No se entiende #{accion} para Reproducir"
    }
    sl.música {|xvariables, resto|
      if @musica.kind_of? SDL::Mixer::Music
        SDL::Mixer.haltMusic(@musica)
        @musica.destroy
      end
    }
  }
  
  #Dibuja imágen en ventana
  @lenguaje.Mostrar { |variables, resto|
    nombre = resto.join(" ")
    
    if !nombre.empty? and @imagenes.has_key? nombre
      @imagenes[nombre].cargar_variables variables
      @imagenes[nombre].fondo = @escena
      @imagenes[nombre].dibujar $pantalla

    else
      Kernel.puts "Imagen #{nombre} no existe"
    end
  }
  
  @lenguaje.Ocultar {|variables, resto|
    nombre = resto.join(" ")
    if !nombre.empty? and @imagenes.has_key? nombre
      @imagenes[nombre].limpiar $pantalla
    else
      Kernel.puts "Imagen #{nombre} no existe"
    end
  }
  
  #La escena se debe dibujar siempre en el
  #fondo de la ventana
  @lenguaje.Escena { |variables, resto|
    nombre = resto.join(" ")
    
    if !nombre.empty? and @imagenes.has_key? nombre
      @escena = @imagenes[nombre]
      @escena.es_escena
      @escena.dibujar $pantalla
      @imagenes.each { |k,i| 
        i.fondo = @escena
        i.dibujar $pantalla if i.mostrando and i.tipo != :escena
      }
      $pantalla.flip
    else
      Kernel.puts "Imagen #{nombre} no existe"
    end
  }
  
  #Crea imagen
  @lenguaje.Imagen { |variables, resto|
    nombre = resto.join(" ")
    imagen = nil
    if variables.has_key? "archivo"
      begin
        imagen = Imagen.new(variables["archivo"])
        imagen.fondo = @escena
        imagen.es_imagen
      rescue
        Kernel.puts "No se pudo encontrar imagen #{variables['archivo']}"
      end
      
   
    elsif variables.has_key? "color"
      imagen = ImagenColor.new(variables["color"])
      imagen.fondo = @escena
    end

    @imagenes[nombre] = imagen unless imagen.nil?
  }
  
  #Crea peronaje
  @lenguaje.Personaje { |variables, resto|
    nombre = resto.join(" ")
    if variables.has_key? "apodo"
      color = variables["color"] 
    else
      color = "#fff"
    end
    
    if variables.has_key? "apodo"
      personaje = Personaje.new(nombre, color)
      personaje.cargar_variables(variables)
      @personajes[variables["apodo"]] = personaje
      
    end
  }

  #Personaliza el juego
  @lenguaje.Configurar { |variables, resto|
    pancho = variables["pantalla_ancho"].to_i
    palto = variables["pantalla_alto"].to_i
    
    pxdes = variables["desplaza_x_gui_dialogo"].to_i
    pydes = variables["desplaza_y_gui_dialogo"].to_i
    
    $config[:DESPLAZA_X_PERSONAJE] = pxdes if pxdes > 0
    $config[:DESPLAZA_Y_PERSONAJE] = pydes if pydes > 0
    
    ftnombre = variables["fuente_tamano_nombre"].to_i
    ftmsg = variables["fuente_tamano_msg"].to_i
    
    $config[:FUENTE_TAMANO_NOMBRE] = ftnombre if ftnombre > 0
    $config[:FUENTE_TAMANO_MSG] = ftmsg if ftmsg > 0
    
    
    $config[:PANTALLA_ANCHO] = pancho if pancho > 0
    $config[:PANTALLA_ALTO] = palto if palto > 0
    
    Ronela.iniciar_sdl if pancho > 0 or palto > 0
  }
end

Class Method Details

.audioObject



437
# File 'lib/ronela.rb', line 437

def Ronela.audio; @@audio; end

.iniciar_sdlObject



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/ronela.rb', line 439

def Ronela.iniciar_sdl
  SDL.init(SDL::INIT_VIDEO|SDL::INIT_AUDIO|SDL::INIT_VIDEO)
  SDL::TTF.init unless SDL::TTF.init?
  begin
    SDL::Mixer.open
    @@audio = true
  rescue
    @@audio = false
  end
  
  SDL::WM.setCaption("Ronela", "")
  $pantalla = SDL.setVideoMode($config[:PANTALLA_ANCHO], $config[:PANTALLA_ALTO], $config[:PANTALLA_BPP], $config[:PANTALLA_FLAGS])
end

Instance Method Details

#guardar_bmp_guion(guion) ⇒ Object

Guarda imagenes del guion en bmp creando un directorio del mismo nombre al guion



740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/ronela.rb', line 740

def guardar_bmp_guion guion
   
  #sale si no hay guion
  abort "No se encontro guion" unless File.exists? guion
  
  @fguion = File.open(guion,"r")
  
  dir_bmps = "bmp_#{File.basename(guion)}"
  unless File.directory?(dir_bmps)
    Dir.mkdir(dir_bmps)
  end
  conteo_bmps = 0

  until @salir
    
    if @fguion.eof?
      @salir = true
    end
    
    linea = @fguion.gets
    @lenguaje.scan linea

    #se detiene hasta que alguien diga algo
    if @parar
      #while evento = SDL::Event.wait
      #  case evento
      #  when SDL::Event::MouseButtonDown
      #    #se cancela los sonidos
      #    SDL::Mixer.halt(-1)
      #    break
      #  when SDL::Event::Quit
      #    @salir = true
      #    break
      #  end
      #end 
      @parar = false
      $pantalla.saveBMP(File.join(dir_bmps, "#{conteo_bmps}.bmp"))
      conteo_bmps += 1
      
    end
    
  end
  @fguion.close
end

#leer_guion(guion) ⇒ Object

Lee archivo con guion e interpreta



701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/ronela.rb', line 701

def leer_guion guion
  
  #sale si no hay guion
  abort "No se encontro guion" unless File.exists? guion
  
  @fguion = File.open(guion,"r")
  
  until @salir
    
    if @fguion.eof?
      @salir = true
    end
    
    linea = @fguion.gets
    @lenguaje.scan linea
    
    #se detiene hasta que alguien diga algo
    if @parar
      while evento = SDL::Event.wait
        case evento
        when SDL::Event::MouseButtonDown
          #se cancela los sonidos
          SDL::Mixer.halt(-1)
          break
        when SDL::Event::Quit
          @salir = true
          break
        end
      end 
      @parar = false
    end
    
  end
  @fguion.close
end